File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ import random
2
+ # Write a program (function!) that takes a list and returns
3
+ # a new list that contains all the elements of the first list
4
+ # minus all the duplicates.
5
+ # Extras:
6
+ # Write two different functions to do this - one using
7
+ # a loop and constructing a list, and another using sets.
8
+ # Go back and do Exercise 5 using sets, and write the
9
+ # solution for that in a different function.
10
+
11
+ def remove_dupes_via_loop (mylist ):
12
+ result = []
13
+ for i in range (len (mylist )):
14
+ found = 0
15
+ for j in range (i + 1 , len (mylist )):
16
+ if mylist [i ] == mylist [j ]:
17
+ found = 1
18
+ break
19
+ if not found :
20
+ result .append (mylist [i ])
21
+ return result
22
+
23
+
24
+ def remove_dupes_via_set (mylist ):
25
+ return list (set (mylist ))
26
+
27
+
28
+ def set_overlap (set1 , set2 ):
29
+ result = set ()
30
+ for elem in set1 :
31
+ if elem in set2 :
32
+ result .add (elem )
33
+
34
+ return result
35
+
36
+ seta = set (random .sample (range (30 ), 15 ))
37
+ setb = set (random .sample (range (30 ), 15 ))
38
+ lista = random .sample (range (30 ), 15 )
39
+
40
+ print (remove_dupes_via_loop (lista ))
41
+ print (remove_dupes_via_set (lista ))
42
+ print (set_overlap (seta , setb ))
You can’t perform that action at this time.
0 commit comments