Google Foobar 挑战奴才工作任务

Google Foobar challenge Minion Work Assignments

我见过很多这样的 google foobar 挑战,真的没想到我会被邀请。我想问第一个问题。总之要求是这样的:

'''Write a function called solution(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, solution(data, n) would return the list [5, 15, 7] because 10 occurs 
twice, and thus was removed from the list entirely.'''

这是我的代码:

def solution(data, n):
    data_new = []
    if len(data) < 100:
        for d in data:
            if n <= 1:
                if data.count(d) > n:
                    pass
                elif data.count(d) == n:
                    data_new.append(d)
            elif n > 1:
                if data.count(d) >= n:
                    pass
                elif data.count(d) < n:
                    data_new.append(d)
    elif len(data) > 100:
        print('too much')
    print(data_new)

solution([1, 2, 2, 3, 3, 3, 4, 5, 5], 1)
output: 1,4

这在本地有效,无论我输入什么列表,它都能正常工作。但是当我得到它来验证它没有完成测试。我当然可以在线搜索找到答案,但这不是我想要的。我在代码中哪里弄错了?

编辑 这是上面的修改版本:

def solution(data, n):
    data_new = []
    if len(data) < 100:
        for d in data:
            if n <= 1:
                if data.count(d) > n:
                    pass
                elif data.count(d) <= n:
                    data_new.append(d)
            elif n > 1:
                if data.count(d) > n:
                    pass
                elif data.count(d) <= n:
                    data_new.append(d)
    return data_new

仔细阅读说明:

The returned list should...

您打印出结果列表,但您从未 return 它。

我已经尝试 google foobar 很多次了,所以我是凭经验写这个答案的。 您的代码的问题是它 returns 作为列表回答但答案应该是 1,4 不是 [1,4] 或类似的东西。 我知道这已经晚了,但这是我的第一个答案(抱歉变量名不好)。

def solution(data, n):
    lis=[]
    lis2=[]
    a=''
    for i in data:
        if i not in lis2 and data.count(i)<=n and n>0:
            lis2.append(str(i))
            lis2.append(',')
        else:
            lis.append(i)
            break
    for i in lis2:
       a+=i
    return a[:-1]

这个函数returns正确的输出。

为什么需要检查 n <= 1 或 n > 1?这应该是一样的:

    data_new = []
    for d in data:
        if data.count(d) <= n:
            data_new.append(d)
    return data_new

同时我不知道为什么这个 java 解决方案即使对于测试用例也失败了

data = {1,2,2,3,3,3,4,5,5};
n = 1;

在我的本地它给出了绝对正确的答案。这不应该是导入问题,因为 9 个测试用例中有两个通过了。 有人知道吗?

public static int[] solution(int[] data, int n) {
        Map<Integer, Integer> freqmap = new HashMap();
        for(int d : data)
            freqmap.put(d, freqmap.getOrDefault(d, 0)+1);
        List<Integer> res = new ArrayList();
        for(int d : data)
        {
            if(freqmap.get(d) <= n)
                res.add(d);
        }
        return res.stream().mapToInt(i->i).toArray();
    }

这是我的解决方案

def solution(data, n): 
        return [i for i in data if data.count(i) <= n]

这对我有用,

def solution(data, n):
    counter = {}
    for i in range(len(data)):
        counter[data[i]] = counter.get(data[i], 0) + 1
    remove = []
    for a, b in counter.items():
        if(b > n):
            for i in range(b):
                data.remove()
    return data

data = [5, 10, 15, 10, 7] # replace this with any other list
n = 1 # replace this with any other integer
print(solution(data, n))

此解决方案通过了 9 项测试中的 7 项。 2 个隐藏测试仍然失败。

def solution(data, n):
    from collections import OrderedDict
    dict1 = OrderedDict()

    for ele in data:
        dict1[ele] = dict1.get(ele,0)+1

    new_list = [k for k,v in dict1.items() if v<=n]
    return new_list