-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12 GenomicRangeQuery
More file actions
63 lines (52 loc) · 2.41 KB
/
12 GenomicRangeQuery
File metadata and controls
63 lines (52 loc) · 2.41 KB
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
/*------------------------------------------------------
A DNA sequence can be represented as a string consisting of the letters A, C, G and T,
which correspond to the types of successive nucleotides in the sequence.
Each nucleotide has an impact factor, which is an integer.
Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively.
You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained
in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters.
There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers.
The K-th query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in
the DNA sequence between positions P[K] and Q[K] (inclusive).
Write a function that, given a non-empty string S consisting of N characters and two non-empty arrays P and
Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
Result array should be returned as an array of integers.
------------------------------------------------------*/
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S, P, Q) {
// write your code in JavaScript (Node.js 8.9.4)
let answers = [];
let nucleoids = {
A: [0],
C: [0],
G: [0],
T: [0]
}
for (let i=0; i<S.length; i+=1) {
if(i === 0) {
nucleoids[S[i]][0] = 1;
continue;
}
nucleoids.A[i] = nucleoids.A[i-1];
nucleoids.C[i] = nucleoids.C[i-1];
nucleoids.G[i] = nucleoids.G[i-1];
nucleoids.T[i] = nucleoids.T[i-1];
nucleoids[S[i]][i] += 1;
}
for (let i=0; i<P.length; i++) {
let rangeStart = P[i];
let rangeEnd = Q[i];
if (nucleoids.A[rangeEnd] - nucleoids.A[rangeStart] > 0 || S[rangeStart] === 'A') {
answers[i] = 1;
} else if (nucleoids.C[rangeEnd] - nucleoids.C[rangeStart] > 0 || S[rangeStart] === 'C') {
answers[i] = 2;
} else if (nucleoids.G[rangeEnd] - nucleoids.G[rangeStart] > 0 || S[rangeStart] === 'G') {
answers[i] = 3;
} else {
answers[i] = 4;
}
}
return answers;
}