Last active
November 18, 2018 13:06
-
-
Save IsmiKin/e5320d956da7ccf94a54bd2d85d49359 to your computer and use it in GitHub Desktop.
Codility challenges
This file contains 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
def fifth_algorithm(data): | |
min_left = data.pop(0) | |
max_left = min_left | |
min_right = None | |
max_right = max(data) | |
overpass_max = False if min_left < max_right else True | |
difference = abs(min_left - max_right) | |
for index, actual_number in enumerate(data): | |
if not overpass_max: | |
if actual_number < min_left: | |
min_left = actual_number | |
difference = abs(min_left - max_right) | |
elif actual_number == max_right: | |
overpass_max = True | |
max_left = actual_number | |
min_right = data[index+1] | |
else: | |
if actual_number < min_right: | |
min_right = actual_number | |
difference = abs(max_right - min_left) | |
return difference |
This file contains 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
def six_algorithm(data): | |
min_left = data.pop(0) | |
max_left = min_left | |
min_right = max(data) | |
max_right = min_right | |
overpass_max = False if min_left < max_right else True | |
difference = abs(min_left - max_right) | |
for actual_number in data: | |
print('left ({} / {}) , right ({} / {})'.format(min_left,max_left, min_right, max_right)) | |
if not overpass_max: | |
if actual_number > max_left: | |
max_left = actual_number | |
difference = abs(max_left - max_right) | |
elif actual_number == max_right: | |
overpass_max = True | |
max_left = actual_number | |
else: | |
if actual_number > max_right: | |
max_right = actual_number | |
difference = abs(max_left - max_right) | |
return difference |
This file contains 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
def solution(A, K): | |
# write your code in Python 3.6 | |
# print('Initial array: {}'.format(A)) | |
array_length = len(A) | |
result_array = A | |
iterator = 0 | |
while iterator < K: | |
# print('Array after {} iterations'.format(iterator)) | |
# print('{}'.format(result_array)) | |
previous_array = result_array.copy() | |
# print('Previous array {}'.format(previous_array)) | |
result_array = previous_array[:-1] | |
result_array.insert(0, previous_array[-1:][0]) | |
iterator = iterator + 1 | |
return result_array |
This file contains 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
def solution(A): | |
# write your code in Python 3.6 | |
big_set = set(range(1,1000000)) | |
small_set = set(A) | |
intersection = big_set.difference(small_set) | |
if intersection is None: | |
return 1 | |
else: | |
return next(iter(intersection)) |
This file contains 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
# you can write to stdout for debugging purposes, e.g. | |
# print("this is a debug message") | |
def solution(A): | |
# write your code in Python 3.6 | |
number_of_pairs = int(len(A)/2) | |
#print('Max num of pairs: {}'.format(number_of_pairs)) | |
paired_numbers = set([]) | |
not_paired_numbers = [] | |
result_number = None | |
for number in A: | |
if number in not_paired_numbers: | |
not_paired_numbers.remove(number) | |
paired_numbers.add(number) | |
elif number not in paired_numbers: | |
not_paired_numbers.append(number) | |
#print('Paired numbers {}'.format(paired_numbers)) | |
#print('Not paired numbers {}'.format(not_paired_numbers)) | |
return not_paired_numbers.pop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment