Advertisement
Monstera

zadanie-ciągi # 1.1

Jan 25th, 2023 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. # 1.1
  2.  
  3.  
  4. def if_arithmetic(array):
  5.     difference = int(array[1]) - int(array[0])
  6.     for index in range(len(array) - 1):
  7.         if not (int(array[index + 1]) - int(array[index]) == difference):
  8.             return False
  9.     return True
  10.  
  11.  
  12. def count_difference(array):
  13.     return int(array[1]) - int(array[0])
  14.  
  15.  
  16. how_many_arithmetic = 0
  17. differences = []
  18.  
  19. with open("ciagi.txt") as file, open("wynik1.txt", "w") as result:
  20.     lines = file.readlines()
  21.  
  22.     for line in lines:
  23.         list_line = line.strip('\n').split(' ')
  24.  
  25.         if len(list_line) < 3:
  26.             continue
  27.         if if_arithmetic(list_line):
  28.             how_many_arithmetic = how_many_arithmetic + 1
  29.             differences.append(count_difference(list_line))
  30.  
  31.     result.write(f"In this file we have {how_many_arithmetic} arithmetic seq.\n")
  32.     result.write(f"The max of diff: {max(differences)}")
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement