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

I am having problems executing the __str__ method as it gives me an error. It pr

ID: 3742216 • Letter: I

Question

I am having problems executing the __str__ method as it gives me an error. It prints my message but gives me an error, I tried replacing print with a return but then it wouldn't print my message. Is there any way to fix this? I have attached my entire code below if that helps with any questions you have about it.

class Diset(object):

"""A disjoint-set data structure is a collection of disjoint dynamic sets"""

# Diset supports Makeset, Findset, Addtoset, and Union methods

def __init__(self):

"""Create an empty dictionary to store keys and values"""

self.dset = {}

def Makeset(self, rep):

"""Creates a key-value pair with rep being the key to an empty set which represents the potential values"""

#If rep is a number, processes number regularly

try:

int(rep)

# If the rep is cointained in dictionary's list of keys, error message is produced

if rep in self.dset.keys():

print("There is already a set with representative" + str(rep))

# If not creates a key-value pair with rep being the key and an empty list to cointain values

else:

self.dset.setdefault(rep, [])

# If rep is not a number, does not process and prints error message

except ValueError:

print(str(rep) + " is not a number, try again")

def Addtoset(self,rep,num):

"""Adds a value to the empty set of the matching key represented by rep"""

#If rep is a number, processes number regularly

try:

n = int(num)

# If repkey is in the dictionary decision path is created

if rep in self.dset.keys():

# If the num alue is already in the repkey values list, prints error message

if num in [x for v in self.dset.values() for x in v]:

print(str(n) + " is already in the Diset")

# If the num alue is not already in the repkey values list, processes normally

else:

# If the rep is already in the value list, it will only append num alue to list

if rep in [x for v in self.dset.values() for x in v]:

self.dset.setdefault(rep, []).append(num)

# If the rep is not already in the value list, it will append first the repkey and then num alue to list

else:

self.dset.setdefault(rep, []).append(rep)

self.dset.setdefault(rep, []).append(num)

#If repkey is not in dictionary does not process and tells user to create set with that repkey first

else:

print(str(rep) + " is not a rep in dictionary, create representative first")

# If rep is not a number, does not process and prints error message

except ValueError:

print(str(num) + " is not a number, try again")

def Findset(self,num):

"""Finds the set where num alue is cointained and returns the repkey"""

# Searches the dictionary's value list for the provided num

# If the num alue is in the value list prints the rep number for that value list

if num in [x for v in self.dset.values() for x in v]:

print([k for k, v in self.dset.items() if num in v])

# If the num alue is not in the dictionary's value list, prints error message

else:

print(str(num) + " is not in dictionary")

def Union(self,rep1, rep2):

"""Joins two sets based on their respective repkey, keeps the first set and appends second set's values to it and then deletes

second key-value pair"""

# If the provided paramters for rep1 and rep2 are cointained in the dictionary's keys will loop through the

# values cointained in rep2's values list and append those value to rep1's value list

if rep1 in self.dset.keys() and rep2 in self.dset.keys():

for i in self.dset[rep2]:

self.dset[rep1].append(i)

#Deletes rep2's key-value pair

self.dset.pop(rep2)

# If the provided parameters for rep1 and rep2 are not cointained in the dicitonary's keys will print

# error message

else:

print(str(rep1) + " and "+ str(rep2) +" are not rep values, try again")

def __str__(self):

"""Returns a string representation of self"""

print("The content of the diset: ")

for i in self.dset.values():

st= str(i) + ": representative is " + str(i[0])

print(st)

S = Diset()

for i in [3,5,8,13]:

S.Makeset(i)

for i in [1,2,4]:

S.Addtoset(3,i)

for i in [6]:

S.Addtoset(5,i)

for i in [7,9,11,12,14]:

S.Addtoset(8,i)

for i in [10,15]:

S.Addtoset(13,i)

78 def _str__(self): 79 80 81 82 83 84 85 86SDiset() 87 for i in [3,5,8,13]: 88 S.Makeset(i) 89 for i in [1,2,4]: 90S.Addtoset(3,i) 91 for i in [6]: 92S. Addtoset(5,i) 93 for i in [7,9,11,12,14]: """Returns a string representation of self" print("The content of the diset: ") fori in self.dset.values( st: str(i) print (st) ": representative is " str(i[0]) + + In C116]: print (S) The content of the diset: [8, 7, 9, 11, 12, 141: representative is 8 [3, 1, 2, 4]: representative is 3 [5, 6]: representative is 5 13, 10, 15]: representative is 13 TypeError Traceback (most recent call last) in O 1 print(S) TypeError: __str_returned non-string (type NoneType) In [1171:

Explanation / Answer

In Python __str__ needs to return a string. So Please change the definition to

def __str__(self):
  st = "The content of the diset: "

for i in self.dset.values():
  st= str(i) + ": representative is " + str(i[0])

#Return is Outside the for loop

return st;




Let me know if there is still any error. If yes comment.

Thanks