电影院售票
电影院今天售票员全都放假。只能依靠电影院自动售票机进行购票,单张票价为25元。而售票机内没有钱,因此只能靠收入的钱去找零。售票机只能单张销售。
现有的货币面值为100,50,25三种。现在有n个人需要买票,且排队顺序不许变,请你根据他们手中的钱计算出,售票机能否进行找零。可以则返回yes,不可以则返回no
思路:
如果是25元的则可以直接购买成功
如果是50元,则需要拿出一张25元,存一张50元
如果是100元则需要拿出3张25元,存100元或者拿出一张50元和一张25元
'''
给出一段测试代码,避免你们自己去兑答案了。
以下代码保存为test.py,后import可用
'''
def assert_equals(func, target):
if func == target:
print('Success!')
else:
print('Fail!{0} not equals {1}'.format(func, target))
import test
def tickets(people):
#用于存放影院收到的钱
sale = []
for i in people:
#如果是25元的则可以直接购买成功
if i == 25:
sale.append(i)
#如果是50元,则需要拿出一张25元,存一张50元
if i == 50:
if sale.count(25)>=1:
sale.remove(25)
sale.append(i)
else:
return "NO"
#如果是100元则需要拿出3张25元,存100元或者拿出一张50元和一张25元
if i == 100:
if sale.count(50)>= 1 and sale.count(25)>=1:
sale.remove(50)
sale.remove(25)
sale.append(i)
elif sale.count(25)>=3:
sale.remove(25)
sale.remove(25)
sale.remove(25)
sale.append(i)
else:
return "NO"
return "YES"
test.assert_equals(tickets([25, 25, 50]), 'YES')
test.assert_equals(tickets([25, 100]), 'NO')
test.assert_equals(tickets([25, 25, 25, 25, 25, 25, 25, 25, 25, 25]), 'YES')
test.assert_equals(tickets([50, 50, 50, 50, 50, 50, 50, 50, 50, 50]), 'NO')
test.assert_equals(tickets([100, 100, 100, 100, 100, 100, 100, 100, 100, 100]), 'NO')
test.assert_equals(tickets([25, 25, 25, 25, 50, 100, 50]), 'YES')
test.assert_equals(tickets([50, 100, 100]), 'NO')
test.assert_equals(tickets([25, 25, 100]), 'NO')
test.assert_equals(tickets([25, 25, 50]), 'YES')
test.assert_equals(tickets([25, 25, 25, 25, 25, 25, 25, 50, 50, 50, 100, 100, 100, 100]),'NO')
test.assert_equals(tickets([25, 100]), 'NO')
test.assert_equals(tickets([50, 50, 100]), 'NO')
test.assert_equals(tickets([25, 50, 100]), 'NO')
test.assert_equals(tickets([25, 25, 50, 50, 100]), 'NO')
test.assert_equals(tickets([100, 50, 25, 25]), 'NO')
结果:
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
Success!
字符串组成列表strarr最长的连续的字符串组合
'''
给你一个字符串组成列表strarr,给你一个整数k。
你的任务是找出由k个字符组成的最长的连续的字符串组合。
例子:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
n是列表的长度,如果if n = 0 or k > n or k <= 0 return ""
'''
'''
解题思路:
1.新建一个列表存放原字符串列表中每个字符的长度
2.通过max函数和index函数找到最长字符串的索引值
3.同时删除两个列表最长字符串的索引值再次循环找到最长字符串的索引值
'''
#请将以下代码存为test.py用于检验答案。
def assert_equals(func, target, *args):
if func == target:
print('Success!')
else:
print('Fail!{0} not equals {1}'.format(func, target))
print(*args)
def longest_consec(list,k):
#1.新建一个列表存放原字符串列表中每个字符的长度
listSort = []
str=""
if len(list) ==0 or k > len(list) or k <=0:
return ""
else:
for i in list:
#1.新建一个列表存放原字符串列表中每个字符的长度
listSort.append(len(i))
for j in range(k):
#通过max函数和index函数找到最长字符串的索引值
index = listSort.index(max(listSort))
str += list[listSort.index(max(listSort))]
#同时删除两个列表最长字符串的索引值对应的字符
listSort.remove(max(listSort))
list.remove(list[index])
return str
assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], 2), "abigailtheta")
assert_equals(longest_consec(["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"], 1), "oocccffuucccjjjkkkjyyyeehh")
assert_equals(longest_consec([], 3), "")
assert_equals(longest_consec(["itvayloxrp","wkppqsztdkmvcuwvereiupccauycnjutlv","vweqilsfytihvrzlaodfixoyxvyuyvgpck"], 2), "wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck")
assert_equals(longest_consec(["wlwsasphmxx","owiaxujylentrklctozmymu","wpgozvxxiu"], 2), "wlwsasphmxxowiaxujylentrklctozmymu")
assert_equals(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas"], -2), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 3), "ixoyx3452zzzzzzzzzzzz")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 15), "")
assert_equals(longest_consec(["it","wkppv","ixoyx", "3452", "zzzzzzzzzzzz"], 0), "")
结果:
Success!
Success!
Success!
Success!
Fail!owiaxujylentrklctozmymuwlwsasphmxx not equals wlwsasphmxxowiaxujylentrklctozmymu
Success!
Fail!zzzzzzzzzzzzwkppvixoyx not equals ixoyx3452zzzzzzzzzzzz
Success!
Success!
题目来源:
https://fishc.com.cn/forum.php?mod=viewthread&tid=86360&ctid=588