Skip to content

Instantly share code, notes, and snippets.

@belyaev-pa
Created September 24, 2018 09:22
Show Gist options
  • Save belyaev-pa/3bb817bc32d210fc518cfb23dfc1a8b2 to your computer and use it in GitHub Desktop.
Save belyaev-pa/3bb817bc32d210fc518cfb23dfc1a8b2 to your computer and use it in GitHub Desktop.
import itertools
def get_data(file_name):
"""generator to yield file row by row
for not upload it to memory"""
with open(file_name, encoding='Windows-1251') as f:
rows = f.readlines()
for row in rows:
yield (row)
def minimum_brides(input_list):
"""function for analysing input list
with new year chaotic algorithm"""
min_brides = 0
for i, obj in reversed(list(enumerate(input_list, 1))):
if (int(obj) - i) > 2:
return 'Too chaotic'
for j in range(max(0, int(obj)-2), i):
if int(input_list[j]) > int(obj):
min_brides += 1
return min_brides
if __name__ == "__main__":
file_name = 'test.txt'
file_rows = get_data(file_name)
first = list(itertools.islice(file_rows, 1))[0]
experiment_count = 0
for i, row in enumerate(file_rows, 1):
if i == 1:
continue
if not i % 2:
print('{}: {}'.format(row, minimum_brides(row.split())))
experiment_count += 1
if experiment_count == int(first):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment