Perimeter of squares in a rectangle

#import <Foundation/Foundation.h>

unsigned long long perimeter(int n) {
    NSMutableArray *numQu = @[@0,@1].mutableCopy;
    unsigned long long n0 = 0,n1=1;
    for (int i = 2; i<=n+1; i++) {
        unsigned long long sum = n0+n1;
        n0 = n1;
        n1 = sum;
        [numQu addObject:@(sum)];
    }
    unsigned long long tol = 0;
    for (NSNumber* ele in numQu) {
        tol+=[ele unsignedLongLongValue];
    }
    return tol*4;
}
2018/1/6 posted in  codeWars

AFNetworking遇到的问题

由于AFNetworking运用了官方NSURLSession所以其中有一些坑

1.苹果运用NSJSONSerialization解析,出现数字类型精度问题

当服务器给我传回来一个3.0的数字类型时,在安卓端是没有问题的,但是在iOS这里会出现2.99999这样的问题。

出现这个问题的原因是:苹果在json解析时,默认为双精度的double类型。

我们相处的解决方案有两种:
1. 跟后台协商将数字型的值改为`字符型`
2. 使用第三方的json解析。

最后我们使用的是第一种方法,因为第二种势必要修改了AFNetwork的源码,开发与维护成本相比较来说要大。

2.json解析失败

由于AF默认的解析方式为0(返回的对象是不可变的,NSDictionaryNSArray):

我们来看一下都有什么选项:

所以当我们服务器返回的json数据是碎片化的(最外层既不是NSArray也不是NSDictionary),那么解析的时候就会出错了。

解决方法是:


在0后添加|字符,增加这种情况,允许碎片化数据。

3. 请求后response的状态码范围问题

正常项目中正常请求成功会返回200,但是服务器若是给你返回了500(我不知道后台为啥会返回这个码),问题就出现了:

这是由于下面的原因:

会发现上图中可接受状态码的范围是200-300;

具体解决方法就不说了。。。

2018/1/5 posted in  iOS

1's, 0's and wildcards

#import <Foundation/Foundation.h>

NSArray *changeTo(NSMutableArray*log, NSMutableArray*res){
    if (log.count>0) {
        NSRange loc = {[[log lastObject] integerValue],1};
        NSMutableArray *cop = res.mutableCopy;
        for (NSString *tmp in res) {
            [cop addObject:[tmp stringByReplacingCharactersInRange:loc withString:@"0"]];
            [cop addObject:[tmp stringByReplacingCharactersInRange:loc withString:@"1"]];
            [cop removeObject:tmp];
        }
        [log removeLastObject];
        return changeTo(log, cop);
    }else{
        return res;
    }
}

NSArray *possibilities(NSString *s) {
    NSMutableArray *log = @[].mutableCopy;
    NSMutableArray *res = @[].mutableCopy;
    for (int i = 0; i<s.length; i++) {
        if ([s characterAtIndex:i]=='?') {
            [log addObject:@(i)];
        }
    }
    [res addObject:s];

    NSArray *tol = changeTo(log , res);
    
    return tol;
}

简化版:

#import <Foundation/Foundation.h>

NSArray *possibilities(NSString *s) {
  // NSLog(@"User `possibilities` accidentally invoked!\n");
  if ([s rangeOfString: @"?"].length == 0) return @[s];
  NSString *r = [s stringByReplacingOccurrencesOfString: @"?" withString: @"0" options: 0 range: [s rangeOfString: @"?"]], *t = [s stringByReplacingOccurrencesOfString: @"?" withString: @"1" options: 0 range: [s rangeOfString: @"?"]];
  return [possibilities(r) arrayByAddingObjectsFromArray: possibilities(t)];
}
2017/12/29 posted in  codeWars

Simple Pig Latin

一开始的复杂版:

#import <Foundation/Foundation.h>

NSString *pigIt(NSString *s) {
    NSMutableArray *str = [s componentsSeparatedByString:@" "].mutableCopy;
    NSMutableString *res = @"".mutableCopy;
    for (NSString *tmp in str) {
        unichar c = [tmp characterAtIndex:0];
        if ((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
            NSMutableString *change = tmp.mutableCopy;
            [change appendFormat:@"%c",c];
            if ([tmp isEqualToString:[str lastObject]]) {
                [change appendString:@"ay"];
            }else{
                [change appendString:@"ay "];
            }
            [res appendString:[change substringFromIndex:1]];
        }else{
            [res appendFormat:@"%c",c];
        }
    }
    return res;
}

简化版

#import <Foundation/Foundation.h>

NSString *pigIt(NSString *s) {
  NSMutableArray *result = [s componentsSeparatedByString: @" "];
  for (size_t i = 0; i < [result count]; i++) result[i] = [[[result[i] substringFromIndex: 1] stringByAppendingString: [result[i] substringToIndex: 1]] stringByAppendingString: @"ay"];
  return [result componentsJoinedByString: @" "];
}
2017/12/27 posted in  codeWars

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