Solve the following problem in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Solve the following problem in python

Take two lists, say for example these two: a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

7th Jun 2017, 6:26 PM
Masquerade
Masquerade - avatar
6 Answers
+ 2
it's quite simple. first it has 3 lists. the 2 you want to check, and an empty one we will put the common numbers in two = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] one = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] both=[] Then it has 2 blocks of code. it only uses 1 of them. it checks the length of the 2 lists. if the second list is bigger it runs the first block. inside it has a for loop. it loops thru the first list because it is smallest. That will make it more efficient. being the shorter of the lists it has less possible common numbers to check. So for each number in the first list it checks if that number is in The second list. if it is, it checks if it is already in the common list. if it is in both list 1 and list 2, but not already in the common list it adds it to the common list. Then the loop simply checks the next number in the one list. if len(one) < len(two): for i in one: if i in two and i not in both: both.append(i) if list one is bigger, it runs this block instead. if len(one) > len(two): it starts a for loop to check all the numbers in the smaller list. for i in two: it checks if each of those numbers are in the bigger list and if they exist already in the common list if i in one and i not in both: if it is in both lists but not yet in the common list it adds it to the common list both.append(i) Then it prints the common list print(both)
8th Jun 2017, 9:51 AM
LordHill
LordHill - avatar
+ 3
Here. Change the 2 lists at the top as you see fit, it will tell you the numbers they both contain.. Let me know what grade you get. two = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] one = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] both=[] if len(one) < len(two): for i in one: if i in two and i not in both: both.append(i) if len(one) > len(two): for i in two: if i in one and i not in both: both.append(i) print(both)
7th Jun 2017, 7:57 PM
LordHill
LordHill - avatar
+ 1
I completely got it. Thanks a lot !
8th Jun 2017, 11:00 AM
Masquerade
Masquerade - avatar
0
Thankyou so much for helping me. Can you also explain me how this code works please. I am a bit confused.
8th Jun 2017, 3:53 AM
Masquerade
Masquerade - avatar
0
Also, Check this a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] common=[] a = set(a) b = set(b) for x in a: if x in b: common.append(x) print(common)
7th Apr 2021, 5:58 AM
Muhammad Zaryab Rafique
Muhammad Zaryab Rafique - avatar
0
Check it, it works correctly a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] c = [x for x in a for y in b if x == y] print (set(c))
7th Apr 2021, 6:01 AM
Muhammad Zaryab Rafique
Muhammad Zaryab Rafique - avatar