Advertisement
Guest User

Untitled

a guest
Jan 8th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1.  
  2. class Bar:
  3.     pass
  4.  
  5. class Choice:
  6.     pass
  7.  
  8. #file with input data
  9. tmpFile = open("Pyfood.txt", "r")
  10.  
  11. barsKinds, BajtazarsMoney = [int(x) for x in tmpFile.readline().split(" ")]
  12. barsPrices = [int(x) for x in tmpFile.readline().split(" ")]
  13. barsAmount = [int(x) for x in tmpFile.readline().split(" ")]
  14.  
  15. barsList = []
  16.  
  17. for x in range(barsKinds):
  18.     newBar = Bar()
  19.     newBar.price = barsPrices[x]
  20.     newBar.amount = barsAmount[x]
  21.     barsList.append(newBar)
  22.  
  23.  
  24. def findBestOneToBuy(barsList):
  25.     """ Finds best bar to buy (with biggest profit) and returns this as a structure Choice """
  26.     bestChoice = Choice()
  27.     bestChoice.idx = 0
  28.     bestChoice.profit = 0
  29.  
  30.     for x in range(len(barsList)): #check every possible condition
  31.         tmpChoice = Choice()
  32.         tmpChoice.idx = x
  33.         tmpChoice.profit = 0
  34.  
  35.         if (barsList[x].amount == 0 or
  36.             barsList[x].price > BajtazarsMoney) : continue
  37.  
  38.         moneySaved = 0
  39.         moneySpent = 0
  40.        
  41.         for i in range(x):  # for every possible condition count profit
  42.             print(i)
  43.             if barsList[i].amount > 0 : moneySaved += barsList[i].price
  44.  
  45.         moneySaved += barsList[x].price
  46.         moneySpent = barsList[x].price
  47.         tmpChoice.profit = moneySaved/moneySpent
  48.  
  49.         if tmpChoice.profit > bestChoice.profit : bestChoice = tmpChoice
  50.         print(tmpChoice.profit)
  51.  
  52.     return bestChoice
  53.  
  54.  
  55. def buyBar(idx, barsList):
  56.     """ Takes idx of bar you want to buy and decreases amounts of
  57.    bars in barsList. returns price to pay and totalValue of stolen goods
  58.    as a tuple """
  59.     totalValue = 0
  60.     for bar in range(idx):
  61.         if barsList[bar].amount >0 :
  62.             barsList[bar].amount -= 1
  63.             totalValue += barsList[bar].price
  64.    
  65.     totalValue += barsList[idx].price
  66.     barsList[idx].amount -= 1
  67.     return barsList[idx].price, totalValue
  68.  
  69.  
  70. moneyGained = 0
  71. while True:
  72.     barToBuy = findBestOneToBuy(barsList)
  73.     print("chosen", barToBuy.idx+1)
  74.     if barsList[barToBuy.idx].price > BajtazarsMoney : break
  75.  
  76.     moneySpent, tmpMoney = buyBar(barToBuy.idx, barsList)
  77.    
  78.     BajtazarsMoney -= moneySpent
  79.     moneyGained += tmpMoney
  80.  
  81. print(moneyGained)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement