Skip to content

Commit d15eb76

Browse files
authored
Add files via upload
1 parent 770cfb8 commit d15eb76

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lists_py.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Create a global variable called myUniqueList. It should be an empty list to
3+
start.
4+
5+
Next, create a function that allows you to add things to that list.
6+
Anything that's passed to this function should get added to myUniqueList,
7+
unless its value already exists in myUniqueList. If the value doesn't exist
8+
already, it should be added and the function should return True. If the value
9+
does exist, it should not be added, and the function should return False;
10+
Finally, add some code below your function that tests it out. It should
11+
add a few different elements, showcasing the different scenarios, and then
12+
finally it should print the value of myUniqueList to show that it worked.
13+
14+
Extra credit:
15+
Add another function that pushes all the rejected inputs into a separate global
16+
array called myLeftovers. If someone tries to add a value to myUniqueList but
17+
it's rejected (for non-uniqueness), it should get added to myLeftovers instead.
18+
"""
19+
20+
myUniqueList=[] # Global variable: Empty list.
21+
myLeftOvers=[] # Global variable: Empty list for rejected inputs.
22+
23+
def AddToList(newthing): # Global function: Adds things to list.
24+
if newthing in myUniqueList: # If something that already exists in myUniqueList is introduced, print False.
25+
myLeftOvers.append(newthing)
26+
return False
27+
else:
28+
myUniqueList.append(newthing) # If something new is presented to myUniqueList, print True.
29+
return True
30+
31+
print(AddToList("1")) # Add 1 to list.
32+
print(AddToList("2"))
33+
print(AddToList("1")) # Add 1 again, returns false since value already exists.
34+
print(myUniqueList) # Prints everything in my global variable.

0 commit comments

Comments
 (0)