Ramakanth Sharma
Ramakanth Sharma

Reputation: 19

Python test cases fails in Google Foobar challenge

I learnt python for Data Science and recently Google invited me to take up the foobar challenge. The first question is pretty simple and I am able to get the desired output on my local machine, but not even a single test case passes in foobar. Please find the question below and the code.

Given two almost identical lists of worker IDs x and y where one of the lists contains an additional ID, write a function solution(x, y) that compares the lists and returns the additional ID.

For example, given the lists x = [13, 5, 6, 2, 5] and y = [5, 2, 5, 13], the function solution(x, y) would return 6 because the list x contains the integer 6 and the list y doesn't. Given the lists x = [14, 27, 1, 4, 2, 50, 3, 1] and y = [2, 4, -4, 3, 1, 1, 14, 27, 50], the function solution(x, y) would return -4 because the list y contains the integer -4 and the list x doesn't.

Below was the first code I wrote, which worked on my local machine, but failed all test cases on foobar.

def solution(x,y):
    set1=set(x)
    set2=set(y)
    answer=list(set1^set2)
    for i in answer:
        print(i)

Upon researching, I could understand that the way foobar calls this function is : solution.solution(x,y) So I tried defining a class and the method within the class, which again works fine on my local machine, but as usual fails all test cases in foobar. Please find the different variants of my code which I tried. All the below codes work fine with the visible test cases of foobar on my local machine, but fails the same on foobar.

Please help me. I just have 2 days left to solve this question.

class solution:
    
    def __init__(self,x,y):
        self.x=x
        self.y=y
        
    def solution(x,y):
        set1=set(x)
        set2=set(y)
        answer=list(set1^set2)
        for i in answer:
            print(i)
class solution:
     
    def solution(x,y):
        set1=set(x)
        set2=set(y)
        answer=list(set1^set2)
        for i in answer:
            print(i)
class solution:
    @staticmethod
    def solution(x,y):
        set1=set(x)
        set2=set(y)
        answer=list(set1^set2)
        for i in answer:
            print(i)

Upvotes: 1

Views: 2269

Answers (2)

Amir Khan
Amir Khan

Reputation: 1

Below code works.

def solution(x, y):
    set1 = set(x)
    set2 = set(y)
    unique = list(set1 ^ set2)
    return unique[0]

Upvotes: 0

Ben Gillett
Ben Gillett

Reputation: 386

the function solution(x, y) would return 6

Reading the instructions very carefully is usually a good starting point for this sort of issue. :) I don't see a return statement in your code - there is a big difference between printing and returning!

...

return i will solve a lot of your problems

Upvotes: 2

Related Questions