Sum without highest and lowest number

int sum(int* numbers, int numbersCount)
{
    if(numbersCount<=1) return 0;
    int min = INT_MAX;
    int max = INT_MIN;
    int sum = 0;
    for (int i = 0; i<numbersCount; i++)
    {
        if(numbers[i]>max) max = numbers[i];
        if(numbers[i]<min) min = numbers[i];
        sum+= numbers[i];
    }
    return sum-min-max;
}

2017/12/27 posted in  codeWars

Find the missing term in an Arithmetic Progression

#import <Foundation/Foundation.h>

NSNumber *findMissing(NSArray *list) {
    int len = [list count];
    int n = len+1;
    int d1 = [list[1] intValue]-[list[0] intValue];
    int d2 = [list[len-1] intValue]-[list[len-2] intValue];
    int d = 0;
    if (d1<0) {
        d = d1>d2?d1:d2;
    }else{
        d = d1<d2?d1:d2;
    }
    if (n == 3) {
        return @(([[list firstObject] intValue]+[[list lastObject] intValue])/2);
    }
    int num = [[list firstObject] intValue];
    for (int i = 0;i<len;i++) {
        if ([list[i] intValue]!=num) {
            return @(num);
        }
        num+=d;
    }
    
    return @(0);
}
2017/12/26 posted in  codeWars

Valid Parentheses

#import <Foundation/Foundation.h>

bool validParentheses(NSString *s) {
    // TODO: Return a boolean value indicating whether the
    // order of parentheses in the given string is valid
    if (s.length==0) {
        return 0;
    }
    int sum = 0;
    NSMutableArray *res = @[].mutableCopy;
    for (int i = 0; i<s.length; i++) {
        if ([s characterAtIndex:i]==(unichar)'(') {
            [res addObject:@('(')];
            sum++;
        }
        if ([s characterAtIndex:i]==(unichar)')') {
            if ([[res lastObject] isEqual:@('(')]) {
                [res removeObjectAtIndex:res.count-1];
            }else{
                [res addObject:@(')')];
            }
            sum--;
        }
    }
    if (sum!=0||[res count]!=0) {
        return 0;
    }
    return 1;
}

简化版:


#import <Foundation/Foundation.h>

bool validParentheses(NSString *s) {
  int brackets = 0;
  for (int i = 0; i < [s length]; i++) {
    if ([s characterAtIndex: i] == '(') brackets++;
    else if ([s characterAtIndex: i] == ')') if (--brackets < 0) return false;
  }
  return !brackets;
}
2017/12/25 posted in  codeWars

Follow that Spy

自己的麻烦些的代码:用了NSMapTable

#import <Foundation/Foundation.h>
NSString *findRoutes(NSArray *routes){
    if ([routes count]==1) {
        return [NSString stringWithFormat:@"%@, %@",routes[0][0],routes[0][1]];
    }
    NSMapTable *map = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
    NSMapTable *tol = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
    for (NSArray *tmp in routes) {
        [map setObject:tmp[1] forKey:tmp[0]];
        for (NSString* tmpTol in tmp) {
            if ([tol objectForKey:tmpTol]) {
                [tol setObject:@"1" forKey:tmpTol];
            }else{
                [tol setObject:@"0" forKey:tmpTol];
            }
        }
    }
    NSString *str = @"";
    NSString *end = @"";
    NSEnumerator *enumerator = [tol keyEnumerator];
    for (NSString *tmp in enumerator) {
        if ([[tol objectForKey:tmp] isEqualToString:@"0"]) {
            if (![map objectForKey:tmp]) {
                end = tmp;
                continue;
            }
            str = tmp;
        }
    }
    NSString *res = @"";
    res = [res stringByAppendingFormat:@"%@, ", str];
    while ([map objectForKey:str]) {
       res =  [res stringByAppendingFormat:@"%@, ", [map objectForKey:str]];
       str = [map objectForKey:str];
        if ([[map objectForKey:str] isEqualToString:end]) {
            res = [res stringByAppendingString:end];
            break;
        }
    }
    return res;
}

别人的简短代码:

#import <Foundation/Foundation.h>
NSString *findRoutes(NSArray *routes){

    NSMutableArray * routesM = [routes mutableCopy];
    NSMutableArray * answer = [@[routes[0][0]] mutableCopy];
    do{
        for(int i = (int)routesM.count - 1; i >= 0; i--){
            if([[answer lastObject] isEqualToString:routesM[i][0]]){
                [answer addObject:routesM[i][1]];
                [routesM removeObjectAtIndex:i];
            } else if([[answer firstObject] isEqualToString:routesM[i][1]]){
                [answer insertObject:routesM[i][0] atIndex:0];
                [routesM removeObjectAtIndex:i];
            }
        }
    } while (routesM.count > 0);
    return [answer componentsJoinedByString:@", "];
}
2017/12/23 posted in  codeWars

Simple Fun #4: Phone Call


#import <Foundation/Foundation.h>
int phoneCall(int min1, int min2_10, int min11, int s) {
    if (s<min1) return 0;
    int sum = 1;
    sum+= (s-min1)>min2_10*9 ? 9 : (s-min1)/min2_10;
    sum+= (s-min1-9*min2_10)<0 ? 0 : (s-min1-9*min2_10)/min11;
    return sum;
}
2017/12/23 posted in  codeWars

Don't give me five!

Don't give me five!

In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!

Examples:

1,9 -> 1,2,3,4,6,7,8,9 -> Result 8
4,17 -> 4,6,7,8,9,10,11,12,13,14,16,17 -> Result 12

The result may contain fives. ;-)
The start number will always be smaller than the end number. Both numbers can be also negative!

I'm very curious for your solutions and the way you solve it. Maybe someone of you will find an easy pure mathematics solution.

Have fun coding it and please don't forget to vote and rank this kata! :-)

#import <Foundation/Foundation.h>
int dontGiveMeFive(int start, int end)
{
  int sum = 0;
    for (int i = start; i<=end; i++) {
        if ([[NSString stringWithFormat:@"%i",i] rangeOfString:@"5"].location!=NSNotFound) {
            continue;
        }
        sum++;
    }
    return sum;
}
2017/12/23 posted in  codeWars