当先锋百科网

首页 1 2 3 4 5 6 7

- (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;

}


- (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;

}


- (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;

}

- (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;
    }
    
}



- (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:@"_"]];

}


- (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;
}



- (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];
    //第一题
   
    
    //第二题
   
    
    //第三题
   
    
    //第四题
    //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'];
    
    
    
    
}