1 2 3 4 5 6 7 8 |
def solution(A): for i in A: if i not in range(1, len(A)+1): return 0 else: if A.count(i) > 1: return 0 return 1 |

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 |
def solution(S, P, Q): # write your code in Python 2.7 n = len(S) A = [0] * n C = [0] * n G = [0] * n T = [0] * n for i in range(n): if S[i] == 'A': A[i] += 1 elif S[i] == 'C': C[i] += 1 elif S[i] == 'G': G[i] += 1 else: T[i] += 1 P_A = prefix(A) P_C = prefix(C) P_G = prefix(G) P_T = prefix(T) result = [] for i, j in zip(P,Q): if P_A[j+1] - P_A[i] > 0: result.append(1) elif P_C[j+1] - P_C[i] > 0: result.append(2) elif P_G[j+1] - P_G[i] > 0: result.append(3) else: result.append(4) return result def prefix(l): m = len(l) P = [0]*(m+1) for i in range(1,m+1): P[i] = P[i-1] + l[i-1] return P |

1 2 |
def solution(A): return len(set((abs(x) for x in A))) |

1 2 3 4 5 6 7 |
def solution(S): if len(S) == 0: return '' for i in range(len(S)): if a[:i][::-1] == a[i+1:]: return i return -1 |