Created
June 21, 2020 14:12
-
-
Save LeperGnome/56ecad1489c45846f50f160733918a61 to your computer and use it in GitHub Desktop.
Tetrika interview
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
def search_pairs(array, k): | |
seen = {} | |
pairs = [] | |
for idx, num in enumerate(array): | |
comp_idx = seen.get(k-num, None) | |
if comp_idx is not None: | |
comp = array[comp_idx] | |
pair = (min(num, comp), max(num, comp)) | |
pairs.append(pair) | |
seen[num] = idx | |
return list(set(pairs)) | |
array = [1, 2, 6, 5, 3, 4, 7, 8, 3, 2] | |
k = 5 | |
print(search_pairs(array, k)) # --> [(1, 4), (2, 3)] | |
# Ответы на вопросы: | |
# 1. Сложность алгоритма - O(n) | |
# 2. Возможно, в моей реализации неоптимально обеспечивается уникальность | |
# ( list(set(...)) ) |
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
import re | |
def get_alphabet_sum(word): | |
alp_sum = 0 | |
for letter in word: | |
alp_sum += ord(letter)-64 | |
return alp_sum | |
def analyze_names(fname='names.txt'): | |
with open(fname) as f: | |
names_raw = f.read() | |
p = re.compile(r'"([A-Z]*?)"') | |
names = p.findall(names_raw) | |
names.sort() | |
result = 0 | |
for idx, name in enumerate(names): | |
result += (idx+1) * get_alphabet_sum(name) | |
return result | |
print(analyze_names()) # --> Ответ: 871853874 |
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
def get_zeros(num): | |
count = 0 | |
power5 = 5 | |
while (num / power5 >= 1): | |
count += int(num / power5) | |
power5 *= 5 | |
return count | |
print(get_zeros(12031283)) | |
# Сложность алгоритма - O(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment