Created
February 10, 2019 13:13
-
-
Save Morriz/202368d62f423d3ed7f29f5492ee4e7b to your computer and use it in GitHub Desktop.
My solution to codility test GenomicRangeQuery: https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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). | |
* | |
* Approach: | |
* for each range find occurence of A,C,G,T char in range and return corresponding value | |
*/ | |
const solution = (S, P, Q) => { | |
const genMap = { | |
A: 1, | |
C: 2, | |
G: 3, | |
T: 4, | |
} | |
const sStr = 'ACGT' | |
const res = [] | |
for (let i = 0; i < P.length; i++) { | |
const start = P[i] | |
const end = Q[i] + 1 | |
const slice = S.slice(start, end) | |
let min | |
for (let i = 0; i < 4; i++) { | |
const char = sStr[i] | |
if (slice.indexOf(char) > -1) { | |
min = genMap[char] | |
break | |
} | |
} | |
res.push(min) | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment