PorisulkiP

Номер 6

Jul 10th, 2022
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. from random import *
  2.  
  3. def matrixGeneration():
  4.     ''' Генерируется матрица согласно условию '''
  5.     global answer, M, N
  6.     N = randint(0, 1000)
  7.     M = randint(-1000000, 1000000)
  8.    
  9.     for x in range(N):
  10.         matrix.append([randint(-1000000, 1000000) for i in range(N)])
  11.      
  12.     # матрица записывается в файл
  13.     f = open("in-6.txt", "w")
  14.     f.write(str(matrix))
  15.     f.close()
  16.  
  17. def openFileAndReadData():
  18.     global answer
  19.     data = []
  20.     with open("in-6.txt") as f:
  21.       for line in f:
  22.           data.append([int(x) for x in line.replace("[", "").replace("]", "").replace(",", "").split()])
  23.     f.close()
  24.     skip = 0
  25.     per = 0
  26.     tmp = 0
  27.     for array in data:
  28.         for x in array:
  29.           try: x = int(x)
  30.           except ValueError: continue # Если символ не цифра, то пропускается
  31.           # print ("x =", x)
  32.           per += 1
  33.           if not per%2 and not tmp:
  34.               per = 1
  35.               tmp = skip
  36.               skip += 1
  37.               break
  38.           if x > M and not tmp:
  39.               answer += 1;
  40.           tmp -= 1
  41.          
  42.     print (f'N: {N}\nM: {M}\nanswer: {answer}')
  43.  
  44. matrix = []
  45. answer = 0
  46.  
  47. matrixGeneration() # Создаётся матрица
  48. openFileAndReadData() # Работа с матрицей
Advertisement
Add Comment
Please, Sign In to add comment