Last active
January 24, 2019 15:05
-
-
Save flavienbwk/64f8b602b102426a426247f7205a0668 to your computer and use it in GitHub Desktop.
Coding interview problem : returns the same numbers present in each given list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find the numbers that appear in all arrays. | |
# Return them as a list. | |
# | |
# Input : | |
# [2, 6, 9, 11, 13, 17] | |
# [3, 6, 7, 10, 13, 18] | |
# [4, 5, 6, 9, 11, 13] | |
# | |
# Output must be : | |
# [6, 13] | |
def intersectValues(lists): | |
k = len(lists) | |
hashtable = {} | |
output = [] | |
for lc in range(0, len(lists)): | |
list = lists[lc] | |
for i in range(0, len(list)): | |
number = list[i] | |
if number not in hashtable: | |
hashtable[number] = [] | |
for a in range(0, k): | |
hashtable[number].append(1 if a == lc else 0) | |
else: | |
if hashtable[number][lc] == 0: | |
hashtable[number][lc] = 1 | |
for number in hashtable: | |
if min(hashtable[number]) == 1: | |
output.append(number) | |
return output | |
print intersectValues([[2, 6, 9, 11, 13, 17], [3, 6, 7, 10, 13, 18], [4, 5, 6, 9, 11, 13]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment