Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def generate_file(file_name, length = 5):
- generated_nums = [length] + [randint(-73, 73) for _ in range(length)]
- with open(file_name, "w") as file:
- file.write('\n'.join(map(str, generated_nums)))
- def copy_file(file_name1, file_name2):
- f1 = open(file_name1)
- f2 = open(file_name2, "w")
- f2.write(f1.read())
- f1.close()
- f2.close()
- def count1(file_name):
- f = open(file_name)
- n = int(f.readline())
- s = [[0, 0]]
- mx = 0
- l = 0
- for i in range(n):
- x = int(f.readline())
- cmb = [[a + x, b + 1] for a, b in s] + [[x, 1]]
- s = {x[0] % 73: x for x in sorted(cmb, key=lambda x: x[0])}.values()
- for a, b in s:
- if a % 73 == 0:
- if a > mx:
- mx = x
- l = b
- if a == mx and b < l:
- l = b
- return l
- def count2(file_name):
- f = open(file_name)
- n = int(f.readline())
- nums = list(map(int, f.readlines()))
- max_sum = 0
- min_len = float('inf')
- for i in range(len(nums)):
- for j in range(i, len(nums) + 1):
- curr_sum = sum(nums[i:j])
- if curr_sum % 73 == 0:
- if curr_sum > max_sum:
- max_sum = curr_sum
- min_len = len(nums[i:j])
- elif curr_sum == max_sum and len(nums[i:j]) < min_len:
- min_len = len(nums[i:j])
- return min_len
- passed_test = 0
- while True:
- generate_file("./27.txt")
- ans1 = count1("./27.txt")
- ans2 = count2("./27.txt")
- if ans1 == ans2:
- passed_test += 1
- else:
- print("Error [{}, {}]".format(ans1, ans2))
- copy_file("./27.txt","./Error_{}.txt".format(passed_test))
- if passed_test == 10_000:
- print("All tests passed!")
- break
- if passed_test % 1_000 == 0:
- print("Passed {} tests".format(passed_test))
Add Comment
Please, Sign In to add comment