谷歌foobar奴才工转移挑战

编程入门 行业动态 更新时间:2024-10-14 00:29:26
本文介绍了谷歌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奴才工转移挑战

本文发布于:2023-11-29 11:25:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奴才   谷歌   foobar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!