/*1. 已知源数组中的元素都是字符串,字符串由字母和空格组成,将数组元素按单词个数排序后返回 (10分)
*/
- (NSArray *)sortByNumberOfWordsInArray:(NSArray *)arr {
NSMutableArray *ary1 = [NSMutableArray new];
NSMutableArray *arr0 = [NSMutableArray arrayWithArray:arr];
for (int i = 0; i < arr.count; i++) {
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[arr[i] componentsSeparatedByString:@" "]];
[arr1 removeObject:@""];
[ary1 addObject:[NSString stringWithFormat:@"%lu",arr1.count]];
[arr1 removeAllObjects];
}
for (int i = 0; i < ary1.count; i++) {
for (int j = i+1; j < ary1.count; j++) {
if ([ary1[i] intValue] > [ary1[j] intValue]) {
[ary1 exchangeObjectAtIndex:i withObjectAtIndex:j];
[arr0 exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
return (NSArray *)arr0;
}
/*
2. 编写函数,将某中间目录插入到指定节点处 (10分)
比如 传入字符串 @"haha", 2 和@"/home/qianfeng/myDir"
返回:@"/home/qianfeng/haha/myDir"
*/
- (NSString *)insertDirectory:(NSString *)dir atIndex:(NSUInteger)index inPath:(NSString *)path{
NSMutableArray *ary =[NSMutableArray arrayWithArray:[dir componentsSeparatedByString:@"/"]];
[ary removeObject:@""];
[ary insertObject:path atIndex:index];
NSMutableString *str = (NSMutableString *)[ary componentsJoinedByString:@"/"];
[str insertString:@"/" atIndex:0];
return (NSString *)str;
}
/*
3.传入两个字符串,找出第二个字符串中每个字符在第一个字符串中出现的次数(10分)
传入:@"hello" @"abel"
返回:@"a:0 b:0 e:1 l:2"
*/
- (NSString *)timesInString:(NSString *)str1 withCharactersInString:(NSString *)str2 {
NSMutableString *str3 = [NSMutableString new];
int count ;
for (int i = 0; i < [str2 length]; i++) {
count = 0;
unichar ch = [str2 characterAtIndex:i];
for (int j = 0; j < [str1 length]; j++) {
unichar ch1 = [str1 characterAtIndex:j];
if (ch == ch1) {
count++;
}
}
[str3 appendFormat:@"%c:%d ",ch,count];
}
[str3 replaceCharactersInRange:NSMakeRange([str3 length]-1, 1) withString:@""];
return (NSString *)str3;
}
/*4.按照新规则比较两个字符串的大小 (10分)
比较两个字符串的大小,首先比较两个字符串最后一个字符,如果最后一个字符相等,比较倒数第二个字符,如果字符相同,更长的大,依次类推,s1大返回1,s2大返回-1,相等返回0
传入@"abc" @"abd" 返回-1
传入@"abc" @"zabc" 返回-1
传入@"abc" @"abc" 返回0
*/
- (int )compareString:(NSString *)src and:(NSString *)dst {
NSUInteger len1 = [src length];
NSUInteger len2 = [dst length];
int flag = 0;
int j = (int)len2-1;
for (int i = (int)len1-1; i >= 0; i--) {
unichar ch = [src characterAtIndex:i];
if (i > 0 && j < 0) {
flag = 1;
break;
}
while (j >= 0) {
unichar ch1 = [dst characterAtIndex:j];
if (ch == ch1) {
j--;
break;
}else if (ch > ch1){
return 1;
}else if (ch < ch1){
return -1;
}
}
}
if (flag == 1) {
return 1;
}else{
return 0;
}
}
/*
5.将字符串中单词按照出现次数排序,每个单词只出现一次,源字符串中单词用下划线连接,生成字符串也应用下滑线连接(20分)
如传入:@"good_good_study_good_study"
返回:@"good_study"
*/
- (NSString *)sortStringByNumberOfWordsFromString:(NSString *)str {
NSMutableArray *ary = (NSMutableArray *)[str componentsSeparatedByString:@"_"];
NSMutableArray *arr = [NSMutableArray new];
[ary removeObject:@""];
int count = 1;
for (int i = 0; i < ary.count; i++) {
if ([ary[i] isEqualToString:@" "]) {
continue;
}
count = 1;
for (int j = i+1; j<ary.count; j++) {
if ([ary[i] isEqualToString:ary[j]]) {
count++;
[ary replaceObjectAtIndex:j withObject:@" "];
}
}
[arr addObject:[NSString stringWithFormat:@"%d",count]];
}
[ary removeObject:@" "];
for (int i = 0; i < arr.count-1; i++) {
for (int j = 0; j < arr.count-1-i; j++) {
if ([arr[j] intValue] < [arr[j+1] intValue]) {
[arr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
[ary exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
return [NSString stringWithString:[ary componentsJoinedByString:@"_"]];
}
/*
6.传入两个字符串,第二个字符串是一串连续数字,将第一个字符串(不超过9个字符),按照第二个字符串中所标示顺序重新排序(20分)
传入:@"abcdef" @"465231"
返回:@"fdeacb"
*/
- (NSString *)sortString:(NSString *)string asOrder:(NSString *)order {
NSMutableString *str1 = [NSMutableString stringWithString:order];
NSMutableString *str2 = [NSMutableString stringWithString:string];
for (int i = 0; i < [str1 length]-1; i++) {
for (int j = 0; j < [str1 length]-1-i; j++) {
if ([[NSString stringWithFormat:@"%c",[str1 characterAtIndex:j]] compare:[NSString stringWithFormat:@"%c",[str1 characterAtIndex:j+1]]] == NSOrderedDescending) {
unichar temp1 = [str1 characterAtIndex:j];
[str1 replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",[str1 characterAtIndex:j+1]]];
[str1 replaceCharactersInRange:NSMakeRange(j+1, 1) withString:[NSString stringWithFormat:@"%c",temp1]];
unichar temp = [str2 characterAtIndex:j];
[str2 replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",[str2 characterAtIndex:j+1]]];
[str2 replaceCharactersInRange:NSMakeRange(j+1, 1) withString:[NSString stringWithFormat:@"%c",temp]];
}
}
}
return (NSString *)str2;
}
/*
7.
编写一个函数,打印下述图形,纪念佐罗先生(20分)
传入:F
打印:
ABCDEF
E
D
C
B
ABCDEF
*/
- (void)printZorroSignForCharacter:(unichar)c {
int n = c - 'A';
NSMutableString *str = [NSMutableString new];
for (int i = 0; i <= n ; i++) {
for (int j = 0; j <= n ; j++) {
if (i == 0 || i == n ) {
[str appendFormat:@"%c",'A'+j];
}else if(i + j == n ){
[str appendFormat:@"%c",'A'+n-i];
}else{
[str appendFormat:@" "];
}
}
[str appendFormat:@"\n"];
}
NSLog(@"\n%@",str);
}
+ (void)test {
LWTest *test = [LWTest new];
//第一题
/*
NSArray *arr = @[@"what you ",@"heool sdhg eui hfjsagl ",@"sdhg ",@"sdhg weg adg",@"sd wyeo ghkad fh ",@"sagh "];
NSLog(@"\n%@",[test sortByNumberOfWordsInArray:arr]);
*/
//第二题
/*
NSString *str = @"/home/qianfeng/myDir";
NSLog(@"\n%@",[test insertDirectory:str atIndex:2 inPath:@"haha"]);
*/
//第三题
/*
NSLog(@"\n%@",[test timesInString:@"hello" withCharactersInString:@"abel"]);
*/
//第四题
//NSLog(@"%d",[test compareString:@"abg" and:@"abd"]);
//第五题
//NSLog(@"%@",[test sortStringByNumberOfWordsFromString:@"good_good_study_good_study_day_day_day_day_up"]);
//第六题
//NSLog(@"\n%@",[test sortString:@"abcdef" asOrder:@"465231"]);
//第七题
[test printZorroSignForCharacter:'G'];
}