Advertisement
ALEXANDAR_GEORGIEV

Exercesis_Multidimensional_list_2

May 19th, 2023
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | Source Code | 0 0
  1. # Exercesis_Multidimensional_list_2
  2. # Flatten_list
  3. line = input().split("|")
  4.  
  5. sub_lists = []
  6.  
  7. for sub_string in line[::-1]:
  8.     sub_lists.extend(sub_string.split())
  9.  
  10. print(*sub_lists)
  11.  
  12. # Knight_game
  13. size = int(input())
  14. matrix = [list(input()) for _ in range(size)]
  15.  
  16. positions = (
  17.     (-2, -1),   # up 2, left 1
  18.     (-2, 1),    # up 2, right 1
  19.     (-1, -2), # up 1, left 2
  20.     (-1, 2),
  21.     (2, 1),
  22.     (2, -1),
  23.     (1, 2),
  24.     (1, -2)
  25. )
  26.  
  27. removed_knights = 0
  28.  
  29. while True:
  30.     max_attacks = 0
  31.     knight_with_most_attack_pos = []
  32.  
  33.     for row in range(size):
  34.         for col in range(size):
  35.             if matrix[row][col] == "K":
  36.                 attacks = 0
  37.  
  38.                 for pos in positions:
  39.                     pos_row = row + pos[0]
  40.                     pos_col = col + pos[1]
  41.  
  42.                     if 0 <= pos_row < size and 0 <= pos_col < size:
  43.                         if matrix[pos_row][pos_col] == "K":
  44.                             attacks += 1
  45.  
  46.                 if attacks > max_attacks:
  47.                     knight_with_most_attack_pos = [row, col]
  48.                     max_attacks = attacks
  49.  
  50.     if knight_with_most_attack_pos:
  51.         matrix[knight_with_most_attack_pos[0]][knight_with_most_attack_pos[1]] = "0"    # Махаме коня
  52.         removed_knights += 1
  53.     else:
  54.         break
  55.  
  56. print(removed_knights)
  57.  
  58. # Matrix_modification
  59. size = int(input())
  60. matrix = [[int(x) for x in input().split()] for _ in range(size)]
  61.  
  62. command = input().split()
  63.  
  64. while command[0] != "END":
  65.     type_command, row, col, value = command[0], int(command[1]), int(command[2]), int(command[3])
  66.  
  67.     if not (0 <= row < size and 0 <= col < size):
  68.         print("Invalid coordinates")
  69.     elif type_command == "Add":
  70.         matrix[row][col] += value
  71.     elif type_command == "Subtrack":
  72.         matrix[row][col] -= value
  73.  
  74.     command = input().split()
  75.  
  76. [print(*row) for row in matrix]
  77.  
  78.  
  79. # Presents_delivery
  80. def eat_cookie(presents_left, nice_kids):
  81.     for coordinates in directions.values():
  82.         r = santa_pos[0] + coordinates[0]
  83.         c = santa_pos[1] + coordinates[1]
  84.  
  85.         if neighborhood[r][c].isalpha():
  86.             if neighborhood[r][c] == "V":
  87.                 nice_kids += 1
  88.  
  89.             neighborhood[r][c] = '-'
  90.             presents_left -= 1
  91.  
  92.         if not presents_left:
  93.             break
  94.  
  95.     return presents_left, nice_kids
  96.  
  97.  
  98.  
  99. presents = int(input())
  100.  
  101. size = int(input())
  102.  
  103. neighborhood = []
  104. santa_pos = []
  105.  
  106. total_nice_kids = 0
  107. nice_kids_visited = 0
  108.  
  109. directions = {
  110.     'up': (-1, 0),
  111.     'down': (1, 0),
  112.     'left': (0, -1),
  113.     'right': (0, 1)
  114. }
  115.  
  116. for row in range(size):
  117.     line = input().split()
  118.     neighborhood.append(line)
  119.  
  120.     if "S" in line:
  121.         santa_pos = [row, line.index("S")]
  122.         neighborhood[row][santa_pos[1]] = '-'
  123.  
  124.     total_nice_kids += line.count("V")
  125.  
  126. command = input()
  127.  
  128. while command != " Christmas morning":
  129.  
  130.     santa_pos = [
  131.         santa_pos[0] + directions[command][0],
  132.         santa_pos[1] + directions[command][1],
  133.     ]
  134.  
  135.     house = neighborhood[santa_pos[0]][santa_pos[1]]
  136.  
  137.     if house == "V":
  138.         nice_kids_visited += 1
  139.         presents -= 1
  140.     elif house == "C":
  141.         presents, nice_kids_visited = eat_cookie(presents, nice_kids_visited)
  142.  
  143.     neighborhood[santa_pos[0]][santa_pos[1]] = '-'
  144.  
  145.     if not presents or nice_kids_visited == total_nice_kids:
  146.         command = input()
  147.  
  148. neighborhood[santa_pos[0]][santa_pos[1]] = "S"
  149.  
  150. if not presents and nice_kids_visited < total_nice_kids:
  151.     print("Santa ran out of presents!")
  152.  
  153. print(*[" ".join(line) for line in neighborhood], sep="\n")
  154.  
  155. if total_nice_kids == nice_kids_visited:
  156.     print(f"Good job, Santa! {total_nice_kids} happy nice kid/s.")
  157. else:
  158.     print(f"No presents for {kkkkkkk} nice kid/s.")
  159.  
  160. # Range_day
  161. def move(directoin: str, steps: int):
  162.     r = my_position[0] + (directions[directoin][0] * steps)
  163.     c = my_position[1] + (directions[directoin][1] * steps)
  164.  
  165.     if not (0 <= r < SIZE and 0 <= c < SIZE):
  166.         return my_position
  167.     if field[r][c] == 'x':
  168.         return my_position
  169.  
  170.     return [r, c]
  171.  
  172.  
  173. def shoot(direction):
  174.     r = my_position[0] + directions[direction][0]
  175.     c = my_position[1] + directions[direction][1]
  176.  
  177.     while 0 <= r < SIZE and 0 <= c < SIZE:
  178.         if field[r][c] == 'x':
  179.             field[r][c] = '.'
  180.             return [r, c]
  181.         r += directions[direction][0]
  182.         c += directions[direction][1]
  183.  
  184.  
  185.  
  186.  
  187. SIZE = 5
  188.  
  189. field = []
  190.  
  191. targets = 0
  192. targets_hit = 0
  193. target_hit_positions = []
  194.  
  195. my_position = []
  196.  
  197. directions = {
  198.     'up': (-1, 0),
  199.     'down': (1, 0),
  200.     'left': (0, -1),
  201.     'right': (0, 1)
  202. }
  203.  
  204. for row in range(SIZE):
  205.     field.append((input().split()))
  206.  
  207.     if 'A' in field[row]:
  208.         my_position = [row, field[row].index('A')]
  209.  
  210.     targets += field[row].count('x')
  211.  
  212. for _ in range(int(input())):
  213.     command_info = input().split()
  214.  
  215.     if command_info[0] == 'move':
  216.         my_position = move(command_info[1], int(command_info[2]))
  217.  
  218.     elif command_info[0] == 'shoot':
  219.         targets_down_pos = shoot(command_info[1])
  220.  
  221.         if targets_down_pos:
  222.             target_hit_positions.append(targets_down_pos)
  223.             targets_hit += 1
  224.  
  225.         if targets_hit == targets:
  226.             print(f"Training completed! All {targets} targets hit.")
  227.  
  228. else:
  229.     print(f"Training not completed! { targets - targets_hit} targets left.")
  230.  
  231. print(*target_hit_positions, sep="\n")
  232.  
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement