andonyan

Treasure Hunt

Feb 28th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. list_initial_loot = input().split('|')
  2.  
  3. # no need to capture input first and then split, lines 1 & 3 can be combined in initial_loot = input().split('|')
  4.  
  5. command = input()
  6. while command != "Yohoho!":
  7.     tokens = command.split(" ")
  8.     comanda = tokens[0]
  9.     if comanda == "Loot":
  10.         for k in tokens[1:]:  # could be simplified by for k in tokens[1:]
  11.             if k not in list_initial_loot:  # could skip the check for k != 'Loot' if above statement is included in the for cycle
  12.                 list_initial_loot.insert(0, k)
  13.     elif comanda == "Drop":
  14.         index = int(tokens[1])
  15.         if 0 <= index < len(list_initial_loot):  # index CAN be 0 , should be 0 <= index < len(list_initial_loot)
  16.             list_initial_loot.append(list_initial_loot.pop(index))  # could be simplified to initial_loot.append(initial_loot.pop(index))
  17.  
  18.     elif comanda == "Steal":
  19.         counta = int(tokens[1])
  20.         stolen = []  # double declaration of stolen [] , not necessary
  21.         if counta <= len(list_initial_loot):  # no need for count to be > 0 , it could also be == to len(initial list) or even > len(list)
  22.             stolen = list_initial_loot[-counta::]
  23.             for i in range(counta):
  24.                 list_initial_loot.pop()
  25.  
  26.         else:
  27.             stolen = list_initial_loot.copy()
  28.             list_initial_loot.clear()
  29.         print(", ".join(stolen))
  30.     command = input()
  31.  
  32. if list_initial_loot:  # could be simplified to if list_initial_loot
  33.     sum_of_items = 0
  34.  
  35.     for item in list_initial_loot:
  36.         sum_of_items += len(item)
  37.     averageGain = sum_of_items / len(list_initial_loot)
  38.     print(f"Average treasure gain: {averageGain:.2f} pirate credits.")
  39. else:
  40.     print("Failed treasure hunt.")
Advertisement
Add Comment
Please, Sign In to add comment