Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*PYTHON PROGRAM* The code below is the UniqueList class. The _add_ just gets add

ID: 3821255 • Letter: #

Question

*PYTHON PROGRAM*

The code below is the UniqueList class. The _add_ just gets added on to the code below

~Create an __add__ method to overload the '+' operator so that it creates a third UniqueList object from two UniqueList objects by "adding" them together. ~

menu_item = 0
list = []
while menu_item != 9:
print ("--------------------")
print ("1. Print the list")
print ("2. Add a name to the list")
print ("3. Remove a name from the list")
print ("9. Quit")
menu_item = input("Pick an item from the menu: ")
if menu_item == 1:
current = 0
if len(list) > 0:
while current < len(list):
print (current, ".", list[current])
current = current + 1
else:
print ("List is empty")
elif menu_item == 2:
name = raw_input("Type in a name to add: ")
if name in list:
print("Name already in list")
else :
list.append(name)
elif menu_item == 3:
del_name = raw_input("What name would you like to remove: ")
if del_name in list:
item_number = list.index(del_name)
del list[item_number]
else:
print (del_name," was not found")
  
print ("Goodbye")

Explanation / Answer

class UniqueList:
def __init__(self):
sef.list = []
def addList(self, item):
if not item in self.list:
self.lis.append(item)
def remList(self, item):
if not item in self.list:
return
self.list.remove(item)
def __str__(self):
result = "["
if len(self.list) > 0:
result += str(self.list[0])
for i in range(1, len(self.list)):
result += ", " + str(self.list[i])
result += "]"
return result
  
def __add__(self, other):
third = UniqueList()
third.list = self.list
for item in other.list:
third.addList(item)
return third
def __add__(self, other):
third = UniqueList()
third.list = self.list
for item in other.list:
third.remList(item)
return third
def __gt__(self, other):
return sum(self.list) > sum(other.list)

# code link: https://paste.ee/p/FdwOs