fork download
  1. def step1(dres, aloot, potential):
  2.  
  3. again = False
  4.  
  5. ndres = {}
  6.  
  7. if len(dres) > 0:
  8.  
  9. avgloot = int(potential / len(dres))
  10.  
  11. for r in dres:
  12. if dres[r] <= avgloot:
  13. potential -= dres[r]
  14. aloot[r] += dres[r]
  15. again = True
  16. else:
  17. ndres[r] = dres[r]
  18.  
  19. return (ndres, aloot, potential, again)
  20.  
  21. def calculate_loot(dres, potential):
  22.  
  23. aloot = {'wood':0, 'iron':0, 'wheat':0, 'gold':0}
  24.  
  25. (dres, aloot, potential, again) = step1(dres, aloot, potential)
  26.  
  27. while again:
  28. (dres, aloot, potential, again) = step1(dres, aloot, potential)
  29.  
  30. if len(dres) > 0:
  31. avgloot = int(potential / len(dres))
  32. for r in dres:
  33. aloot[r] += avgloot
  34.  
  35. return aloot
  36.  
  37. potential = 250
  38.  
  39. for dres in [
  40. {'wood':100, 'iron':25, 'wheat':76, 'gold':50000},
  41. {'wood':2500, 'iron':2500, 'wheat':5000, 'gold':5000 },
  42. {'wood':100, 'iron':0, 'wheat':1, 'gold':0 },
  43. {'wood':0, 'iron':0, 'wheat':0, 'gold':50000}
  44. ]:
  45.  
  46. print(dres)
  47. print(calculate_loot(dres, potential))
  48. print(' ')
Success #stdin #stdout 0.1s 10104KB
stdin
Standard input is empty
stdout
{'wood': 100, 'wheat': 76, 'gold': 50000, 'iron': 25}
{'wood': 75, 'wheat': 75, 'gold': 75, 'iron': 25}
 
{'wood': 2500, 'wheat': 5000, 'gold': 5000, 'iron': 2500}
{'wood': 62, 'wheat': 62, 'gold': 62, 'iron': 62}
 
{'wood': 100, 'wheat': 1, 'gold': 0, 'iron': 0}
{'wood': 100, 'wheat': 1, 'gold': 0, 'iron': 0}
 
{'wood': 0, 'wheat': 0, 'gold': 50000, 'iron': 0}
{'wood': 0, 'wheat': 0, 'gold': 250, 'iron': 0}