计算字符串的标准偏差

时间:2018-06-15 17:02:07

标签: ios objective-c

我希望用户提供一个字符串。该字符串将分为单个字符。每个字符将被放入一个数字(a = 1,b = 2,c = 3等),然后放入一个带有值的数组中,然后计算标准差。

我已经有了转换字母的代码:

for (int i = 0 ; i < str.length ; ++i ) {
    // 'A' unicode value is 65, so by substracting 64 you'll get 1 for A, 2 for B, 3 for C...
    total += [str characterAtIndex:i] - 64;

但是该代码只使用字符串(也许有一种方法可以直接将值放入字符串中?)

标准差的代码:

NSArray *numbers = @[@1, @2];
NSExpression *expression = [NSExpression expressionForFunction:@"stddev:" arguments:@[[NSExpression expressionForConstantValue:numbers]]];
NSNumber *value = [expression expressionValueWithObject:nil context:nil];
NSLog(@"%@,", value);

1 个答案:

答案 0 :(得分:0)

如果您的问题是“如何将字母串转换为数字数组?”,请尝试以下方法:

export class MapComponent implements OnInit {
  constructor() { }
  options = {...};
  layers = [ ... ];
  layersControl = {
    overlays: {
      'Map WMS Layer": tileLayer.wms('http://...', {version: '1.1.1', newModelName: 'default', view: 'default', year: '2018');
    }
  };
  ngOnInit() {}
  initMap(map: Map) {...}
  updateLayer($event) {
    let overlays = {};
    overlays[$event.newKey] = tileLayer.wms('http://...', { 
      version: '1.1.1',
      newModelName: $event.newModelName,
      view: $event.view,
      year: $event.year
    });
    this.layersControl.overlays = overlays;
  }
}