|  | @@ -1,47 +1,95 @@
 | 
											
												
													
														|  |  """Calculate donations"""
 |  |  """Calculate donations"""
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import re
 |  |  import re
 | 
											
												
													
														|  | 
 |  | +import math
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -RESOURCES = {
 |  | 
 | 
											
												
													
														|  | -    'bbl': {},
 |  | 
 | 
											
												
													
														|  | -    'kg': {},
 |  | 
 | 
											
												
													
														|  | -    'pcs': {},
 |  | 
 | 
											
												
													
														|  | -    'G': {},
 |  | 
 | 
											
												
													
														|  | -    'g': {},
 |  | 
 | 
											
												
													
														|  | -    '$': {},
 |  | 
 | 
											
												
													
														|  | -}
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  def sum_donations():
 |  |  def sum_donations():
 | 
											
												
													
														|  |      """Count total donations"""
 |  |      """Count total donations"""
 | 
											
												
													
														|  | 
 |  | +    resources = {
 | 
											
												
													
														|  | 
 |  | +        'bbl': {},
 | 
											
												
													
														|  | 
 |  | +        'kg': {},
 | 
											
												
													
														|  | 
 |  | +        'pcs': {},
 | 
											
												
													
														|  | 
 |  | +        'G': {},
 | 
											
												
													
														|  | 
 |  | +        'g': {},
 | 
											
												
													
														|  | 
 |  | +        '$': {},
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  |      with open('donation.txt', 'r') as file:
 |  |      with open('donation.txt', 'r') as file:
 | 
											
												
													
														|  |          for line in file:
 |  |          for line in file:
 | 
											
												
													
														|  |              try:
 |  |              try:
 | 
											
												
													
														|  |                  donation = re.search(r'\d.*?(bbl|kg|pcs|G|g|\$)', line).group(0)
 |  |                  donation = re.search(r'\d.*?(bbl|kg|pcs|G|g|\$)', line).group(0)
 | 
											
												
													
														|  |                  player = re.search(r'.*?\s\t', line).group(0).strip()
 |  |                  player = re.search(r'.*?\s\t', line).group(0).strip()
 | 
											
												
													
														|  | -                print(donation)
 |  | 
 | 
											
												
													
														|  |                  amount, resource = donation.split(' ')
 |  |                  amount, resource = donation.split(' ')
 | 
											
												
													
														|  |                  amount = int(amount.replace('.', ''))
 |  |                  amount = int(amount.replace('.', ''))
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -                if player not in RESOURCES[resource]:
 |  | 
 | 
											
												
													
														|  | -                    RESOURCES[resource][player] = 0
 |  | 
 | 
											
												
													
														|  | -                RESOURCES[resource][player] += amount
 |  | 
 | 
											
												
													
														|  | 
 |  | +                if player not in resources[resource]:
 | 
											
												
													
														|  | 
 |  | +                    resources[resource][player] = 0
 | 
											
												
													
														|  | 
 |  | +                resources[resource][player] += amount
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |              except Exception as exception:
 |  |              except Exception as exception:
 | 
											
												
													
														|  |                  print('exception')
 |  |                  print('exception')
 | 
											
												
													
														|  |                  print('%s %s' % (line, exception))
 |  |                  print('%s %s' % (line, exception))
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +    return resources
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    for resource, players in RESOURCES.items():
 |  | 
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +def print_resources(resources):
 | 
											
												
													
														|  | 
 |  | +    """Print donations per resource"""
 | 
											
												
													
														|  | 
 |  | +    for resource, players in resources.items():
 | 
											
												
													
														|  |          print(resource)
 |  |          print(resource)
 | 
											
												
													
														|  |          count = 1
 |  |          count = 1
 | 
											
												
													
														|  |          for player in sorted(players, key=players.get, reverse=True):
 |  |          for player in sorted(players, key=players.get, reverse=True):
 | 
											
												
													
														|  | -            print('%12d %s' % (players[player], player))
 |  | 
 | 
											
												
													
														|  | 
 |  | +            print('%20s %s' % (bucks(players[player]), player))
 | 
											
												
													
														|  |              if count >= 10:
 |  |              if count >= 10:
 | 
											
												
													
														|  | -                break;
 |  | 
 | 
											
												
													
														|  | 
 |  | +                break
 | 
											
												
													
														|  |              count += 1
 |  |              count += 1
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +def calc_reward(resources):
 | 
											
												
													
														|  | 
 |  | +    """Calculate reward"""
 | 
											
												
													
														|  | 
 |  | +    reward = {
 | 
											
												
													
														|  | 
 |  | +        'bbl': 130,
 | 
											
												
													
														|  | 
 |  | +        'kg': 120,
 | 
											
												
													
														|  | 
 |  | +        'pcs': 850000,
 | 
											
												
													
														|  | 
 |  | +        'g': 1250,
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    resource_koef = {
 | 
											
												
													
														|  | 
 |  | +        'bbl': 1000000000,
 | 
											
												
													
														|  | 
 |  | +        'kg': 1105000000,
 | 
											
												
													
														|  | 
 |  | +        'pcs': 104000000,
 | 
											
												
													
														|  | 
 |  | +        'g': 160000,
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    player_reward = {}
 | 
											
												
													
														|  | 
 |  | +    for resource, players in resources.items():
 | 
											
												
													
														|  | 
 |  | +        if resource not in reward:
 | 
											
												
													
														|  | 
 |  | +            continue
 | 
											
												
													
														|  | 
 |  | +        for player, amount in players.items():
 | 
											
												
													
														|  | 
 |  | +            if player not in player_reward:
 | 
											
												
													
														|  | 
 |  | +                player_reward[player] = 0
 | 
											
												
													
														|  | 
 |  | +            player_reward[player] = reward[resource] * amount
 | 
											
												
													
														|  | 
 |  | +            koef = math.floor(amount / resource_koef[resource])
 | 
											
												
													
														|  | 
 |  | +            player_reward[player] += round(player_reward[player] / 100 * koef * 2)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    return player_reward
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +def print_reward(players):
 | 
											
												
													
														|  | 
 |  | +    """print player rewards"""
 | 
											
												
													
														|  | 
 |  | +    total = 0
 | 
											
												
													
														|  | 
 |  | +    for player, reward in sorted(players.items(), key=lambda x: x[1], reverse=True):
 | 
											
												
													
														|  | 
 |  | +        total += reward
 | 
											
												
													
														|  | 
 |  | +        print('%20s %s' % (bucks(reward), player))
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    print('total: %s' % bucks(total))
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +def bucks(integer):
 | 
											
												
													
														|  | 
 |  | +    """Format number"""
 | 
											
												
													
														|  | 
 |  | +    return '{:,}'.format(integer).replace(',', '.')
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  if __name__ == '__main__':
 |  |  if __name__ == '__main__':
 | 
											
												
													
														|  | -    sum_donations()
 |  | 
 | 
											
												
													
														|  | 
 |  | +    RESOURCES = sum_donations()
 | 
											
												
													
														|  | 
 |  | +    PLAYERS = calc_reward(RESOURCES)
 | 
											
												
													
														|  | 
 |  | +    print_reward(PLAYERS)
 | 
											
												
													
														|  | 
 |  | +    print_resources(RESOURCES)
 |