Advertisement
mbratanov

Untitled

Oct 10th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def boarding_passengers(capacity, *args):
  5.     guests = {}
  6.     boarding_queue = deque(args)
  7.     total_passengers = sum(int(group[0]) for group in args)
  8.     while capacity > 0 and boarding_queue:
  9.         passengers, benefit_plan = boarding_queue.popleft()
  10.         passengers = int(passengers)
  11.         if passengers <= capacity:
  12.             if benefit_plan not in guests:
  13.                 guests[benefit_plan] = 0
  14.             guests[benefit_plan] += passengers
  15.             capacity -= passengers
  16.     result = ["Boarding details by benefit plan:"]
  17.     for group, guests_num in sorted(guests.items(), key=lambda item: (-item[1], item[0])):
  18.         result.append(f"## {group}: {guests_num} guests")
  19.  
  20.     if sum(guests.values()) == total_passengers:
  21.         result.append("All passengers are successfully boarded!")
  22.  
  23.     elif capacity == 0:
  24.         result.append("Boarding unsuccessful. Cruise ship at full capacity.")
  25.     else:
  26.         result.append(f"Partial boarding completed. Available capacity: {capacity}.")
  27.  
  28.     return "\n".join(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement