谷歌foobar奴才工转移挑战 [英] Google foobar minion labor shifts challenge

查看:34
本文介绍了谷歌foobar奴才工转移挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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奴才工转移挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆