Skip to content

Commit 96b0498

Browse files
authored
Create unit1_ex1.3.2.py
1 parent e4f13fc commit 96b0498

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

unit1_ex1.3.2.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# exercise 1.3.1 from unit 1
2+
'''
3+
Write a function called intersection defined as follows:
4+
5+
def intersection(list_1, list_2):
6+
The function accepts two lists and returns a list of the members that are in the intersection between them. That is, members that are in both the first list and the second list. No member will appear twice in the cut list.
7+
8+
An example of running the intersection function:
9+
10+
print(intersection([1, 2, 3, 4], [8, 3, 9]))
11+
print(intersection([5, 5, 6, 6, 7, 7], [1, 5, 9, 5, 6]))
12+
[3]
13+
[5,6]
14+
Guidelines:
15+
16+
It is forbidden to use an external library, except the functools library.
17+
It is allowed to use the for loop only within a list assembly structure.
18+
The intersection function block must contain only one line of code.
19+
Do not implement additional helper functions in the code.
20+
'''
21+
22+
import functools
23+
24+
def intersection(list_1, list_2):
25+
return list(set([x for x in list_1 if x in list_2]))
26+
27+
28+
def main():
29+
print(intersection([1, 2, 3, 4], [1, 3, 9]))
30+
print(intersection([5, 5, 6, 6, 7, 7], [1, 5, 7, 7, 6]))
31+
32+
if __name__ == "__main__":
33+
main()

0 commit comments

Comments
 (0)