Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Advanced Retake Exam - 14 April 2022
- # https://judge.softuni.org/Contests/Practice/Index/3430#0
- # 01. Ramen Shop
- # 02. Martian Explorer
- # 03. Words Sorting
- -----------------------------------------------------------------------------------------------------
- # 01. Ramen Shop
- from collections import deque
- ramen = deque([int(x) for x in input().split(', ')])
- customers = deque([int(x) for x in input().split(', ')])
- while True:
- if not customers or not ramen:
- break
- last_ramen = ramen[-1]
- first_customer = customers[0]
- if last_ramen == first_customer:
- ramen.pop()
- customers.popleft()
- continue
- if last_ramen > first_customer:
- ramen[-1] -= first_customer
- customers.popleft()
- elif first_customer > last_ramen:
- customers[0] -= last_ramen
- ramen.pop()
- if not customers:
- print(f"Great job! You served all the customers.")
- if ramen:
- print(f"Bowls of ramen left: {', '.join(str(x) for x in ramen)}")
- else:
- print("Out of ramen! You didn't manage to serve all customers.")
- if customers:
- print(f"Customers left: {', '.join(str(x) for x in customers)}")
- -----------------------------------------------------------------------------------------------------
- # 02. Martian Explorer
- # NEW VERSION !
- def validate_step(r, c):
- if r >= SIZE:
- r = 0
- elif r < 0:
- r = (SIZE - 1)
- if c >= SIZE:
- c = 0
- elif c < 0:
- c = (SIZE - 1)
- return r, c
- def get_next_position(direction, r, c):
- if direction == 'left':
- return validate_step(r, c - 1)
- elif direction == 'right':
- return validate_step(r, c + 1)
- elif direction == 'up':
- return validate_step(r - 1, c)
- elif direction == 'down':
- return validate_step(r + 1, c)
- SIZE = 6
- matrix = []
- row, col = 0, 0
- harvested = {'W': 0, 'M': 0, 'C': 0}
- deposits_dict = {'W': 'Water', 'M': 'Metal', 'C': 'Concrete'}
- for r_idx in range(SIZE):
- matrix.append(input().split())
- for c_idx in range(SIZE):
- if matrix[r_idx][c_idx] == 'E':
- row, col = r_idx, c_idx
- moves = input().split(', ')
- for move in moves:
- row, col = get_next_position(move, row, col)
- current_step = matrix[row][col]
- matrix[row][col] = '-'
- if current_step == 'R':
- print(f"Rover got broken at ({row}, {col})")
- break
- if current_step in ['W', 'M', 'C']:
- harvested[current_step] += 1
- print(f'{deposits_dict[current_step]} deposit found at ({row}, {col})')
- if harvested['W'] and harvested['M'] and harvested['C']:
- print('Area suitable to start the colony.')
- else:
- print('Area not suitable to start the colony.')
- =====================================================================================================
- # 02. Martian Explorer
- # OLD VERSION
- from collections import deque
- def is_inside(r, c):
- return 0 <= r < size and 0 <= c < size
- def get_next_position_func(direction, current_row, current_col):
- if direction == 'up':
- if is_inside(current_row - 1, current_col):
- return current_row - 1, current_col
- return 5, current_col
- elif direction == 'down':
- if is_inside(current_row + 1, current_col):
- return current_row + 1, current_col
- return 0, current_col
- elif direction == 'left':
- if is_inside(current_row, current_col - 1):
- return current_row, current_col - 1
- return current_row, 5
- elif direction == 'right':
- if is_inside(current_row, current_col + 1):
- return current_row, current_col + 1
- return current_row, 0
- deposits_found = {"W": 0, "M": 0, "C": 0}
- size = 6
- matrix = []
- row, col = 0, 0
- for row_index in range(size):
- current_line = input()
- matrix.append(current_line.split())
- for col_index in range(size):
- if matrix[row_index][col_index] == 'E':
- row, col = row_index, col_index
- all_commands = deque(input().split(', '))
- while all_commands:
- current_direction = all_commands.popleft()
- row, col = get_next_position_func(current_direction, row, col)
- current_position = matrix[row][col]
- if current_position == 'R':
- print(f'Rover got broken at ({row}, {col})')
- break
- elif current_position == 'W':
- print(f'Water deposit found at ({row}, {col})')
- elif current_position == 'M':
- print(f'Metal deposit found at ({row}, {col})')
- elif current_position == 'C':
- print(f'Concrete deposit found at ({row}, {col})')
- if current_position in ['M', 'C', 'W']:
- deposits_found[current_position] += 1
- matrix[row][col] = '-'
- if deposits_found['W'] and deposits_found['M'] and deposits_found['C']:
- print('Area suitable to start the colony.')
- else:
- print('Area not suitable to start the colony.')
- -----------------------------------------------------------------------------------------------------
- # 03. Words Sorting
- def words_sorting(*args):
- words_dict = {}
- total_sum = 0
- for el in args:
- ascii_sum = 0
- for char in el:
- ascii_sum += ord(char)
- total_sum += ascii_sum
- words_dict[el] = ascii_sum
- final_print = ""
- if total_sum % 2 == 0:
- for word, points in sorted(words_dict.items(), key=lambda x: x[0]):
- final_print += f'{word} - {points}\n'
- else:
- for word, points in sorted(words_dict.items(), key=lambda x: -x[1]):
- final_print += f'{word} - {points}\n'
- return final_print
- -----------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement