Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. """
  2. pirple.com/python
  3.  
  4. Homework Assignment #4: Lists
  5.  
  6. Create a global variable called myUniqueList. It should be an empty list to start.
  7. Next, create a function that allows you to add things to that list.
  8. Anything that's passed to this function should get added to myUniqueList,
  9. unless its value already exists in myUniqueList.
  10. If the value doesn't exist already it should be added and the function should return True.
  11. If the value does exist, it should not be added, and the function should return False;
  12. Finally, add some code below your function that tests it out.
  13. It should add a few different elements, showcasing the different scenarios,
  14. and then finally it should print the value of myUniqueList to show that it worked.
  15.  
  16. Extra Credit:
  17. Add another function that pushes all the rejected inputs into a separate global array called myLeftovers.
  18. If someone tries to add a value to myUniqueList but it's rejected (for non-uniqueness),
  19. it should get added to myLeftovers instead.
  20. """
  21.  
  22. myUniqueList = []
  23. myLeftovers = []
  24.  
  25. def addToList(thing):
  26. if thing in myUniqueList:
  27. addToLeftovers(thing)
  28. return False
  29. else:
  30. myUniqueList.append(thing)
  31. return True
  32.  
  33. def addToLeftovers(thing):
  34. myLeftovers.append(thing)
  35.  
  36. # Test the addToList function
  37. print(myUniqueList) # []
  38. print(addToList("dog")) # Returns True
  39. print(myUniqueList) # ['dog']
  40. print(myLeftovers) # []
  41. # Adding the element that already exists
  42. print(addToList("dog")) # Returns False
  43. print(myUniqueList) # ['dog']
  44. print(myLeftovers) # ['dog']
  45. # Adding a new element
  46. print(addToList("cat")) # Returns True
  47. print(myUniqueList) # ['dog', 'cat']
  48. print(myLeftovers) # ['dog']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement