Last active
January 29, 2018 08:26
-
-
Save giordanocardillo/3a597ad81be06c168a154a9d94d9ef1e 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
Codility Tests |
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
#100% | |
def solution(N): | |
binary_string = "{0:b}".format(N) | |
max_zeroes = 0 | |
count = 0 | |
for letter in binary_string: | |
if letter == "0": | |
count += 1 | |
if letter == "1": | |
if count > max_zeroes: | |
max_zeroes = count | |
count = 0 | |
return max_zeroes |
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
#100% | |
def solution(A): | |
odd = 0 | |
for number in A: | |
odd ^= number | |
return odd |
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
#100% | |
def solution(A, K): | |
if len(A) > 0: | |
for iteration in range(K): | |
last_a_number = A.pop() | |
A.insert(0, last_a_number) | |
return A |
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
#100% | |
def solution(A): | |
total = sum(A) | |
splits = len(A) - 1 | |
left = 0 | |
diff = float("inf") | |
for index, number in enumerate(A): | |
if index < splits: | |
left += number | |
right = total - left | |
diff = min(diff, abs(left - right)) | |
return diff |
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
#100% | |
import math | |
def solution(X, Y, D): | |
return math.ceil((Y - X) / D) |
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
#100% | |
def solution(A): | |
total = sum(A) | |
n_plus_one = len(A) + 1 | |
sum_up_to_n_plus_one = (n_plus_one * (n_plus_one + 1)) // 2 | |
return abs(total - sum_up_to_n_plus_one) |
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
#100% | |
def solution(A): | |
seen = [None] * (len(A) + 1) | |
for number in A: | |
if (number < 1 or number > len(A)) or seen[number]: | |
return 0 | |
seen[number] = True | |
return 1 | |
#OR | |
def solution(A): | |
seen = set() | |
for number in A: | |
if number in seen or (number < 0 or number > len(A)): | |
return 0 | |
seen.add(number) | |
return 1 |
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
#100% | |
def solution(X, A): | |
leaves = set() | |
for second, leaf_position in enumerate(A): | |
leaves.add(leaf_position) | |
if len(leaves) == X: | |
return second | |
return -1 |
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
#100% | |
def solution(A): | |
smallest = 1 | |
check = [False] * (len(A) + 1) | |
for number in A: | |
if 0 < number < (len(A) +1): | |
check[number - 1] = True | |
if number <= smallest: | |
while check[smallest - 1]: | |
smallest += 1 | |
return smallest | |
#OR | |
def solution(A): | |
seen = set() | |
for number in A: | |
if 0 < number <= len(A) + 1: | |
seen.add(number) | |
for number in range(1, len(A) + 2): | |
if number not in seen: | |
return number | |
return 1 | |
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
#100% | |
def solution(N, A): | |
counters = [0] * N | |
max_value = 0 | |
reset_to = 0 | |
for position in A: | |
if 0 <= position <= N: | |
if counters[position - 1] < reset_to: | |
counters[position - 1] = reset_to | |
counters[position - 1] += 1 | |
if counters[position - 1] > max_value: | |
max_value = counters[position -1] | |
if position == N + 1: | |
reset_to = max_value | |
for index, counter in enumerate(counters): | |
if counters[index] < reset_to: | |
counters[index] = reset_to | |
return counters |
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
#100% | |
def solution(A, B, K): | |
return (B//K) - (A//K) + (1 if A % K == 0 else 0) |
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
#100% | |
def solution(A): | |
total = 0 | |
counter = 0 | |
for number in reversed(A): | |
if number == 1: | |
counter += 1 | |
elif number == 0: | |
total += counter | |
if total > 1e9: | |
return -1 | |
return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment