Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. target_log = {}
  2.  
  3. while True:
  4.     commands = input()
  5.     if commands == 'Sail':
  6.         break
  7.     commands = commands.split('||')
  8.     town = commands[0]
  9.     peoples = int(commands[1])
  10.     gold = int(commands[2])
  11.     if town not in target_log:
  12.         target_log[town] = [peoples, gold]
  13.     else:
  14.         target_log[town][0] += peoples
  15.         target_log[town][1] += gold
  16.  
  17. while True:
  18.     tokens = input()
  19.     if tokens == 'End':
  20.         break
  21.     tokens = tokens.split('=>')
  22.     command = tokens[0]
  23.     city = tokens[1]
  24.     if command == 'Plunder':
  25.         people = int(tokens[2])
  26.         money = int(tokens[3])
  27.         if city in target_log:
  28.             target_log[city][0] -= people
  29.             target_log[city][1] -= money
  30.             print(f'{city} plundered! {money} gold stolen, {people} citizens killed.')
  31.             if target_log[city][0] <= 0 or target_log[city][1] <= 0:
  32.                 print(f'{city} has been wiped off the map!')
  33.                 target_log.pop(city)
  34.  
  35.     elif command == 'Prosper':
  36.         money = int(tokens[2])
  37.         if money > 0:
  38.             target_log[city][1] += money
  39.             print(f'{money} gold added to the city treasury. {city} now has {target_log[city][1]} gold.')
  40.         elif money < 0:
  41.             print(f'Gold added cannot be a negative number!')
  42.  
  43. sorted_target_log = dict(sorted(target_log.items(), key=lambda x: (-x[1][1], x[0])))
  44.  
  45. if len(sorted_target_log) > 0:
  46.     print(f'Ahoy, Captain! There are {len(sorted_target_log)} wealthy settlements to go to:')
  47.     for k,v in sorted_target_log.items():
  48.         print(f'{k} -> Population: {v[0]} citizens, Gold: {v[1]} kg')
  49. else:
  50.     print('Ahoy, Captain! All targets have been plundered and destroyed!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement