Advertisement
GeorgiLukanov87

Python Advanced Retake Exam - 15 December 2021

Sep 20th, 2022
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.05 KB | None | 0 0
  1. # Python Advanced Retake Exam - 15 December 2021
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/3306#0
  4.  
  5. # 01. Christmas Elves
  6. # 02. North Pole Challenge
  7. # 03. Naughty or Nice
  8.  
  9. --------------------------------------------------------------------------------------------
  10.  
  11. # 01. Christmas Elves
  12.  
  13.  
  14. from collections import deque
  15.  
  16. elves_energy = deque([int(x) for x in input().split()])  # FIRST
  17. materials = deque([int(x) for x in input().split()])  # LAST
  18. toys = 0
  19. total_energy = 0
  20. turns = 0
  21. while True:
  22.     if not materials or not elves_energy:
  23.         break
  24.        
  25.     first_elf = elves_energy[0]
  26.     last_material = materials[-1]
  27.    
  28.     if first_elf < 5:
  29.         elves_energy.popleft()
  30.         continue
  31.        
  32.     turns += 1
  33.     if turns % 3 == 0 and turns % 5 == 0:
  34.         if first_elf >= last_material * 2:
  35.             first_elf -= last_material * 2
  36.             total_energy += last_material * 2
  37.             elves_energy.append(first_elf)
  38.             elves_energy.popleft()
  39.             materials.pop()
  40.         else:
  41.             elves_energy.popleft()
  42.             elves_energy.append(first_elf * 2)
  43.         continue
  44.  
  45.     if turns % 3 == 0:
  46.         if first_elf >= last_material * 2:
  47.             toys += 2
  48.             first_elf -= last_material * 2
  49.             total_energy += last_material * 2
  50.             elves_energy.append(first_elf + 1)
  51.             elves_energy.popleft()
  52.             materials.pop()
  53.         else:
  54.             elves_energy.popleft()
  55.             elves_energy.append(first_elf * 2)
  56.         continue
  57.  
  58.     if turns % 5 == 0:
  59.         if first_elf >= last_material:
  60.             first_elf -= last_material
  61.             total_energy += last_material
  62.             elves_energy.append(first_elf)
  63.             elves_energy.popleft()
  64.             materials.pop()
  65.         else:
  66.             elves_energy.popleft()
  67.             elves_energy.append(first_elf * 2)
  68.         continue
  69.  
  70.     if first_elf >= last_material:
  71.         toys += 1
  72.         first_elf -= last_material
  73.         total_energy += last_material
  74.         elves_energy.append(first_elf + 1)
  75.         elves_energy.popleft()
  76.         materials.pop()
  77.     else:
  78.         elves_energy.popleft()
  79.         elves_energy.append(first_elf * 2)
  80.  
  81. print(f'Toys: {toys}')
  82. print(f'Energy: {total_energy}')
  83. if elves_energy:
  84.     print(f'Elves left: {", ".join(str(x) for x in elves_energy)}')
  85. if materials:
  86.     print(f'Boxes left: {", ".join(str(x) for x in materials)}')
  87.  
  88.  
  89. --------------------------------------------------------------------------------------------
  90.  
  91.  
  92. # 02. North Pole Challenge
  93.  
  94.  
  95. def is_inside_func(r, c):  # Validating if cell is in the matrix ! ROW != COL !
  96.     return 0 <= r < row_size and 0 <= c < col_size
  97.  
  98.  
  99. def check_and_change_func(row, col, collected_all):  # Check every step and extract info !
  100.     current_step = matrix[row][col]
  101.     if current_step in field_history:
  102.         field_history[current_step] -= 1  # Reduce item found !
  103.         collected[current_step] += 1  # Increase item found !
  104.         if not field_history['D'] and not field_history['G'] and not field_history['C']:
  105.             collected_all = True  # NEED STOP THE PROGRAM !
  106.             return collected_all
  107.  
  108.     matrix[row][col] = 'x'
  109.     return collected_all
  110. #####################
  111. #                   #
  112. #     . . . . .     #
  113. #     C . . G .     #
  114. #     . C . . .     #
  115. #     G . . C .     #
  116. #     . D . . D     #
  117. #     Y . . . G     #
  118. #                   #
  119. #####################
  120. def move_func(direction, steps, row, col, collected_all):
  121.     if direction == 'left' and steps:
  122.         if not is_inside_func(row, col - 1):  # FIRST STEP -> OUTSIDE !
  123.             col = col_size - 1
  124.         else:  # FIRST STEP -> INSIDE !
  125.             col -= 1
  126.         while steps:
  127.             collected_all = check_and_change_func(row, col, collected_all)
  128.             if collected_all:
  129.                 return row, col, collected_all
  130.             steps -= 1
  131.             if steps:
  132.                 col -= 1
  133.             if col == -1:
  134.                 col = col_size - 1
  135.     # -------------------------------------------------------------------------
  136.     if direction == 'right' and steps:
  137.         if not is_inside_func(row, col + 1):  # 1st step -> OUTSIDE !
  138.             col = 0
  139.         else:  # FIRST STEP -> INSIDE !
  140.             col += 1
  141.         while steps:
  142.             collected_all = check_and_change_func(row, col, collected_all)
  143.             if collected_all:
  144.                 return row, col, collected_all
  145.             steps -= 1
  146.             if steps:
  147.                 col += 1
  148.             if col == col_size:
  149.                 col = 0
  150.     # -------------------------------------------------------------------------
  151.     if direction == 'down' and steps:
  152.         if not is_inside_func(row + 1, col):  # 1st step -> OUTSIDE !
  153.             row = 0
  154.         else:  # FIRST STEP -> INSIDE !
  155.             row += 1
  156.         while steps:
  157.             collected_all = check_and_change_func(row, col, collected_all)
  158.             if collected_all:
  159.                 return row, col, collected_all
  160.             steps -= 1
  161.             if steps:
  162.                 row += 1
  163.             if row == row_size:
  164.                 row = 0
  165.     # -------------------------------------------------------------------------
  166.     if direction == 'up' and steps:
  167.         if not is_inside_func(row - 1, col):  # 1st step -> OUTSIDE !
  168.             row = row_size - 1
  169.         else:  # FIRST STEP -> INSIDE !
  170.             row -= 1
  171.         while steps:
  172.             collected_all = check_and_change_func(row, col, collected_all)
  173.             if collected_all:
  174.                 return row, col, collected_all
  175.             steps -= 1
  176.             if steps:
  177.                 row -= 1
  178.             if row == -1:
  179.                 row = row_size - 1
  180.     return row, col, collected_all
  181.     # -------------------------------------------------------------------------
  182.  
  183.  
  184. size = input().split(', ')
  185. row_size = int(size[0])
  186. col_size = int(size[1])
  187.  
  188. matrix = []
  189. field_history = {'D': 0, 'G': 0, 'C': 0}
  190. collected = {'D': 0, 'G': 0, 'C': 0}
  191. row, col = 0, 0
  192.  
  193. for row_index in range(row_size):
  194.     matrix.append(input().split())
  195.     for col_index in range(col_size):
  196.         if matrix[row_index][col_index] == 'Y':
  197.             row, col = row_index, col_index
  198.         if matrix[row_index][col_index] in field_history:
  199.             field_history[matrix[row_index][col_index]] += 1
  200.  
  201. collected_all = False
  202. while True:
  203.     command = input()
  204.     if command == 'End':
  205.         break
  206.  
  207.     command = command.split('-')
  208.     direction = command[0]
  209.     steps = int(command[1])
  210.  
  211.     if steps:
  212.         matrix[row][col] = 'x'
  213.         row, col, collected_all = move_func(direction, steps, row, col, collected_all)
  214.         matrix[row][col] = 'Y'
  215.         if collected_all:
  216.             print('Merry Christmas!')
  217.             break
  218.  
  219. print("You've collected:")
  220. print(f'- {collected["D"]} Christmas decorations')
  221. print(f'- {collected["G"]} Gifts')
  222. print(f'- {collected["C"]} Cookies')
  223.  
  224. for el in matrix:
  225.     print(*el)
  226.  
  227.  
  228. --------------------------------------------------------------------------------------------
  229.  
  230. # 03. Naughty or Nice -> JUDGE 95/100
  231.  
  232.  
  233. def naughty_or_nice_list(tuples_names, *arguments, **keywords):
  234.     not_found = []
  235.     found_kids = {'Nice': [], "Naughty": []}
  236.     names_dict = {}
  237.     for number, name in tuples_names:
  238.         if number not in names_dict:
  239.             names_dict[number] = []
  240.         names_dict[number].append(name)
  241.  
  242.     arguments_dict = {}
  243.     if arguments:
  244.         for el in arguments:
  245.             command = int(el[0])
  246.             behavior = el[2:]
  247.             arguments_dict[command] = behavior
  248.  
  249.     for command, behavior in arguments_dict.items():
  250.         if command in names_dict.keys() and not len(names_dict[command]) > 1:
  251.             found_kids[behavior].extend(names_dict[command])
  252.             del names_dict[command]
  253.     repeating_names = 0
  254.     if keywords:
  255.         for name, behavior in keywords.items():
  256.             for el_list in names_dict.values():
  257.                 if name in el_list:
  258.                     repeating_names += el_list.count(name)
  259.                     if not repeating_names > 1:
  260.                         found_kids[behavior].extend([name])
  261.                         repeating_names = 0
  262.                         for key, values in names_dict.items():
  263.                             if name in values:
  264.                                 names_dict[key].remove(name)
  265.                                 continue
  266.  
  267.     for key, last_value in names_dict.items():
  268.         if last_value:
  269.             not_found.extend(last_value)
  270.             names_dict[key] = []
  271.  
  272.     final_print = ""
  273.     for behave, names in found_kids.items():
  274.         if names:
  275.             final_print += f"{behave}: {', '.join(names)}\n"
  276.     last_names = []
  277.     if not_found:
  278.         final_print += f'Not found: '
  279.         for name in not_found:
  280.             last_names.append(name)
  281.     final_print += f'{", ".join(last_names)}'
  282.  
  283.     return final_print
  284.  
  285. --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement