python - 不确定哪些测试用例未通过我的代码

标签 python testing testcase

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

最佳答案

if 语句也是错误的。想想 sum += 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'

告诉我! ☺

关于python - 不确定哪些测试用例未通过我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37962055/

相关文章:

python - 类型错误 : argument of type 'WindowsPath' is not iterable - in django python

python - python pptx 模块 add_picture 中出现未知错误

java - 如何通过 Maven 命令重新运行 TestNG 失败的测试

testing - 如何以随机顺序运行采样器/采样器集

if-statement - 如果分支覆盖测试中第一个 if 条件为真,else if 条件是否运行?

unit-testing - 使用 ActiveSupport::TestCase 进行事件管理 Controller 测试

Python glmnet "No module named _glmnet"

python - 在Python代码中嵌入VB宏

c# - MSTest 是否有相当于 NUnit 的 TestCase 的工具?

perl - 如何为 Perl 脚本编写单元测试用例