-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLesson 3 - GenomicRangeQuery.m
63 lines (52 loc) · 2.24 KB
/
Lesson 3 - GenomicRangeQuery.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#import <Foundation/Foundation.h>
NSMutableArray * solution(NSString *S, NSMutableArray *P, NSMutableArray *Q) {
NSMutableArray * values = [@[] mutableCopy];
NSMutableArray * results = [@[] mutableCopy];
NSMutableDictionary * acgtCount = [@{@"1": [@[] mutableCopy],
@"2": [@[] mutableCopy],
@"3": [@[] mutableCopy],
@"4": [@[] mutableCopy]} mutableCopy];
S = [S stringByReplacingOccurrencesOfString:@"A" withString:@"1"];
S = [S stringByReplacingOccurrencesOfString:@"C" withString:@"2"];
S = [S stringByReplacingOccurrencesOfString:@"G" withString:@"3"];
S = [S stringByReplacingOccurrencesOfString:@"T" withString:@"4"];
for (int j = 1; j <= 4; j++) {
for (int i = 0; i < [S length]; i++) {
NSMutableArray * positions = acgtCount[[NSString stringWithFormat:@"%i", j]];
[positions addObject:@0];
}
}
for (int i = 0; i < [S length]; i++) {
NSString * ch = [S substringWithRange:NSMakeRange(i, 1)];
NSMutableArray * positions = acgtCount[ch];
positions[i] = @1;
[values addObject:[NSNumber numberWithInt:[ch intValue]]];
}
for (int j = 1; j <= 4; j++) {
for (int i = 1; i < [S length]; i++) {
NSMutableArray * positions = acgtCount[[NSString stringWithFormat:@"%i", j]];
positions[i] = [NSNumber numberWithInt:[positions[i] intValue] + [positions[i-1] intValue]];
}
}
for (int i = 0; i < [P count] ; i++) {
int p = [P[i] intValue];
int q = [Q[i] intValue];
int tmpIdx = 0;
for (int j = 1; j <= 4; j++) {
NSMutableArray * positions = acgtCount[[NSString stringWithFormat:@"%i", j]];
int qValue = [positions[q] intValue];
int pValue = [positions[p] intValue];
if (p == q && qValue > 0) {
tmpIdx = j;
}
if (qValue - pValue > 0) {
[results addObject:[NSNumber numberWithInt:j]];
break;
}
}
if (tmpIdx > 0) {
[results addObject:[NSNumber numberWithInt:tmpIdx]];
}
}
return results;
}