[codility]GenomicRangeQuery

Python代码如下:

def solution(S, P, Q):
    # write your code in Python 2.6
    dict = {'A':0, 'C':1, 'G':2, 'T':3}
    nuclSumTable = [[0 for col in range(len(S)+1)] for row in range(4)]
    #print nuclSumTable
    for value in enumerate(S):
        for i in xrange(0, 4):
            nuclSumTable[i][value[0]+1] = nuclSumTable[i][value[0]]
        nuclSumTable[dict[value[1]]][value[0]+1] = nuclSumTable[dict[value[1]]][value[0]]+1
    
    # query based on sum tables
    #print nuclSumTable
    results = [0]*len(P)
    for i in xrange(0, len(P)):
        for nuclIndex in xrange(0, 4):
            if nuclSumTable[nuclIndex][Q[i]+1] > nuclSumTable[nuclIndex][P[i]]:
                results[i] = nuclIndex+1
                break
            
    return results
    pass

你可能感兴趣的:([codility]GenomicRangeQuery)