Home > Net >  Google foo.bar hiring challenge skipping work
Google foo.bar hiring challenge skipping work

Time:05-24

This morning I started Google's foo.bar challenge. The quest was comparing 2 lists and returning non-same values.

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.

So I wrote this code;

x = [13, 5, 6, 2, 5]
y = [5, 2, 5, 13]

list_difference1 = [item for item in x if item not in y]
list_difference2 = [item for item in y if item not in x]


list_difference = list_difference2   list_difference1
list_difference

output: [6]

This is what it looks like in the script;

def solution(x, y):
    list_difference1 = [item for item in x if item not in y]
    list_difference2 = [item for item in y if item not in x]

    list_difference = list_difference2   list_difference1

    return list_difference

I can't understand what I have done wrong.

CodePudding user response:

You're supposed to return 6, not [6].

CodePudding user response:

Use sets, which are faster than lists:

x = [13, 5, 6, 2, 5]
y = [5, 2, 5, 13]

def solution(x, y):
    return list(set(x) ^ set (y))

print(solution(x, y))
# [6]

Otherwise, you code seems correct (just not as efficient):

def solution2(x, y):
    list_difference1 = [item for item in x if item not in y]
    list_difference2 = [item for item in y if item not in x]
    list_difference = list_difference2   list_difference1
    return list_difference

print(solution2(x, y))
# [6]
  • Related