'Google foobar minion labor shifts challenge
I have a Google foobar challenge:
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.
And this was my answer:
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
But it didn't pass the test.
Solution 1:[1]
Even shorter
def? ?solution(data,? ?n):?
return [x for x in data if data.count(x) <= n]
Solution 2:[2]
you can try this one
def solution(data, n):
# Your code here
if len(data) < 100:
list = filter(lambda a: data.count(a)<= n, data)
return(list)
Solution 3:[3]
This is an old question but has not yet received the correct answer. Before looking into the complexity your answer is incorrect. You have to be careful when you want to modify the data structure the loop is dependent on. In your case
for i in data:
if i == _id:
data.remove(_id)
else:
continue
will not delete 'id' from 'data' in all cases, because you are changing the length of 'data' along the process and you will skip over some occurrence of 'id' in 'data' when you do this. Instead what you can do is store the index of occurrence of 'id' in the first for loop then delete them in the reverse order in a second 'for loop'. In your algorithm, the above bug occurs twice (in both of the 'for loops').
Solution 4:[4]
def? ?answer(data=[],? ?n=0):
d={}
for i in data:
if i not in d:
d[i] = 1
else:
d[i] = d[i]+1
target=[]
for i in d.keys():
if d[i] > n:
target.append(i) #get those numbers appear counts more than n
result=[x for x in data if x not in target]
return result
Solution 5:[5]
I do not know if it is the best way to do it. But I had reversed the list to do it. My code is as follows:
def solution(data, n):
# Your code here
data.reverse()
for val in data:
num=data.count(val)
if(num>n):
for i in range(num):
data.remove(val)
data.reverse()
return(data)
Solution 6:[6]
def main():
data = [2, 3, 2, 5, 6, 2, 5, 1, 8, 9, 9, -9]
n = 3
res = answer(data, n)
print(res)
def answer(data, n):
store = []
for x in range(len(data)):
count = 0
for y in data:
if y == data[x]:
count += 1
if count > n:
if data[x] not in store:
store.append(data[x])
for z in store:
for r in reversed(range(len(data))):
if data[r] == z:
data.pop(r)
return data
main()
Solution 7:[7]
This is my solution - Duplicate steps will not be traversed
def solution(data, n):
flag = True
counter = 0
length = len(data)
while flag:
repeats = data.count(data[counter])
if repeats > n:
data = [i for i in data if i != data[counter]]
length = length - repeats
counter = 0
else:
counter = counter + 1
if counter == length:
flag = False
print(','.join(map(str,data)))
break
Solution 8:[8]
I recently ran into this and saw that there are specific conditions that have to be met, such as if there are no matching total numbers that equal n then the entire list is returned. I figured it out this way:
Updated to reflect correct output for the OP's ask
def answer(data, n):
#Append all match count cases in data to bl and eliminate duplicates.
#For example [5, 10, 15, 10, 7], n = 2, bl = [5, 7, 15].
bl = list(sorted(x for x in data if data.count(x) != n))
#Returns a statement if the list has more than 100 integers within.
if len(data) > 100:
return "\nToo many data integers! Data must be below 100 integers."
#Returns empty list if n = 0.
if n == 0:
return bl
#Return list if duplicates were found, otherwise return the original list.
if bl:
return bl
else:
return data
Input:
answer([0, 2, 2, 1, 1, 3, 3, 3, 4, 5, 5], 2)
Output:
[0, 3, 3, 3, 4]
Solution 9:[9]
Here is my solution. It has passed all of the test cases.
def solution(data,n):
workers = data[:99]
for i in workers:
num_count = data.count(i)
if num_count > n:
for t in range(num_count):
data.remove(i)
else:
continue
return data
Solution 10:[10]
You can answer this by using list comprehensions in one line (not optimal for larger data set):
def answer(data, n):
return [x for x in data if x not in [y for y in set(data) if data.count(y) > n]]
Normal way:
def answer(data, n):
data_set = set(data)
for d in data_set:
if data.count(d) > n:
data = filter(lambda a: a != d, data)
return data
Solution 11:[11]
Test case failing data =[1, 2, 2, 3, 3, 3, 4, 5, 5],n=1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow