-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_main.py
More file actions
46 lines (32 loc) · 1.58 KB
/
04_main.py
File metadata and controls
46 lines (32 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
Python is Easy Homework #4: Lists
Create a global variable called myUniqueList. It should be an empty list to start.
Next, create a function that allows you to add things to that list.
Anything that's passed to this function should get added to myUniqueList, unless its value already exists in myUniqueList.
If the value doesn't exist already, it should be added and the function should return True.
If the value does exist, it should not be added, and the function should return False;
Finally, add some code below your function that tests it out. It should add a few different elements,
showcasing the different scenarios, and then finally it should print the value of myUniqueList to show that it worked.
Extra Credit:
Add another function that pushes all the rejected inputs into a separate global array called myLeftovers.
If someone tries to add a value to myUniqueList but it's rejected (for non-uniqueness), it should get added to myLeftovers instead.
'''
myUniqueList = [] #Global
myLeftOvers = [] #Global
def addList(x):
if x not in myUniqueList:
myUniqueList.append(x)
return True
else:
addReject(x)
return False
def addReject(x):
myLeftOvers.append(x)
#Tests
print(addList("Hi"), myUniqueList, myLeftOvers)
print(addList("Bye"), myUniqueList, myLeftOvers)
print(addList("Hi"), myUniqueList, myLeftOvers)
print(addList(5), myUniqueList, myLeftOvers)
print(addList([2, 2]), myUniqueList, myLeftOvers)
print(addList(5), myUniqueList, myLeftOvers)
print(addList([2, 2]), myUniqueList, myLeftOvers)