Question: Python function class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = '_value', '_next' def __init__(self, value, next=None): DO NOT EDIT Initialize a

Python function

class LinkedNode: # DO NOT MODIFY THIS CLASS # __slots__ = '_value', '_next' def __init__(self, value, next=None): """ DO NOT EDIT Initialize a node :param value: value of the node :param next: pointer to the next node in the LinkedList, default is None """ self._value = value # element at the node self._next = next # reference to next node in the LinkedList def __repr__(self): """ DO NOT EDIT String representation of a node :return: string of value """ return str(self._value) __str__ = __repr__ def get_value(self): return self._value def get_next(self): return self._next def set_value(self, value): self._value = value def set_next(self, next): self._next = next # IMPLEMENT THESE FUNCTIONS - DO NOT MODIFY FUNCTION SIGNATURES # def insert(value, node=None): """ insert a new node to the end of the list that has head node :param value: the value that we are inserting :param node: the head node of the list that we are handling :return: the starting node of the linked list """ if node is None: node = LinkedNode(value) return node elif node._next is None: new_node = LinkedNode(value) node.set_next(new_node) return node else: insert(value, node._next) return node def to_string(node, result=""): """ The values should be separated by a comma and a single space, with no leading or trailing comma Return an empty string if there are no nodes in the list. Recommended Time complexity O(n2). :param node: the head node of the list that we are dealing :param result: the string we want to return :return: a string representation of the list """ if node is None: return "" else: if node._next is None: result += str(node._value) print(result) # this will print out the correct result string return result # but here it returns None else: result += str(node._value) result += ", " to_string(node._next, result)

____________________________________________________

test case:

import unittest from Recursion import insert, remove, remove_all, to_string, search, sum_list, \ count, reverse, length, list_percentage

class TestProject2(unittest.TestCase):

def test_to_string(self): list1 = insert(0) insert(1, list1) insert(2, list1) insert(3, list1) print(to_string(list1)) #it prints out None here, but in the function it prints out the right string assert to_string(list1) == "0, 1, 2, 3"

if __name__ == "__main__": unittest.main() _______________________________________________

for the function to string, it won't return the result properly. but it does print out the right string in the function.

what is the issue?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!