Advertisement
GeorgiLukanov87

Python Advanced Retake Exam - 14 April 2022

Sep 15th, 2022 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.72 KB | None | 0 0
  1. # Python Advanced Retake Exam - 14 April 2022
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/3430#0
  4.  
  5. # 01. Ramen Shop
  6. # 02. Martian Explorer
  7. # 03. Words Sorting
  8.  
  9. -----------------------------------------------------------------------------------------------------
  10.  
  11. # 01. Ramen Shop
  12.  
  13.  
  14. from collections import deque
  15.  
  16. ramen = deque([int(x) for x in input().split(', ')])
  17. customers = deque([int(x) for x in input().split(', ')])
  18.  
  19. while True:
  20.     if not customers or not ramen:
  21.         break
  22.  
  23.     last_ramen = ramen[-1]
  24.     first_customer = customers[0]
  25.  
  26.     if last_ramen == first_customer:
  27.         ramen.pop()
  28.         customers.popleft()
  29.         continue
  30.  
  31.     if last_ramen > first_customer:
  32.         ramen[-1] -= first_customer
  33.         customers.popleft()
  34.  
  35.     elif first_customer > last_ramen:
  36.         customers[0] -= last_ramen
  37.         ramen.pop()
  38.  
  39. if not customers:
  40.     print(f"Great job! You served all the customers.")
  41.     if ramen:
  42.         print(f"Bowls of ramen left: {', '.join(str(x) for x in ramen)}")
  43. else:
  44.     print("Out of ramen! You didn't manage to serve all customers.")
  45.     if customers:
  46.         print(f"Customers left: {', '.join(str(x) for x in customers)}")
  47.  
  48.  
  49. -----------------------------------------------------------------------------------------------------
  50.  
  51. # 02. Martian Explorer
  52. # NEW VERSION !
  53.  
  54. def validate_step(r, c):
  55.     if r >= SIZE:
  56.         r = 0
  57.     elif r < 0:
  58.         r = (SIZE - 1)
  59.  
  60.     if c >= SIZE:
  61.         c = 0
  62.     elif c < 0:
  63.         c = (SIZE - 1)
  64.     return r, c
  65.  
  66.  
  67. def get_next_position(direction, r, c):
  68.     if direction == 'left':
  69.         return validate_step(r, c - 1)
  70.     elif direction == 'right':
  71.         return validate_step(r, c + 1)
  72.     elif direction == 'up':
  73.         return validate_step(r - 1, c)
  74.     elif direction == 'down':
  75.         return validate_step(r + 1, c)
  76.  
  77.  
  78. SIZE = 6
  79. matrix = []
  80. row, col = 0, 0
  81.  
  82. harvested = {'W': 0, 'M': 0, 'C': 0}
  83. deposits_dict = {'W': 'Water', 'M': 'Metal', 'C': 'Concrete'}
  84.  
  85. for r_idx in range(SIZE):
  86.     matrix.append(input().split())
  87.     for c_idx in range(SIZE):
  88.         if matrix[r_idx][c_idx] == 'E':
  89.             row, col = r_idx, c_idx
  90.  
  91. moves = input().split(', ')
  92.  
  93. for move in moves:
  94.     row, col = get_next_position(move, row, col)
  95.     current_step = matrix[row][col]
  96.     matrix[row][col] = '-'
  97.  
  98.     if current_step == 'R':
  99.         print(f"Rover got broken at ({row}, {col})")
  100.         break
  101.     if current_step in ['W', 'M', 'C']:
  102.         harvested[current_step] += 1
  103.         print(f'{deposits_dict[current_step]} deposit found at ({row}, {col})')
  104.  
  105. if harvested['W'] and harvested['M'] and harvested['C']:
  106.     print('Area suitable to start the colony.')
  107. else:
  108.     print('Area not suitable to start the colony.')
  109.  
  110.  
  111. =====================================================================================================
  112.  
  113. # 02. Martian Explorer
  114. # OLD VERSION
  115.  
  116. from collections import deque
  117.  
  118.  
  119. def is_inside(r, c):
  120.     return 0 <= r < size and 0 <= c < size
  121.  
  122.  
  123. def get_next_position_func(direction, current_row, current_col):
  124.     if direction == 'up':
  125.         if is_inside(current_row - 1, current_col):
  126.             return current_row - 1, current_col
  127.         return 5, current_col
  128.  
  129.     elif direction == 'down':
  130.         if is_inside(current_row + 1, current_col):
  131.             return current_row + 1, current_col
  132.         return 0, current_col
  133.  
  134.     elif direction == 'left':
  135.         if is_inside(current_row, current_col - 1):
  136.             return current_row, current_col - 1
  137.         return current_row, 5
  138.  
  139.     elif direction == 'right':
  140.         if is_inside(current_row, current_col + 1):
  141.             return current_row, current_col + 1
  142.         return current_row, 0
  143.  
  144.  
  145. deposits_found = {"W": 0, "M": 0, "C": 0}
  146. size = 6
  147. matrix = []
  148. row, col = 0, 0
  149.  
  150. for row_index in range(size):
  151.     current_line = input()
  152.     matrix.append(current_line.split())
  153.     for col_index in range(size):
  154.         if matrix[row_index][col_index] == 'E':
  155.             row, col = row_index, col_index
  156.  
  157. all_commands = deque(input().split(', '))
  158. while all_commands:
  159.     current_direction = all_commands.popleft()
  160.     row, col = get_next_position_func(current_direction, row, col)
  161.     current_position = matrix[row][col]
  162.  
  163.     if current_position == 'R':
  164.         print(f'Rover got broken at ({row}, {col})')
  165.         break
  166.     elif current_position == 'W':
  167.         print(f'Water deposit found at ({row}, {col})')
  168.     elif current_position == 'M':
  169.         print(f'Metal deposit found at ({row}, {col})')
  170.     elif current_position == 'C':
  171.         print(f'Concrete deposit found at ({row}, {col})')
  172.  
  173.     if current_position in ['M', 'C', 'W']:
  174.         deposits_found[current_position] += 1
  175.     matrix[row][col] = '-'
  176.  
  177. if deposits_found['W'] and deposits_found['M'] and deposits_found['C']:
  178.     print('Area suitable to start the colony.')
  179. else:
  180.     print('Area not suitable to start the colony.')
  181.  
  182.  
  183. -----------------------------------------------------------------------------------------------------
  184.  
  185. # 03. Words Sorting
  186.  
  187.  
  188. def words_sorting(*args):
  189.     words_dict = {}
  190.     total_sum = 0
  191.     for el in args:
  192.         ascii_sum = 0
  193.         for char in el:
  194.             ascii_sum += ord(char)
  195.         total_sum += ascii_sum
  196.         words_dict[el] = ascii_sum
  197.  
  198.     final_print = ""
  199.     if total_sum % 2 == 0:
  200.         for word, points in sorted(words_dict.items(), key=lambda x: x[0]):
  201.             final_print += f'{word} - {points}\n'
  202.     else:
  203.         for word, points in sorted(words_dict.items(), key=lambda x: -x[1]):
  204.             final_print += f'{word} - {points}\n'
  205.  
  206.     return final_print
  207.  
  208.  
  209. -----------------------------------------------------------------------------------------------------
  210.  
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement