-
-
Save schepelin/205504f02ea59c08187f to your computer and use it in GitHub Desktop.
hh test
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
| # coding: utf-8 | |
| def hh_2_brute_force(subsequence): | |
| subsequence = str(subsequence) | |
| limit = int('1'+subsequence) if subsequence.startswith('0') else int(subsequence) | |
| sequence = ''.join([str(num) for num in range(1, limit+1)]) | |
| return sequence.index(subsequence)+1 | |
| # настрой себе линтер что бы видеть ошибки pep8 | |
| # не делай функий "сделать все" их тяжело тестировать, читать | |
| # и тем более рефакторить (т.к. тяжело тестировать) | |
| def hh_2_smart(subsequence, step=1000, limit=1000000000): # limit не нужен | |
| subsequence = str(subsequence) | |
| initial = 1 | |
| index_storage = 0 | |
| sequence_storage = '' | |
| # while True и проверка subsequence.isdigit() | |
| while initial < limit or initial < int(subsequence): | |
| # генератор и xrange | |
| sequence = sequence_storage + ''.join([str(num) for num in range(initial, initial+step)]) | |
| initial += step | |
| try: | |
| index_in_current_sequence = sequence.index(subsequence) | |
| return index_in_current_sequence + index_storage + 1 | |
| except ValueError: # можно все вот это поведение вынести в отдельную функцию | |
| try: | |
| right_index = sequence.rindex(subsequence[0]) | |
| tail = sequence[right_index:] | |
| if tail == subsequence[:len(tail)]: | |
| sequence_storage = tail | |
| index_storage += len(sequence) - len(tail) | |
| else: | |
| sequence_storage = '' | |
| index_storage += len(sequence) | |
| except ValueError: # про это уже написал | |
| sequence_storage = '' | |
| index_storage += len(sequence) | |
| # # мне кажеться это тоже самое: | |
| # sequence_storage = '' | |
| # index_storage += len(sequence) | |
| # try: | |
| # right_index = sequence.rindex(subsequence[0]) | |
| # except ValueError: | |
| # continue | |
| # else: | |
| # tail = sequence[right_index:] | |
| # if tail == subsequence[:len(tail)]: | |
| # sequence_storage = tail | |
| # index_storage -= len(tail) | |
| if __name__ == '__main__': | |
| print hh_2_smart('041') | |
| print hh_2_brute_force('041') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment