2016-06-22 81 views
0

我在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 
+0

您的最終陳述是問題所在。總和增加了25,而不是p。她確實會回覆變化,不是嗎? –

+0

我的當前程序通過了代碼中顯示的測試。你能想到一個測試用例,我的代碼會失敗嗎? –

+0

好吧,只是實現沒有其他條款仍然我的四個測試案例顯示通過,但我不通過未知的第9個測試用例在codewars @ Ev.Kounis –

回答

2

if語句也是錯誤的。想想總結+ = p錯誤的測試用例[25,50,100],以及if語句錯誤的以下場景[25,50]。用下面的代碼,這兩個問題應該得到解決。

def tickets(people): 
    register = {'25s': 0, '50s': 0, '100s': 0} 
    cash_in_register = 0 
    for p in people: 
     if p < 25: 
      return 'NO' 
     elif p == 25: 
      cash_in_register += p 
      register['25s'] += 1 
     else: 
      if (p - cash_in_register) <= 25: # do you have enough money for change? 
       if p == 50 and register['25s'] >= 1: 
        register['50s'] += 1 
        register['25s'] -= 1 
        cash_in_register += 25 
       elif (p == 100 and register['50s'] >= 1 and register['25s'] >= 1): 
        register['100s'] += 1 
        register['50s'] -= 1 
        register['25s'] -= 1 
        cash_in_register += 25 
       elif (p == 100 and register['25s'] >= 3): 
        register['100s'] += 1 
        register['25s'] -= 3 
        cash_in_register += 25 
       else: 
        return 'NO' 
      else: 
       return 'NO' 
    return 'YES' 

讓我知道! ☺

+0

請先在Codewars平臺上嘗試您的代碼,然後再提交。你的代碼沒有工作 –

+0

所以我必須現在在Codewars中創建一個配置文件?它有什麼問題?你提供的4種情況仍然有效+我向你解釋你做錯了什麼並且修復了它。 –

+0

它不能告訴你問題出在哪裏! –

1

我認爲問題在於你不看你有的實際賬單。

看看測試用例[25,25,50,50,50]:它應該產生一個「NO」,但是如果我正確理解你的代碼,你的答案是「YES」。

第二個50後,有50美元的總和,但它的50美元比爾。因此,當另一位顧客以50美元入場時,收銀員不能退還25美元。

+0

非常真實..這完全改變了球賽。我將調整我的代碼。 –