我有一个Google foobar挑战:
I have a Google foobar challenge:
编写一个名为answer(data,n)的函数,该函数接收少于100个整数和一个数字n的列表,并返回该列表,但将出现n次以上的所有数字完全删除.返回的列表应与原始列表保持相同的顺序-您不希望混淆那些经过精心计划的轮换!例如,如果数据为[5,10,15,10,7]并且n为1,由于10两次出现,因此answer(data,n)将返回列表[5,15,7],因此已从列表中将其删除.完整列出.
Write a function called answer(data, n) that takes in a list of less than 100 integers and a number n, and returns that same list but with all of the numbers that occur more than n times removed entirely. The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations! For instance, if data was [5, 10, 15, 10, 7] and n was 1, answer(data, n) would return the list [5, 15, 7] because 10 occurs twice, and thus was removed from the list entirely.
这是我的答案:
def answer(data=[], n=0): for id in data: num = data.count(id) if num > n: for i in data: if i == id: data.remove (id) else: continue else: continue return data但是它没有通过测试.
推荐答案更短
def solution(data, n): return [x for x in data if data.count(x) <= n]更多推荐
谷歌foobar奴才工转移挑战
发布评论