【Kata Daily 190906】Vasya - Clerk(职员)
题目:
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 single100, 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.
-----------------------------------------------------------------------------------------------------------
找零钱的题目,客人中会出现25,50,100面值的钞票,电影票25块钱。问一条队中,店员能不能卖完电影票而且不用另外找零钱。
解题办法:
看下网友的解题思路:
def tickets(people): n25, n50, n100 = 0, 0, 0 for i in people: if i == 25: n25 += 1 elif i == 50: n25 -= 1 n50 += 1 elif i == 100 and n50>0: n25 -= 1 n50 -= 1 elif i == 100 and n50==0: n25 -= 3 if n25<0 or n50<0: return "NO" else: return "YES"
解读:利用钱包中钱的个数来判断是否能够找得开零钱。并对100元的情况进行了分别处理,即优先使用50元来找零。
还有一种没有通过测试的算法:
def tickets(people): sum, change,a = 0, 0, "" for i in people: sum += 25 change = i - 25 sum -= change if sum < 0: a = "NO" else: a = "YES" return a
疑惑:举不出例子来说明该算法的错误之处。另外此段算法给到你,你该如何编写测试用例?

浙公网安备 33010602011771号