Skip to content

Instantly share code, notes, and snippets.

@tmshv
Created November 12, 2013 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmshv/7424060 to your computer and use it in GitHub Desktop.
Save tmshv/7424060 to your computer and use it in GitHub Desktop.
Hexlet:: OS X #3, 7 http://hexlet.org/lesson/osx_3_7/
//
// DNA.h
// Hexlet DNA
//
// Created by Roman Timashev on 12/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Cell : NSObject
@property NSMutableArray *dna;
-(int) hammingDistance: (Cell*) cell;
@end
//
// DNA.m
// Hexlet DNA
//
// Created by Roman Timashev on 12/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import "Cell.h"
@implementation Cell{
}
-(id) init{
self = [super init];
_dna = [[NSMutableArray alloc] init];
NSString *dnaSymbols = @"ATGC";
for (int i = 0; i<100; i++) {
NSUInteger index = (int) (rand() % 4);
unichar c = [dnaSymbols characterAtIndex:index];
NSString *s = [NSString stringWithCharacters:&c length:1];
[_dna setObject:s atIndexedSubscript:i];
}
return self;
}
-(int) hammingDistance:(Cell*) cell{
int dist = 0;
for(int i=0; i<[_dna count]; i++){
NSString *dna1 = [self.dna objectAtIndex:i];
NSString *dna2 = [cell.dna objectAtIndex:i];
if([dna1 isEqualToString:dna2]) dist ++;
}
return dist;
}
-(NSString*) description{
NSString *out = @"";
for(int i=0; i<[_dna count]; i++){
NSString *dnaSymbol = [_dna objectAtIndex:i];
out = [out stringByAppendingString:dnaSymbol];
}
return out;
}
@end
//
// main.m
// Hexlet DNA
//
// Created by Roman Timashev on 12/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Cell.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Cell *c1 = [[Cell alloc] init];
Cell *c2 = [[Cell alloc] init];
// insert code here...
NSLog(@"%@", c1);
NSLog(@"%@", c2);
NSLog(@"%i", [c1 hammingDistance:c2]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment