AlexG2230954

27. Тестирование

Jun 24th, 2021 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. from random import randint
  2.  
  3. def generate_file(file_name, length = 5):
  4.     generated_nums = [length] + [randint(-73, 73) for _ in range(length)]
  5.    
  6.     with open(file_name, "w") as file:
  7.         file.write('\n'.join(map(str, generated_nums)))
  8.  
  9.  
  10. def copy_file(file_name1, file_name2):
  11.     f1 = open(file_name1)
  12.     f2 = open(file_name2, "w")
  13.     f2.write(f1.read())
  14.     f1.close()
  15.     f2.close()
  16.  
  17. def count1(file_name):
  18.     f = open(file_name)
  19.     n = int(f.readline())
  20.  
  21.     s = [[0, 0]]
  22.     mx = 0
  23.     l = 0
  24.  
  25.     for i in range(n):
  26.         x = int(f.readline())
  27.         cmb = [[a + x, b + 1] for a, b in s] + [[x, 1]]
  28.         s = {x[0] % 73: x for x in sorted(cmb, key=lambda x: x[0])}.values()
  29.        
  30.         for a, b in s:
  31.            
  32.             if a % 73 == 0:
  33.                 if a > mx:
  34.                     mx = x
  35.                     l = b
  36.                
  37.                 if a == mx and b < l:
  38.                     l = b
  39.     return l
  40.  
  41. def count2(file_name):
  42.     f = open(file_name)
  43.     n = int(f.readline())
  44.     nums = list(map(int, f.readlines()))
  45.     max_sum = 0
  46.     min_len = float('inf')
  47.    
  48.     for i in range(len(nums)):
  49.        
  50.         for j in range(i, len(nums) + 1):
  51.             curr_sum = sum(nums[i:j])
  52.            
  53.             if curr_sum % 73 == 0:
  54.                 if curr_sum > max_sum:
  55.                     max_sum = curr_sum
  56.                     min_len = len(nums[i:j])
  57.                
  58.                 elif curr_sum == max_sum and len(nums[i:j]) < min_len:
  59.                     min_len = len(nums[i:j])
  60.    
  61.     return min_len
  62.  
  63. passed_test = 0
  64.  
  65. while True:
  66.     generate_file("./27.txt")
  67.  
  68.     ans1 = count1("./27.txt")
  69.     ans2 = count2("./27.txt")
  70.    
  71.     if ans1 == ans2:
  72.         passed_test += 1
  73.    
  74.     else:
  75.         print("Error [{}, {}]".format(ans1, ans2))
  76.         copy_file("./27.txt","./Error_{}.txt".format(passed_test))
  77.    
  78.     if passed_test == 10_000:
  79.         print("All tests passed!")
  80.         break
  81.    
  82.     if passed_test % 1_000 == 0:
  83.         print("Passed {} tests".format(passed_test))
  84.  
Add Comment
Please, Sign In to add comment