Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Advanced Exam - 24 October 2020
- # https://judge.softuni.org/Contests/Practice/Index/2551#0
- # 01. Scheduling
- # 02. Checkmate
- # 03. List Pureness
- ---------------------------------------------------------------------------------------------------
- # 01. Scheduling
- # 1 - No deque() !
- jobs = list(map(int, input().split(', ')))
- searched_index = int(input())
- my_dict = {}
- for index, el in enumerate(jobs):
- my_dict[index] = el
- sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])
- total_cycles = 0
- for index, num in sorted_dict:
- total_cycles += num
- if index == searched_index:
- break
- print(total_cycles)
- ---------------------------------------------------------------------------------------------------
- # 2 - Using deque() !
- from collections import deque
- jobs = list(map(int, input().split(', ')))
- searched_index = int(input())
- my_dict = {}
- for index, el in enumerate(jobs):
- my_dict[index] = el
- all_jobs = deque(sorted(my_dict.items(), key=lambda x: x[1]))
- total_cycles = 0
- while all_jobs:
- first_index = all_jobs[0][0]
- first_cycle = all_jobs[0][1]
- total_cycles += first_cycle
- all_jobs.popleft()
- if first_index == searched_index:
- print(total_cycles)
- break
- ---------------------------------------------------------------------------------------------------
- # 02. Checkmate
- # NEW VERSION !
- def is_inside_func(current_row, current_col):
- return 0 <= current_row < SIZE and 0 <= current_col < SIZE
- # All possible moves(indexes) of the QUEEN in one LIST = UP DOWN LEFT RIGHT and all diagonals !
- # R L U D RD RU LU LD
- moves = [(0, 1), (0, -1), (-1, 0), (+1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)]
- def queens_moves_func(r, c):
- possible_attack = []
- for move in moves:
- row_col = (r + move[0], c + move[1])
- row, col = row_col
- while is_inside_func(row, col):
- if matrix[row][col] == 'Q':
- break
- elif matrix[row][col] == 'K':
- possible_attack.append((row, col))
- break
- row += move[0]
- col += move[1]
- return possible_attack
- SIZE = 8
- matrix = []
- queens = []
- for row_index in range(SIZE):
- matrix.append(input().split())
- for col_index in range(SIZE):
- if matrix[row_index][col_index] == 'Q':
- queens.append((row_index, col_index))
- attacking_queens = []
- for queen in queens:
- q_row = queen[0]
- q_col = queen[1]
- result = queens_moves_func(q_row, q_col)
- if result:
- attacking_queens.append(queen)
- if attacking_queens:
- for el in attacking_queens:
- print([*el])
- else:
- print('The king is safe!')
- ===================================================================================================
- # 02. Checkmate
- # OLD VERSION !
- def is_inside_func(current_row, current_col, size=8):
- return 0 <= current_row < size and 0 <= current_col < size
- def queens_moves_func(row, col, matrix):
- position = 1
- possible_attack = []
- # --------------------------------------------------------------------------
- while is_inside_func(row, col + position): # RIGHT
- if matrix[row][col + position] == 'Q':
- break
- elif matrix[row][col + position] == 'K':
- possible_attack.append((row, col + position))
- break
- position += 1
- position = 1
- while is_inside_func(row, col - position): # LEFT
- if matrix[row][col - position] == 'Q':
- break
- elif matrix[row][col - position] == 'K':
- possible_attack.append((row, col - position))
- break
- position += 1
- position = 1
- while is_inside_func(row - position, col): # UP
- if matrix[row - position][col] == 'Q':
- break
- elif matrix[row - position][col] == 'K':
- possible_attack.append((row - position, col))
- break
- position += 1
- position = 1
- while is_inside_func(row + position, col): # DOWN
- if matrix[row + position][col] == 'Q':
- break
- elif matrix[row + position][col] == 'K':
- possible_attack.append((row + position, col))
- break
- position += 1
- # --------------------------------------------------------------------------
- # DIAGONALS!
- position = 1
- while is_inside_func(row + position, col + position): # RIGHT DOWN DIAGONAL
- if matrix[row + position][col + position] == 'Q':
- break
- elif matrix[row + position][col + position] == 'K':
- possible_attack.append((row + position, col + position))
- break
- position += 1
- position = 1
- while is_inside_func(row - position, col + position): # RIGHT UP DIAGONAL
- if matrix[row - position][col + position] == 'Q':
- break
- elif matrix[row - position][col + position] == 'K':
- possible_attack.append((row - position, col + position))
- break
- position += 1
- position = 1
- while is_inside_func(row - position, col - position): # LEFT UP DIAGONAL
- if matrix[row - position][col - position] == 'Q':
- break
- elif matrix[row - position][col - position] == 'K':
- possible_attack.append((row - position, col - position))
- break
- position += 1
- position = 1
- while is_inside_func(row + position, col - position): # LEFT DOWN DIAGONAL
- if matrix[row + position][col - position] == 'Q':
- break
- elif matrix[row + position][col - position] == 'K':
- possible_attack.append((row + position, col - position))
- break
- position += 1
- return possible_attack
- matrix = []
- queens = []
- king_row = 0
- king_col = 0
- for row_index in range(8):
- matrix.append(input().split())
- for col_index in range(8):
- if matrix[row_index][col_index] == 'K':
- king_row = row_index
- king_col = col_index
- if matrix[row_index][col_index] == 'Q':
- queens.append((row_index, col_index))
- attacking_queens = []
- for current_queen in queens:
- q_row = current_queen[0]
- q_col = current_queen[1]
- current_result = queens_moves_func(q_row, q_col, matrix)
- if current_result:
- attacking_queens.append(current_queen)
- if attacking_queens:
- for el in attacking_queens:
- print([*el])
- else:
- print('The king is safe!')
- ---------------------------------------------------------------------------------------------------
- # 03. List Pureness
- from collections import deque
- def best_list_pureness(nums_list, K):
- best_pureness = float('-inf')
- best_rotate = 0
- rotate_this = deque(nums_list)
- for current_rotation in range(K + 1):
- pureness = 0
- for index, el in enumerate(rotate_this):
- pureness += el * index
- if pureness > best_pureness:
- best_pureness = pureness
- best_rotate = current_rotation
- rotate_this.rotate()
- return f'Best pureness {best_pureness} after {best_rotate} rotations'
- ---------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement