-
-
Save netman92/8536efc9d59446e43a6f to your computer and use it in GitHub Desktop.
codility
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
| #!/usr/bin/python | |
| def solution(X, A): | |
| n = len(A) | |
| right = sum(1 for item in A if item != X) | |
| left = 0 | |
| for k, element in enumerate(A, start=1): | |
| if element == X: | |
| left += 1 | |
| else: | |
| right -= 1 | |
| if right and left == right: | |
| return k | |
| return -1 | |
| if __name__ == "__main__": | |
| assert solution(5, [5,5,1,7,2,3,5]) == 4 | |
| assert solution(1, []) == -1 | |
| assert solution(1, [0]*20) == -1 | |
| assert solution(1, [1,2]) == 1 | |
| assert solution(1, [1,1,1,1,1,1,1,1,2]) == 1 | |
| assert solution(1, [1]*50 + [0]*50) == 50 | |
| assert solution(1, [1]*50000 + [0]*50000) == 50000 |
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
| #!/usr/bin/python | |
| def to_binary(n): | |
| if n == -1: | |
| return [1] | |
| else: | |
| return [-1*(n%-2)] + to_binary(n/-2) | |
| def solution(A): | |
| num = sum(bit*(-2)**i for i, bit in enumerate(A) if bit != 0) | |
| return to_binary(num) | |
| if __name__ == "__main__": | |
| assert solution([1,0,0,1,1]) == [1,1,0,1] | |
| assert solution([1,0,0,1,1,1]) == [1,1,0,1,0,1,1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment