Advertisement
GeorgiLukanov87

Python Advanced Retake Exam - 19 August 2020

Sep 13th, 2022 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1. # Python Advanced Retake Exam - 19 August 2020
  2. #
  3. # https://judge.softuni.org/Contests/Practice/Index/2463#0
  4. #
  5. # 01. Taxi Express
  6. # 02. Minesweeper Generator
  7. # 03. Numbers Search
  8.  
  9.  
  10. ---------------------------------------------------------------------------------------------
  11.  
  12. # 01. Taxi Express
  13.  
  14.  
  15. from collections import deque
  16.  
  17. customers = deque([int(x) for x in input().split(', ')])
  18. taxis = deque([int(x) for x in input().split(', ')])
  19. total_time = sum(customers)
  20.  
  21. while customers and taxis:
  22.     first_customer = customers[0]
  23.     last_taxi = taxis[-1]
  24.     if last_taxi >= first_customer:
  25.         customers.popleft()
  26.     taxis.pop()
  27.  
  28. if not customers:
  29.     print(f'All customers were driven to their destinations')
  30.     print(f'Total time: {total_time} minutes')
  31. else:
  32.     print('Not all customers were driven to their destinations')
  33.     if customers:
  34.         print(f'Customers left: {", ".join(str(x) for x in customers)}')
  35.  
  36.  
  37.  
  38.    
  39. ---------------------------------------------------------------------------------------------
  40. # 02. Minesweeper Generator
  41. # New VERSION !
  42.  
  43. def is_inside(r, c):
  44.     return 0 <= r < SIZE and 0 <= c < SIZE
  45.  
  46.  
  47. def warning_calculate(bomb_indexes):
  48.     for b in around_bomb:
  49.         r, c = (bomb_indexes[0] + b[0], bomb_indexes[1] + b[1])
  50.         if is_inside(r, c):
  51.             if matrix[r][c] != '*':
  52.                 matrix[r][c] += 1
  53.  
  54.  
  55. around_bomb = [(0, 1), (0, -1), (-1, 0), (+1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
  56. SIZE = int(input())
  57. BOMBS = int(input())
  58. matrix = []
  59. for _ in range(SIZE):
  60.     line = SIZE * '0'
  61.     matrix.append([int(x) for x in list(line)])
  62. bombs_pos = []
  63. for _ in range(BOMBS):
  64.     bomb_row_col = (input().strip('()').split(', '))
  65.     row = int(bomb_row_col[0])
  66.     col = int(bomb_row_col[1])
  67.     matrix[row][col] = '*'
  68.     bombs_pos.append((row, col))
  69.  
  70. for bomb in bombs_pos:
  71.     warning_calculate(bomb)
  72.  
  73. for el in matrix:
  74.     print(*el)
  75.  
  76. ============================================================================================
  77. # 02. Minesweeper Generator
  78. # OLD VERSION !
  79.  
  80. def is_inside_func(current_row, current_col):
  81.     return 0 <= current_row < size and 0 <= current_col < size
  82.  
  83.  
  84. def calculate_bomb_warnings_func(row, col):
  85.     warnings_count = 0
  86.  
  87.     if is_inside_func(row, col + 1):  # RIGHT
  88.         if matrix[row][col + 1] == '*':
  89.             warnings_count += 1
  90.            
  91.     if is_inside_func(row, col - 1):  # LEFT
  92.         if matrix[row][col - 1] == '*':
  93.             warnings_count += 1
  94.  
  95.     if is_inside_func(row + 1, col):  # DOWN
  96.         if matrix[row + 1][col] == '*':
  97.             warnings_count += 1
  98.  
  99.     if is_inside_func(row - 1, col):  # UP
  100.         if matrix[row - 1][col] == '*':
  101.             warnings_count += 1
  102.  
  103.     if is_inside_func(row - 1, col + 1):  # TOP RIGHT
  104.         if matrix[row - 1][col + 1] == '*':
  105.             warnings_count += 1
  106.  
  107.     if is_inside_func(row - 1, col - 1):  # TOP LEFT
  108.         if matrix[row - 1][col - 1] == '*':
  109.             warnings_count += 1
  110.  
  111.     if is_inside_func(row + 1, col - 1):  # BOTTOM LEFT
  112.         if matrix[row + 1][col - 1] == '*':
  113.             warnings_count += 1
  114.  
  115.     if is_inside_func(row + 1, col + 1):  # BOTTOM RIGHT
  116.         if matrix[row + 1][col + 1] == '*':
  117.             warnings_count += 1
  118.  
  119.     return warnings_count
  120.  
  121.  
  122. size = int(input())
  123. n_bombs = int(input())
  124. bombs_indexes = []
  125.  
  126. matrix = []
  127. for _ in range(n_bombs):  # Extracting bomb indexes
  128.     current_bomb = eval(input())
  129.     bomb_index1 = int(current_bomb[0])
  130.     bomb_index2 = int(current_bomb[1])
  131.     bombs_indexes.append((bomb_index1, bomb_index2))
  132.  
  133. for row in range(size):  # Creating the matrix
  134.     matrix.append([])
  135.     for cow in range(size):
  136.         matrix[row].append(0)
  137.  
  138. for current_index_bomb in bombs_indexes:  # Placing the bombs
  139.     index1 = current_index_bomb[0]
  140.     index2 = current_index_bomb[1]
  141.     if is_inside_func(index1, index2):
  142.         matrix[index1][index2] = "*"
  143.  
  144. for row_index in range(size):
  145.     for col_index in range(size):
  146.         if (row_index, col_index) not in bombs_indexes:
  147.             warnings = calculate_bomb_warnings_func(row_index, col_index)
  148.             matrix[row_index][col_index] = warnings
  149.  
  150. for el in matrix:
  151.     print(*el)
  152.  
  153. ---------------------------------------------------------------------------------------------
  154. # 03. Numbers Search
  155.  
  156. def numbers_searching(*nums):
  157.     sorted_nums = sorted(nums)
  158.     missing = []
  159.     duplicated = []
  160.     for num in range(sorted_nums[0], sorted_nums[-1] + 1):
  161.         if num not in sorted_nums:
  162.             missing.append(num)
  163.         else:
  164.             x = sorted_nums.count(num)
  165.             if x > 1 and num not in duplicated:
  166.                 duplicated.append(num)
  167.  
  168.     missing.append(duplicated)
  169.     return missing
  170.  
  171. ---------------------------------------------------------------------------------------------
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement