Skip to content

Instantly share code, notes, and snippets.

View gbazilio's full-sized avatar

Guilherme Bazilio gbazilio

View GitHub Profile
@gbazilio
gbazilio / genomic-query.py
Created January 29, 2017 16:25
Codility - Genomic range query
def solution(S, P, Q):
m_size = len(P)
count_size = len(S) + 1
A = [0] * count_size
C = [0] * count_size
G = [0] * count_size
for i in xrange(len(S)):
nucleotide = S[i]
A[i + 1] += A[i] + (1 if nucleotide == 'A' else 0)
@gbazilio
gbazilio / passing-cars.py
Created January 29, 2017 13:34
Codility - Passing cars
def solution(A):
passing_cars = 0
east_cars = 0
for index in xrange(len(A)):
if A[index] == 1:
passing_cars += east_cars
else:
east_cars += 1
@gbazilio
gbazilio / max-counter.py
Created January 22, 2017 14:56
Codility - Max Counter
def solution(N, A):
max_counter = last_max_counter = 0
counters = [0] * N
for k in xrange(len(A)):
current_counter = A[k]
if current_counter == N+1:
last_max_counter = max_counter
else:
@gbazilio
gbazilio / frog-jump-leaves.py
Created January 12, 2017 21:39
Codiligty - Frog jump leaves
def solution(X, A):
count_array = [0] * (X)
leaves = 0
for index in xrange(len(A)):
if count_array[A[index] - 1] == 0:
count_array[A[index] - 1] = 1
leaves += 1
if leaves == X:
return index
return -1
@gbazilio
gbazilio / missing-integer.py
Created January 11, 2017 00:23
Codility - Missing integer
def solution(A):
count_length = len(A)
count_array = [0] * (count_length + 1)
for value in A:
if value > 0 and value <= count_length:
count_array[value-1] += 1
for index in xrange(len(count_array)):
if count_array[index] == 0:
@gbazilio
gbazilio / permutation.py
Created January 9, 2017 23:46
Codility - Permutation
def solution(A):
n = len(A)
A_sum = sum(set(A))
ideal_sum = n*(n+1)/2
return 1 if A_sum == ideal_sum else 0
@gbazilio
gbazilio / tape-equilibrium.py
Created January 8, 2017 22:25
Codility - Tape equilibirum
def solution(A):
sum_of_part_two = sum(A)
min_difference = None
sum_of_part_one = 0
for i in xrange(1, len(A)):
sum_of_part_one += A[i-1]
sum_of_part_two -= A[i-1]
difference = abs(sum_of_part_one - sum_of_part_two)
if (min_difference == None):
@gbazilio
gbazilio / frog-jump.py
Created January 8, 2017 17:15
Codility - Frog jumps
def solution(X, Y, D):
return int(math.ceil((Y - X) / float(D)))
@gbazilio
gbazilio / missing-element.py
Created January 8, 2017 14:28
Codility - Missing element
def solution(A):
A_length = len(A)
array_sum = sum(A)
complete_sequence_sum = (A_length * (A_length + 1) / 2) + (A_length + 1)
return complete_sequence_sum - array_sum
@gbazilio
gbazilio / odd-occurrences.py
Last active January 17, 2017 20:43
Codility - Odd occurrences
def solution(A):
A = sorted(A)
for index in range(0, len(A), 2):
next_index = index+1
if next_index >= len(A) or A[index] != A[index+1]:
return A[index]