donations.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Calculate donations"""
  2. import re
  3. import math
  4. def sum_donations():
  5. """Count total donations"""
  6. resources = {
  7. 'bbl': {},
  8. 'kg': {},
  9. 'pcs': {},
  10. 'G': {},
  11. 'g': {},
  12. '$': {},
  13. }
  14. with open('donations.txt', 'r') as file:
  15. for line in file:
  16. try:
  17. donation = re.search(r'\d.*?(bbl|kg|pcs|G|g|\$)', line).group(0)
  18. player = re.search(r'.*?\s\t', line).group(0).strip()
  19. amount, resource = donation.split(' ')
  20. amount = int(amount.replace('.', ''))
  21. if player not in resources[resource]:
  22. resources[resource][player] = 0
  23. resources[resource][player] += amount
  24. if 'total' not in resources[resource]:
  25. resources[resource]['total'] = 0
  26. resources[resource]['total'] += amount
  27. except Exception as exception:
  28. print('exception')
  29. print('%s %s' % (line, exception))
  30. return resources
  31. def print_resources(resources):
  32. """Print donations per resource"""
  33. for resource, players in resources.items():
  34. print(resource)
  35. count = 1
  36. for player in sorted(players, key=players.get, reverse=True):
  37. print('%20s %s' % (bucks(players[player]), player))
  38. if count >= 10:
  39. break
  40. count += 1
  41. def calc_reward(resources):
  42. """Calculate reward"""
  43. reward = {
  44. 'bbl': 130,
  45. 'kg': 120,
  46. 'pcs': 850000,
  47. 'g': 1250,
  48. }
  49. resource_koef = {
  50. 'bbl': 1000000000,
  51. 'kg': 1105000000,
  52. 'pcs': 104000000,
  53. 'g': 160000,
  54. }
  55. player_reward = {}
  56. for resource, players in resources.items():
  57. if resource not in reward:
  58. continue
  59. for player, amount in players.items():
  60. if player == 'total':
  61. continue
  62. if player not in player_reward:
  63. player_reward[player] = 0
  64. tmp_reward = reward[resource] * amount
  65. koef = math.floor(amount / resource_koef[resource])
  66. player_reward[player] += tmp_reward + round(tmp_reward / 100 * koef * 2)
  67. return player_reward
  68. def print_reward(players):
  69. """print player rewards"""
  70. total = 0
  71. for player, reward in sorted(players.items(), key=lambda x: x[1], reverse=True):
  72. total += reward
  73. print('%20s %s' % (bucks(reward), player))
  74. print('total: %s' % bucks(total))
  75. def bucks(integer):
  76. """Format number"""
  77. return '{:,}'.format(integer).replace(',', '.')
  78. if __name__ == '__main__':
  79. RESOURCES = sum_donations()
  80. PLAYERS = calc_reward(RESOURCES)
  81. print_reward(PLAYERS)
  82. print_resources(RESOURCES)