長度 n 的 ATCG 各種排列

""" method 1_1 (by York) (rank 5) """ def decode(x,n): s = [] base="ATCG" for i in xrange(n): s += base[x%4] x /= 4 return "".join(s) def gen(n): return [decode(x,n) for x in xrange(4**n)]

""" method 1_2 (by York) (rank 4) """ def mod4(x,n): for i in xrange(n): yield x%4 x /= 4 def decode(x,n): return "".join(["ATCG"[i] for i in mod4(x,n)]) def gen2(n): return [decode(x,n) for x in xrange(4**n)]

""" method 2_1 (by zhouer) (rank 3) """ def gen(n): if n == 0: return [''] else: return [x+y for x in ['A','T','C','G'] for y in gen(n - 1)]

""" method 2_2 (by York) (rank 2) """ def gen(n): if n == 0: return [''] else: return [x+y for x in gen(n-1) for y in 'ATCG']

""" method 2_3 (by York) most fast (rank 1) """ def gen(n): I = list('ATCG') if n == 0: return [''] else: return [x+y for x in gen(n-1) for y in I]

""" method 3_1 (by mantour) (rank 2) """ def gen(n): L = [''] for i in range(n): tmp = [j+k for j in L for k in 'ATCG'] L = tmp return L

""" method 3_2 (by York) (rank 2) """ def gen(n): L = [''] for i in xrange(n): L = [j+k for j in L for k in 'ATCG'] return L

""" method 3_3 (by York) most fast (rank 1) """ def gen(n): L = [''] I = list('ATCG') for i in xrange(n): L = [j+k for j in L for k in I] return L