Google Foobar challenge Minion Work Assignments

Question:

I’ve seen a lot of these google foobar challenges and really didn’t think I would be invited. I want to ask about the first question. All in all the requirement is this:

'''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.'''

And this is my code:

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

This works locally and whatever list I feed it, works as it should. But when I get it to verify it doesn’t get the tests done. I sure can search online to find the answer, but it’s not what I want. Where am I mistaken in the code?

EDIT
This is reworked version of the above:

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
Asked By: Tony

||

Answers:

Read the instructions carefully:

The returned list should…

You print out the resulting list, but you never return it.

Answered By: Code-Apprentice

I have attempted google foobar many times so i am writing this answer by experience.
the problem with your code is that it returns answer as a list but answer should be 1,4
not [1,4] or something like that.
I know this is late but this is my first answer(sorry for bad variable names).

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]

this function returns the correct output.

Why do you need to check if n <= 1 or n > 1? This should work the same:

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

Meanwhile I have no idea why this java solution failed even for the test case

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

In my local it gives absolutely correct answer. It should not be the import issue since there are two passed out of the 9 tests cases.
Anyone has some idea?

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();
    }
Answered By: photoelectric

Here is my solution

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

This did worked for me,

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))

Answered By: Mithson

7 out of 9 tests passed with this solution. 2 hidden tests still failing.

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
Answered By: Tarun

My answer right now. just created a new list appending i if it is not greater than n:

def solution(data, n):
ndata = []
for i in data:
    c = data.count(i)
    if c <= n:
        ndata.append(i)
return ndata
Answered By: FelixOakz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.