2022-03-25

When a method has no return statement, how do I print out it's return value?

EDITED.

I am learning about Linked Lists. For each process applied by a Method, it is printed out to the console. So, adding, removing, searching (i.e, displaying the result of a search), are all streamed to stdout, but I cannot seem to do this for the insertion Method even though the insert Method is executed.

Some Methods have a return statement, while others rely on the __repr__() for conversion to string, to then be streamed to the console. The insertion Method (not mine, but a course worked example) takes two arguments and does not have a return statement. The most consistent error message I get when attempting to print is TypeError: %d format: a real number is required, not NoneType, or TypeError: not enough arguments for format string, where I have replaced %d with %s.

What I do not understand is, why I am unable to display test data for the insert Method, while I can do so for all others. The code,

#!/usr/bin/env python3

class Node:
  data = None
  next_node = None

  def __init__(self, data):
    self.data = data

  def __repr__(self):
    return "<Node data: {}>".format(self.data)

# Linked List
class LinkedList:

  def __init__(self):
    self.head = None

  def is_empty(self):
    return self.head == None  # corrected

  def size(self):
    current = self.head
    count = 0

    while current:
      count += 1
      current = current.next_node

    return count

  # Adding a node
  def add(self, data):

    new_node = Node(data)
    new_node.next_node = self.head
    self.head = new_node
    

  # Searching the List
  def search(self, key):

    current = self.head

    while current:
      if current.data == key:
        return current
      else:
        current = current.next_node
    return None
  
  # Inserting into the List
  def insert(self, data, index):
    
    if index == 0:
      self.add(data)

    if index > 0:
      new_data = Node(data)

    position = index
    current = self.head

    while position > 1:
      current = current.next_node
      position -= 1

    past_node = current
    future_node = current.next_node

    past_node.next_node = new_data
    new_data = current.next_node

  # Removing a node from the List
  def remove(self, key):
    
    current = self.head
    previous = None
    found = False

    while current and not found:
      if current.data == key and current == self.head:
        found = True
        self.head = current.next_node
      elif current.data == key:
        found = True
        previous.next_node = current.next_node

    return current

  def __repr__(self):
    
    nodes = []
    current = self.head

    while current:
      if current is self.head:
        nodes.append("[Head: {}]".format(current.data))
      elif current.next_node is None:
        nodes.append("[Tail {}]".format(current.data))
      else:
        nodes.append("[{}]".format(current.data))

      current = current.next_node
    return '-> '.join(nodes)

Test output;

l = LinkedList()
l.add(1)
l.add(2)
l.add(3)
l.add(5)
l.add(6)
l.add(7)

length = l.size()
print("Size of list: {}".format(length))  # Size of list: 6

print(l)  # [Head: 7]-> [6]-> [5]-> [3]-> [2]-> [Tail: 1]

seek = l.search(7)
print("Found: {}".format(seek))  # Found: <Node data: 7>

between = l.insert(4, 3)
if between is not None:
  print(f"Inserted {between} at index {between}")
else:
  print("A problem with code") # A problem with code

gone = l.remove(1)
print("Removed: {}".format(gone))  # Removed: <Node data: 1>

# Note the insertion of '4' at index 3
print(l)  # [Head: 7]-> [6]-> [5]-> [4]-> [3]-> [Tail: 2]

THIS CODE WORKS!

Other variants of the print format have been tried f"{}", .format() and even an attempt at conversion to string str() was made, with no luck. Could someone explain exactly what the problem is (though, I believe it to be a NoneType issue) and how to resolve it?

I hope my question is clearer. Thank you.



No comments:

Post a Comment