Advertisement
GeorgiLukanov87

Python Advanced Exam - 24 October 2020

Sep 12th, 2022 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. # Python Advanced Exam - 24 October 2020
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/2551#0
  4.  
  5. # 01. Scheduling
  6. # 02. Checkmate
  7. # 03. List Pureness
  8.  
  9. ---------------------------------------------------------------------------------------------------
  10.  
  11. # 01. Scheduling
  12.  
  13. # 1 - No deque() !
  14.  
  15. jobs = list(map(int, input().split(', ')))
  16. searched_index = int(input())
  17.  
  18. my_dict = {}
  19. for index, el in enumerate(jobs):
  20.     my_dict[index] = el
  21.  
  22. sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])
  23. total_cycles = 0
  24. for index, num in sorted_dict:
  25.     total_cycles += num
  26.     if index == searched_index:
  27.         break
  28.  
  29. print(total_cycles)
  30.  
  31. ---------------------------------------------------------------------------------------------------
  32. # 2 - Using deque() !
  33.  
  34. from collections import deque
  35.  
  36. jobs = list(map(int, input().split(', ')))
  37. searched_index = int(input())
  38.  
  39. my_dict = {}
  40. for index, el in enumerate(jobs):
  41.     my_dict[index] = el
  42.  
  43. all_jobs = deque(sorted(my_dict.items(), key=lambda x: x[1]))
  44. total_cycles = 0
  45.  
  46. while all_jobs:
  47.     first_index = all_jobs[0][0]
  48.     first_cycle = all_jobs[0][1]
  49.     total_cycles += first_cycle
  50.     all_jobs.popleft()
  51.     if first_index == searched_index:
  52.         print(total_cycles)
  53.         break
  54.  
  55. ---------------------------------------------------------------------------------------------------
  56.  
  57. # 02. Checkmate
  58. # NEW VERSION !
  59.  
  60. def is_inside_func(current_row, current_col):
  61.     return 0 <= current_row < SIZE and 0 <= current_col < SIZE
  62.  
  63.  
  64. # All possible moves(indexes) of the QUEEN in one LIST = UP DOWN LEFT RIGHT and all diagonals !
  65. #          R        L        U        D       RD       RU       LU       LD
  66. moves = [(0, 1), (0, -1), (-1, 0), (+1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
  67.  
  68.  
  69. def queens_moves_func(r, c):
  70.     possible_attack = []
  71.     for move in moves:
  72.         row_col = (r + move[0], c + move[1])
  73.         row, col = row_col
  74.         while is_inside_func(row, col):
  75.             if matrix[row][col] == 'Q':
  76.                 break
  77.             elif matrix[row][col] == 'K':
  78.                 possible_attack.append((row, col))
  79.                 break
  80.             row += move[0]
  81.             col += move[1]
  82.  
  83.     return possible_attack
  84.  
  85.  
  86. SIZE = 8
  87. matrix = []
  88. queens = []
  89.  
  90. for row_index in range(SIZE):
  91.     matrix.append(input().split())
  92.     for col_index in range(SIZE):
  93.         if matrix[row_index][col_index] == 'Q':
  94.             queens.append((row_index, col_index))
  95.  
  96. attacking_queens = []
  97. for queen in queens:
  98.     q_row = queen[0]
  99.     q_col = queen[1]
  100.     result = queens_moves_func(q_row, q_col)
  101.     if result:
  102.         attacking_queens.append(queen)
  103.  
  104. if attacking_queens:
  105.     for el in attacking_queens:
  106.         print([*el])
  107. else:
  108.     print('The king is safe!')
  109.  
  110.  
  111. ===================================================================================================
  112.  
  113. # 02. Checkmate
  114. # OLD VERSION !
  115.  
  116. def is_inside_func(current_row, current_col, size=8):
  117.     return 0 <= current_row < size and 0 <= current_col < size
  118.  
  119.  
  120. def queens_moves_func(row, col, matrix):
  121.     position = 1
  122.     possible_attack = []
  123.     # --------------------------------------------------------------------------
  124.     while is_inside_func(row, col + position):  # RIGHT
  125.         if matrix[row][col + position] == 'Q':
  126.             break
  127.         elif matrix[row][col + position] == 'K':
  128.             possible_attack.append((row, col + position))
  129.             break
  130.         position += 1
  131.  
  132.     position = 1
  133.     while is_inside_func(row, col - position):  # LEFT
  134.         if matrix[row][col - position] == 'Q':
  135.             break
  136.         elif matrix[row][col - position] == 'K':
  137.             possible_attack.append((row, col - position))
  138.             break
  139.         position += 1
  140.  
  141.     position = 1
  142.     while is_inside_func(row - position, col):  # UP
  143.         if matrix[row - position][col] == 'Q':
  144.             break
  145.         elif matrix[row - position][col] == 'K':
  146.             possible_attack.append((row - position, col))
  147.             break
  148.         position += 1
  149.  
  150.     position = 1
  151.     while is_inside_func(row + position, col):  # DOWN
  152.         if matrix[row + position][col] == 'Q':
  153.             break
  154.         elif matrix[row + position][col] == 'K':
  155.             possible_attack.append((row + position, col))
  156.             break
  157.         position += 1
  158.     # --------------------------------------------------------------------------
  159.     # DIAGONALS!
  160.     position = 1
  161.     while is_inside_func(row + position, col + position):  # RIGHT DOWN DIAGONAL
  162.         if matrix[row + position][col + position] == 'Q':
  163.             break
  164.         elif matrix[row + position][col + position] == 'K':
  165.             possible_attack.append((row + position, col + position))
  166.             break
  167.         position += 1
  168.  
  169.     position = 1
  170.     while is_inside_func(row - position, col + position):  # RIGHT UP DIAGONAL
  171.         if matrix[row - position][col + position] == 'Q':
  172.             break
  173.         elif matrix[row - position][col + position] == 'K':
  174.             possible_attack.append((row - position, col + position))
  175.             break
  176.         position += 1
  177.  
  178.     position = 1
  179.     while is_inside_func(row - position, col - position):  # LEFT UP DIAGONAL
  180.         if matrix[row - position][col - position] == 'Q':
  181.             break
  182.         elif matrix[row - position][col - position] == 'K':
  183.             possible_attack.append((row - position, col - position))
  184.             break
  185.         position += 1
  186.  
  187.     position = 1
  188.     while is_inside_func(row + position, col - position):  # LEFT DOWN DIAGONAL
  189.         if matrix[row + position][col - position] == 'Q':
  190.             break
  191.         elif matrix[row + position][col - position] == 'K':
  192.             possible_attack.append((row + position, col - position))
  193.             break
  194.         position += 1
  195.  
  196.     return possible_attack
  197.  
  198.  
  199. matrix = []
  200. queens = []
  201. king_row = 0
  202. king_col = 0
  203. for row_index in range(8):
  204.     matrix.append(input().split())
  205.     for col_index in range(8):
  206.         if matrix[row_index][col_index] == 'K':
  207.             king_row = row_index
  208.             king_col = col_index
  209.  
  210.         if matrix[row_index][col_index] == 'Q':
  211.             queens.append((row_index, col_index))
  212.  
  213. attacking_queens = []
  214. for current_queen in queens:
  215.     q_row = current_queen[0]
  216.     q_col = current_queen[1]
  217.     current_result = queens_moves_func(q_row, q_col, matrix)
  218.     if current_result:
  219.         attacking_queens.append(current_queen)
  220.  
  221. if attacking_queens:
  222.     for el in attacking_queens:
  223.         print([*el])
  224. else:
  225.     print('The king is safe!')
  226.  
  227.    
  228. ---------------------------------------------------------------------------------------------------
  229.  
  230. # 03. List Pureness
  231.  
  232.  
  233. from collections import deque
  234.  
  235.  
  236. def best_list_pureness(nums_list, K):
  237.     best_pureness = float('-inf')
  238.     best_rotate = 0
  239.     rotate_this = deque(nums_list)
  240.     for current_rotation in range(K + 1):
  241.         pureness = 0
  242.         for index, el in enumerate(rotate_this):
  243.             pureness += el * index
  244.  
  245.         if pureness > best_pureness:
  246.             best_pureness = pureness
  247.             best_rotate = current_rotation
  248.  
  249.         rotate_this.rotate()
  250.  
  251.     return f'Best pureness {best_pureness} after {best_rotate} rotations'
  252.  
  253. ---------------------------------------------------------------------------------------------------
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement