Advertisement
Guest User

Final Exam Preparation - 24 July 2019/01.Concert

a guest
Dec 5th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. class Band:
  2.     def __init__(self, name: str, time: int):
  3.         self.name = name
  4.         self.time = time
  5.         self.members = list()
  6.  
  7.  
  8. all_bands = list()
  9.  
  10.  
  11. def band_exist(b):
  12.     for band in all_bands:
  13.         if band.name == b:
  14.             return band
  15.  
  16.  
  17. def add_band_and_members(b, b_m):
  18.     if band_exist(b):
  19.         current_band = band_exist(b)
  20.         for member in b_m:
  21.             if member not in current_band.members:
  22.                 current_band.members.append(member)
  23.     else:
  24.         new_band = Band(b, 0)
  25.         for member in b_m:
  26.             if member not in new_band.members:
  27.                 new_band.members.append(member)
  28.         all_bands.append(new_band)
  29.  
  30.  
  31. def update_or_add_band_time(b_n, p_t):
  32.     existing_band = band_exist(b_n)
  33.     if existing_band:
  34.         existing_band.time += p_t
  35.     else:
  36.         all_bands.append(Band(name=b_n, time=p_t))
  37.  
  38.  
  39. def total_time():
  40.     t_time = 0
  41.     for b in all_bands:
  42.         t_time += b.time
  43.     return t_time
  44.  
  45.  
  46. while True:
  47.     txt = input()
  48.     if txt == "start of concert":
  49.         break
  50.     else:
  51.         txt_split = txt.split("; ")
  52.         command = txt_split[0]
  53.         if command == "Add":
  54.             band_name = txt_split[1]
  55.             band_members = txt_split[2].split(", ")
  56.             add_band_and_members(band_name, band_members)
  57.  
  58.         elif command == "Play":
  59.             band_name = txt_split[1]
  60.             play_time = int(txt_split[2])
  61.             update_or_add_band_time(band_name, play_time)
  62. total_time = total_time()
  63. print(f"Total time: {total_time}")
  64. all_bands = sorted(all_bands, key=lambda x: (-x.time, x.name))
  65. for band in all_bands:
  66.     print(f"{band.name} -> {band.time}")
  67. band_to_print = band_exist(input())
  68. print(f"{band_to_print.name}")
  69. for member in band_to_print.members:
  70.     print(f"=> {member}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement