Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active June 24, 2021 04:44
Show Gist options
  • Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
Save qmmr/ca6896428b4f9378d738ea8933fc048b to your computer and use it in GitHub Desktop.
Python Homework Assignment #4: Lists
"""
pirple.com/python
Homework Assignment #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 = []
myLeftovers = []
def addToList(thing):
if thing in myUniqueList:
addToLeftovers(thing)
return False
else:
myUniqueList.append(thing)
return True
def addToLeftovers(thing):
myLeftovers.append(thing)
# Test the addToList function
print(myUniqueList) # []
print(addToList("dog")) # Returns True
print(myUniqueList) # ['dog']
print(myLeftovers) # []
# Adding the element that already exists
print(addToList("dog")) # Returns False
print(myUniqueList) # ['dog']
print(myLeftovers) # ['dog']
# Adding a new element
print(addToList("cat")) # Returns True
print(myUniqueList) # ['dog', 'cat']
print(myLeftovers) # ['dog']
@anonymousincognito
Copy link

Thank you, I don't know the word "in" so I can't do the homework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment