我在CodeWars中發現了以下代碼,並且也寫了說明。它說我的代碼通過了8個測試用例,而不是第9個。有人可以給我一個想法什麼是錯的,或者我應該怎麼做呢?我只能訪問我已經回答的四個測試用例。 https://www.codewars.com/kata/555615a77ebc7c2c8a0000b8/discuss#label-issue不知道哪個測試用例失敗我的代碼
'''
The new "Avengers" movie has just been released! There are a lot of people at the cinema
box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill.
A "Avengers" ticket costs 25 dollars. Vasya is currently working as a clerk. He wants to
sell a ticket to every single person in this line. Can Vasya sell a ticket to each person
and give the change if he initially has no money and sells the tickets strictly in the
order people follow in the line? Return YES, if Vasya can sell a ticket to each person
and give the change. Otherwise return NO.
Examples:
### Python ###
tickets([25, 25, 50]) # => YES
tickets([25, 100])
# => NO. Vasya will not have enough money to give change to 100 dollars
'''
def tickets(people):
sum = 0
for p in people:
if p < 25:
return 'NO'
if p == 25:
sum += p
elif p > 25:
if (sum - p) <0 :
return 'NO'
else:
sum += p
return 'YES'
print(tickets([25, 25, 50])) #YES
print(tickets([25, 100])) #NO
print(tickets([25, 25, 50, 50, 50])) #YES
print(tickets([25, 25, 25, 25, 50, 100, 50])) #YES
您的最終陳述是問題所在。總和增加了25,而不是p。她確實會回覆變化,不是嗎? –
我的當前程序通過了代碼中顯示的測試。你能想到一個測試用例,我的代碼會失敗嗎? –
好吧,只是實現沒有其他條款仍然我的四個測試案例顯示通過,但我不通過未知的第9個測試用例在codewars @ Ev.Kounis –