Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Advanced Retake Exam - 15 December 2021
- # https://judge.softuni.org/Contests/Practice/Index/3306#0
- # 01. Christmas Elves
- # 02. North Pole Challenge
- # 03. Naughty or Nice
- --------------------------------------------------------------------------------------------
- # 01. Christmas Elves
- from collections import deque
- elves_energy = deque([int(x) for x in input().split()]) # FIRST
- materials = deque([int(x) for x in input().split()]) # LAST
- toys = 0
- total_energy = 0
- turns = 0
- while True:
- if not materials or not elves_energy:
- break
- first_elf = elves_energy[0]
- last_material = materials[-1]
- if first_elf < 5:
- elves_energy.popleft()
- continue
- turns += 1
- if turns % 3 == 0 and turns % 5 == 0:
- if first_elf >= last_material * 2:
- first_elf -= last_material * 2
- total_energy += last_material * 2
- elves_energy.append(first_elf)
- elves_energy.popleft()
- materials.pop()
- else:
- elves_energy.popleft()
- elves_energy.append(first_elf * 2)
- continue
- if turns % 3 == 0:
- if first_elf >= last_material * 2:
- toys += 2
- first_elf -= last_material * 2
- total_energy += last_material * 2
- elves_energy.append(first_elf + 1)
- elves_energy.popleft()
- materials.pop()
- else:
- elves_energy.popleft()
- elves_energy.append(first_elf * 2)
- continue
- if turns % 5 == 0:
- if first_elf >= last_material:
- first_elf -= last_material
- total_energy += last_material
- elves_energy.append(first_elf)
- elves_energy.popleft()
- materials.pop()
- else:
- elves_energy.popleft()
- elves_energy.append(first_elf * 2)
- continue
- if first_elf >= last_material:
- toys += 1
- first_elf -= last_material
- total_energy += last_material
- elves_energy.append(first_elf + 1)
- elves_energy.popleft()
- materials.pop()
- else:
- elves_energy.popleft()
- elves_energy.append(first_elf * 2)
- print(f'Toys: {toys}')
- print(f'Energy: {total_energy}')
- if elves_energy:
- print(f'Elves left: {", ".join(str(x) for x in elves_energy)}')
- if materials:
- print(f'Boxes left: {", ".join(str(x) for x in materials)}')
- --------------------------------------------------------------------------------------------
- # 02. North Pole Challenge
- def is_inside_func(r, c): # Validating if cell is in the matrix ! ROW != COL !
- return 0 <= r < row_size and 0 <= c < col_size
- def check_and_change_func(row, col, collected_all): # Check every step and extract info !
- current_step = matrix[row][col]
- if current_step in field_history:
- field_history[current_step] -= 1 # Reduce item found !
- collected[current_step] += 1 # Increase item found !
- if not field_history['D'] and not field_history['G'] and not field_history['C']:
- collected_all = True # NEED STOP THE PROGRAM !
- return collected_all
- matrix[row][col] = 'x'
- return collected_all
- #####################
- # #
- # . . . . . #
- # C . . G . #
- # . C . . . #
- # G . . C . #
- # . D . . D #
- # Y . . . G #
- # #
- #####################
- def move_func(direction, steps, row, col, collected_all):
- if direction == 'left' and steps:
- if not is_inside_func(row, col - 1): # FIRST STEP -> OUTSIDE !
- col = col_size - 1
- else: # FIRST STEP -> INSIDE !
- col -= 1
- while steps:
- collected_all = check_and_change_func(row, col, collected_all)
- if collected_all:
- return row, col, collected_all
- steps -= 1
- if steps:
- col -= 1
- if col == -1:
- col = col_size - 1
- # -------------------------------------------------------------------------
- if direction == 'right' and steps:
- if not is_inside_func(row, col + 1): # 1st step -> OUTSIDE !
- col = 0
- else: # FIRST STEP -> INSIDE !
- col += 1
- while steps:
- collected_all = check_and_change_func(row, col, collected_all)
- if collected_all:
- return row, col, collected_all
- steps -= 1
- if steps:
- col += 1
- if col == col_size:
- col = 0
- # -------------------------------------------------------------------------
- if direction == 'down' and steps:
- if not is_inside_func(row + 1, col): # 1st step -> OUTSIDE !
- row = 0
- else: # FIRST STEP -> INSIDE !
- row += 1
- while steps:
- collected_all = check_and_change_func(row, col, collected_all)
- if collected_all:
- return row, col, collected_all
- steps -= 1
- if steps:
- row += 1
- if row == row_size:
- row = 0
- # -------------------------------------------------------------------------
- if direction == 'up' and steps:
- if not is_inside_func(row - 1, col): # 1st step -> OUTSIDE !
- row = row_size - 1
- else: # FIRST STEP -> INSIDE !
- row -= 1
- while steps:
- collected_all = check_and_change_func(row, col, collected_all)
- if collected_all:
- return row, col, collected_all
- steps -= 1
- if steps:
- row -= 1
- if row == -1:
- row = row_size - 1
- return row, col, collected_all
- # -------------------------------------------------------------------------
- size = input().split(', ')
- row_size = int(size[0])
- col_size = int(size[1])
- matrix = []
- field_history = {'D': 0, 'G': 0, 'C': 0}
- collected = {'D': 0, 'G': 0, 'C': 0}
- row, col = 0, 0
- for row_index in range(row_size):
- matrix.append(input().split())
- for col_index in range(col_size):
- if matrix[row_index][col_index] == 'Y':
- row, col = row_index, col_index
- if matrix[row_index][col_index] in field_history:
- field_history[matrix[row_index][col_index]] += 1
- collected_all = False
- while True:
- command = input()
- if command == 'End':
- break
- command = command.split('-')
- direction = command[0]
- steps = int(command[1])
- if steps:
- matrix[row][col] = 'x'
- row, col, collected_all = move_func(direction, steps, row, col, collected_all)
- matrix[row][col] = 'Y'
- if collected_all:
- print('Merry Christmas!')
- break
- print("You've collected:")
- print(f'- {collected["D"]} Christmas decorations')
- print(f'- {collected["G"]} Gifts')
- print(f'- {collected["C"]} Cookies')
- for el in matrix:
- print(*el)
- --------------------------------------------------------------------------------------------
- # 03. Naughty or Nice -> JUDGE 95/100
- def naughty_or_nice_list(tuples_names, *arguments, **keywords):
- not_found = []
- found_kids = {'Nice': [], "Naughty": []}
- names_dict = {}
- for number, name in tuples_names:
- if number not in names_dict:
- names_dict[number] = []
- names_dict[number].append(name)
- arguments_dict = {}
- if arguments:
- for el in arguments:
- command = int(el[0])
- behavior = el[2:]
- arguments_dict[command] = behavior
- for command, behavior in arguments_dict.items():
- if command in names_dict.keys() and not len(names_dict[command]) > 1:
- found_kids[behavior].extend(names_dict[command])
- del names_dict[command]
- repeating_names = 0
- if keywords:
- for name, behavior in keywords.items():
- for el_list in names_dict.values():
- if name in el_list:
- repeating_names += el_list.count(name)
- if not repeating_names > 1:
- found_kids[behavior].extend([name])
- repeating_names = 0
- for key, values in names_dict.items():
- if name in values:
- names_dict[key].remove(name)
- continue
- for key, last_value in names_dict.items():
- if last_value:
- not_found.extend(last_value)
- names_dict[key] = []
- final_print = ""
- for behave, names in found_kids.items():
- if names:
- final_print += f"{behave}: {', '.join(names)}\n"
- last_names = []
- if not_found:
- final_print += f'Not found: '
- for name in not_found:
- last_names.append(name)
- final_print += f'{", ".join(last_names)}'
- return final_print
- --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement