Cześć. Od razu mówię, że proszę o poświęcenie mi trochę czasu, więc jeśli ktoś nie ma ochoty pomagać to niech nie pomaga i proszę o wyrozumiałość - chcę się czegoś nauczyć. Wstawiam krótki kod w języku Python. Ponieważ dopiero się go uczę i nie znam go na wylot, to bardzo prosiłbym o jakieś wskazówki odnośnie kodu. Kod to rozwiązanie zadania z konkursu programistycznego
sprzed kilku lat stąd dziwne nazwy jak Bajtazar, bo takie imię było w poleceniu do zadania. Z góry dziękuję za poświęcony mi czas :)
treść zadania jeśli ktoś chce ("AUTOMAT"): http://amppz.mimuw.edu.pl/amppz2012-tresci-zadan.pdf
kod: http://pastebin.com/Ex34eE66
class Bar:
pass
class Choice:
pass
#file with input data
tmpFile = open("Pyfood.txt", "r")
barsKinds, BajtazarsMoney = [int(x) for x in tmpFile.readline().split(" ")]
barsPrices = [int(x) for x in tmpFile.readline().split(" ")]
barsAmount = [int(x) for x in tmpFile.readline().split(" ")]
barsList = []
for x in range(barsKinds):
newBar = Bar()
newBar.price = barsPrices[x]
newBar.amount = barsAmount[x]
barsList.append(newBar)
def findBestOneToBuy(barsList):
""" Finds best bar to buy (with biggest profit) and returns this as a structure Choice """
bestChoice = Choice()
bestChoice.idx = 0
bestChoice.profit = 0
for x in range(len(barsList)): #check every possible condition
tmpChoice = Choice()
tmpChoice.idx = x
tmpChoice.profit = 0
if (barsList[x].amount == 0 or
barsList[x].price > BajtazarsMoney) : continue
moneySaved = 0
moneySpent = 0
for i in range(x): # for every possible condition count profit
print(i)
if barsList[i].amount > 0 : moneySaved += barsList[i].price
moneySaved += barsList[x].price
moneySpent = barsList[x].price
tmpChoice.profit = moneySaved/moneySpent
if tmpChoice.profit > bestChoice.profit : bestChoice = tmpChoice
print(tmpChoice.profit)
return bestChoice
def buyBar(idx, barsList):
""" Takes idx of bar you want to buy and decreases amounts of
bars in barsList. returns price to pay and totalValue of stolen goods
as a tuple """
totalValue = 0
for bar in range(idx):
if barsList[bar].amount >0 :
barsList[bar].amount -= 1
totalValue += barsList[bar].price
totalValue += barsList[idx].price
barsList[idx].amount -= 1
return barsList[idx].price, totalValue
moneyGained = 0
while True:
barToBuy = findBestOneToBuy(barsList)
print("chosen", barToBuy.idx+1)
if barsList[barToBuy.idx].price > BajtazarsMoney : break
moneySpent, tmpMoney = buyBar(barToBuy.idx, barsList)
BajtazarsMoney -= moneySpent
moneyGained += tmpMoney
print(moneyGained)
głównie zależy mi na wskazówkach typu:
"to i to jest nieczytelne"
"tego i tamtego nie powinno się używać" etc.
Mam nadzieję, że wiecie o co mi chodzi. Niestety nie mam nikogo, kto mógłby mi na miejscu dawać takie rady, więc męczę was. Jeszcze raz dziękuję :)
wklejenie kodu do posta
- @furious programming