Skip to content

Instantly share code, notes, and snippets.

@klen
Created November 9, 2013 12:17
Show Gist options
  • Save klen/7384862 to your computer and use it in GitHub Desktop.
Save klen/7384862 to your computer and use it in GitHub Desktop.
Objective-C tutorial
//
// Cell.h
// Cell
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Cell : NSObject
@property NSMutableArray *DNA;
@property (readonly) NSArray *choices;
-(int) hammingDistance:(Cell *)c;
@end
//
// Cell.m
// Cell
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import "Cell.h"
@implementation Cell
-(id) init {
self = [super init];
_choices = [[NSArray alloc] initWithObjects:@"A",@"T",@"G",@"C", nil];
_DNA = [[NSMutableArray alloc] initWithCapacity:100];
for (int i = 0; i < 100; i++) {
int num = arc4random() % 4;
_DNA[i] = _choices[num];
}
return self;
}
-(int) hammingDistance:(Cell *)c {
int distance = 0;
for (int i = 0; i < 100; i++) {
NSString *a = _DNA[i];
NSString *b = [c DNA][i];
distance = a == b ? distance : distance + 1;
}
return distance;
}
@end
//
// main.m
// Cell
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Cell.h"
@implementation Cell (mutator)
-(void) mutate:(int)g {
for (int i = 0; i < g; i++) {
int p = arc4random() % 4;
self.DNA[p] = self.choices[p];
}
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Cell *a, *b;
a = [[Cell alloc] init];
b = [[Cell alloc] init];
int d = [a hammingDistance:b];
NSLog(@"%i", d);
[a mutate:50];
[b mutate:50];
d = [a hammingDistance:b];
NSLog(@"%i", d);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment