I recently discovered Codility and I'm going on with the demo training. I wrote this solution to the Genomic Range Query problem, it works fine, solution is provided with dynamic programming, but it scores only 87% instead of 100% as expected.
Anyone has any idea?
Here you can find the problem, it is in the Prefix section. Just start a test to see the problem description! Codility training
Thank you!
def solution(S, P, Q):
# write your code in Python 2.6
S = list(S)
sol = [[0]*len(S),[0]*len(S),[0]*len(S),[0]*len(S)]
mapping = {"A":1, "C":2, "G":3, "T":4}
for i in range(0,len(S)):
if S[i] == 'A':
sol[0][i]+= 1
elif S[i] == 'C':
sol[1][i] += 1
elif S[i] == 'G':
sol[2][i] += 1
elif S[i] == 'T':
sol[3][i] += 1
if i < len(S)-1:
sol[0][i+1] = sol[0][i]
sol[1][i+1] = sol[1][i]
sol[2][i+1] = sol[2][i]
sol[3][i+1] = sol[3][i]
for n in range(0, len(P)):
l = P[n]
r = Q[n]
pre_sum = [0,0,0,0]
if l > 0:
pre_sum = [sol[0][l],sol[1][l],sol[2][l],sol[3][l]]
post_sum = [sol[0][r],sol[1][r],sol[2][r],sol[3][r]]
if post_sum[0]-pre_sum[0] > 0:
P[n] = 1
elif post_sum[1]-pre_sum[1] > 0:
P[n] = 2
elif post_sum[2]-pre_sum[2] > 0:
P[n] = 3
elif post_sum[3]-pre_sum[3] > 0:
P[n] = 4
else:
P[n] = mapping[S[P[n]]];
return P
pass
This works 100/100 too
def solution(S, P, Q):
res = []
for i in range(len(P)):
if 'A' in S[P[i]:Q[i]+1]:
res.append(1)
elif 'C' in S[P[i]:Q[i]+1]:
res.append(2)
elif 'G' in S[P[i]:Q[i]+1]:
res.append(3)
else:
res.append(4)
return res
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With