Advertisement
ALEXANDAR_GEORGIEV

Exercesis_Multidimensional_list

May 18th, 2023
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | Source Code | 0 0
  1. # Exercesis_Multidimensional_list
  2. # Diagonal_differences
  3. num = int(input())
  4.  
  5. matrix = [[int(n) for n in input().split()] for row in range(num)]
  6.  
  7. primary_sum = 0
  8. secondary_sum = 0
  9.  
  10. for i in range(num):
  11.     primary_sum += matrix[i][i]
  12.     secondary_sum += matrix[i][num - i - 1]
  13.  
  14. print(abs(primary_sum - secondary_sum))
  15.  
  16. # Diagonals
  17. n = int(input())
  18.  
  19. matrix = [[int(x) for x in input().split(", ")] for _ in range(n)]
  20.  
  21. primary = [matrix[r][r] for r in range(n)]  # взимаме числата от диагонала
  22. secondary = [matrix[r][n - r - 1] for r in range(n)]  # взимаме числата от другия диагонала
  23.  
  24. print(f"Primary diagonal: {', '.join(str(x) for x in primary)}. Sum: {sum(primary)}")
  25. print(f"Secondary diagonal: {', '.join(str(x) for x in secondary)}. Sum: {sum(secondary)}")
  26.  
  27. # Matrix_of_palindromes
  28. rows, cols = [int(x) for x in input().split()]
  29.  
  30. start = ord('a')
  31.  
  32. for row in range(start, start + rows):
  33.     for col in range(start, start + cols):
  34.         print(f"{chr(row)}{chr(row + col - start)}{chr(row)}", end=" ")
  35.  
  36.     print() # За да извадим na нов ред
  37.  
  38. # matrix_shiffling
  39. def check_valid_indices(indices):
  40.     return {indices[0], indices[2]}.issubset(valid_rows) and {indices[1], indices[3]}.issubset(valid_cols)
  41.  
  42.  
  43. def swap_command(command: str, indices: list):
  44.     if check_valid_indices(indices) and command == 'swap' and len(indices) == 4:
  45.         row1, col1, row2, col2 = indices
  46.  
  47.         matrix[row1][col1], matrix[row2][col2] = matrix[row2][col2], matrix[row1][col1] # Разменяме местата
  48.  
  49.         print(*[' '.join(r) for r in matrix], sep="\n")
  50.     else:
  51.         print("Invalid input!")
  52.  
  53.  
  54. rows, cols = [int(x) for x in input().split()]
  55. matrix = [input().split() for _ in range(rows)]
  56.  
  57. valid_rows = range(rows)
  58. valid_cols = range(cols)
  59.  
  60.  
  61.  
  62. while True:
  63.     command_type, *info = [int(x) if x.isdigit() else x for x in input().split()]
  64.  
  65.     if command_type == "END":
  66.         break
  67.  
  68.     swap_command(command_type, inf
  69.  
  70.  
  71. # Maximal_sum
  72. rows, cols = [int(x) for x in input().split()]
  73.  
  74. matrix = [[int(x) for x in input().split()] for row in range(rows)]
  75.  
  76. max_sum = float("-inf")
  77. biggest_matrix = []
  78.  
  79. for row in range(rows - 2):
  80.     for col in range(cols - 2):
  81.         first_row = matrix[row][col:col + 3]
  82.         second_row = matrix[row + 1][col:col + 3]
  83.         third_row = matrix[row + 2][col:col + 3]
  84.  
  85.         current_sum = sum(first_row) + sum(second_row) + sum(third_row)
  86.  
  87.         if current_sum > max_sum:
  88.             max_sum = current_sum
  89.             biggest_matrix = [first_row, second_row, third_row]
  90.  
  91. print(f"Sum = {max_sum}")
  92. [print(*row) for row in biggest_matrix]
  93.  
  94. # radioactive_mutant_vampire_bunnies
  95. def find_player_position():
  96.     for row in range(rows):
  97.         if "P" in matrix[row]:
  98.             return row, matrix[row].index("P")
  99.  
  100.  
  101. def check_valid_index(row, col, player=False):
  102.     global wins # За да можем да променяме wins във функцията
  103.  
  104.     if 0 <= row < rows and 0 <= col < cols:
  105.         return True
  106.     if player:
  107.         wins = True
  108.  
  109.  
  110. def bunnies_positions():
  111.     positions = []
  112.  
  113.     for row in range(rows):
  114.         for col in range(cols):
  115.             if matrix[row][col] == "B":
  116.                 positions.append([row, col])
  117.     return positions
  118.  
  119.  
  120. def bunnies_move(bunnies_pos):
  121.     for row, col in (bunnies_pos):
  122.         for bunnie_move in direction.values():
  123.             new_row, new_col = row + bunnie_move[0], col + bunnie_move[1]
  124.  
  125.             if check_valid_index(new_row, new_col):
  126.                 matrix[new_row][new_col] = "B"
  127.  
  128.  
  129. def show_resilts(status="won"):
  130.     [print(*row, sep="") for row in matrix]
  131.     print(f"{status}: {player_row} {player_col}")
  132.  
  133.     raise SystemExit
  134.  
  135.  
  136. def check_player_alive(row, col):
  137.     if matrix[row][col] == "B":
  138.         show_resilts("dead")
  139.  
  140.  
  141. rows, cols = [int(x) for x in input().split()]
  142. matrix = [list(input()) for _ in range(rows)]
  143.  
  144. commands = input()
  145.  
  146. wins = False
  147. direction = {
  148.     "U": (-1, 0),    # Посоката в която се движим
  149.     "D": (1, 0),
  150.     "L": (0, -1),
  151.     "R": (0, 1)
  152. }
  153.  
  154. player_row, player_col = find_player_position()
  155.  
  156. matrix[player_row][player_col] = '.'
  157.  
  158. for command in commands:
  159.     player_movement_row, player_movement_col = player_row + direction[command][0], player_col + direction[command][1]
  160.  
  161.     if check_valid_index(player_movement_row, player_movement_col, True):
  162.         player_row, player_col = player_movement_row, player_movement_col
  163.  
  164.     bunnies_move(bunnies_positions())
  165.  
  166.     if wins:
  167.         show_resilts()
  168.  
  169.     check_player_alive(player_row, player_col)
  170.  
  171.  
  172. # Snake_moves
  173. from collections import deque
  174.  
  175. rows, cols = [int(x) for x in input().split()]
  176. word = list(input()) # abc -> ["a", "b", "c"]
  177.  
  178. word_copy = deque(word)
  179.  
  180. for row in range(rows):
  181.     while len(word_copy) < cols:
  182.         word_copy.extend(word)
  183.  
  184.     if row % 2 == 0:
  185.         print(*[word_copy.popleft() for _ in range(cols)], sep="")
  186.     else:
  187.         print(*[word_copy.popleft() for _ in range(cols)][::-1], sep="")
  188.        
  189.  
  190. # Two_by_two_squares_in_matrix
  191. rows, cols = [int(x) for x in input().split()]
  192.  
  193. matrix = [input().split() for row in range(rows)]
  194.  
  195. equal_blocks = 0
  196.  
  197. for row in range(rows - 1):
  198.     for col in range(cols - 1):
  199.         symbol = matrix[row][col]
  200.  
  201.         if matrix[row][col + 1] == symbol and matrix[row + 1][col] == matrix[row + 1][col + 1] == symbol:
  202.             equal_blocks += 1
  203.  
  204. print(equal_blocks)
  205.  
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement