Any questions that reference the Node class are referring to a class defined in the following way:
from __future__ import annotations
class Node:
value: int
next: Node | None
def __init__(self, val: int, next: Node | None):
self.value = val
self.next = next
def __str__(self) -> str:
rest: str
if self.next is None:
rest = "None"
else:
rest = str(self.next)
return f"{self.value} -> {rest}"Multiple Choice
(Select all that apply) Which of the following properties of a recursive function will ensure that it does not have an infinite loop?
The function calls itself in the recursive case.
The recursive case progresses towards the base case.
The base case returns a result directly (it does not call the function again).
The base case is always reached.
None of the above
(Fill in the blank) A linked list in python consists of one or more instances of the _____ class.
listintNodeNoneNone of the above
(True/False) Attempting to access the
valueornextattribute ofNonewill result in an error.(True/False) There is no way to traverse to the start of a linked list that has multiple Nodes given only a reference to the last
Node.
SOLUTIONS
B, C, and D. A is true of all recursive functions, but does not guarantee that there won’t be an infinite loop.
C
True, attempting to access any attributes of
Nonewill result in an error since it has no attributes.True, Nodes only know about the
Node“in front” of them, or the nextNode, so you cannot move backwards in a linked list.
Code Writing
Write a recursive function (not a method of the
Nodeclass) namedrecursive_rangewithstartandendintparameters that will create a linked list with the Nodes having values counting fromstarttoend, not includingend, either counting down (decrementing) or up (incrementing) depending on whatstartandendare. The function signature is below to get you started.def recursive_range(start: int, end: int) -> Node | None:Write a recursive method of the
Nodeclass namedappendthat has parametersselfandnew_valwhich is of typeint, and this method should create a newNodeat the end of the linked list and returnNone. In other words, the lastNodeobject before this method is called will have anextattribute ofNone, but after this method is called, it should have anextattribute equal to aNodeobject with valuenew_valandnextattribute beingNone(since that new node is now the lastNodein the linked list).Write a recursive method of the
Nodeclass namedget_lengththat has parametersselfandcountwhich is of typeint, which if you were to call with acountargument of 0, would return the length of the linked list starting withself(not includingNone). Hint: Usecountto keep track of aNodecount between function calls. How would you write this method as an iterative function (with nocountparameter)?
SOLUTIONS
Recursive range has two base cases, and the one that is used depends on if
startis greater than or less thanend.def recursive_range(start: int, end: int) -> Node | None: if start == end: return None elif start < end: return Node(start, recursive_range(start + 1, end)) else: return Node(start, recursive_range(start - 1, end))Here is one way to make the
appendmethod:def append(self, new_val: int) -> None: if self.next is None: self.next = Node(new_val, None) else: self.next.append(new_val)Here are two possibilities:
def get_length(self, count: int) -> int:
if self.next is None:
return count + 1
else:
return self.next.get_length(count + 1) def get_length(self, count: int) -> int:
count += 1
if self.next is None:
return count
else:
return self.next.get_length(count)Short Answer
Based on the following code snippet, what would be the output of the following lines of code given in parts 1.1-1.4?
from __future__ import annotations # Node class definition included for reference! class Node: value: int next: Node | None def __init__(self, val: int, next: Node | None): self.value = val self.next = next def __str__(self) -> str: rest: str if self.next is None: rest = "None" else: rest = str(self.next) return f"{self.value} -> {rest}" x: Node = Node(4, None) y: Node = Node(8, None) x.next = y z: Node = Node(16, None) z.next = x x = Node(32, None)1.1.
print(z.next.next.value)1.2.
print(y.next)1.3.
print(x)1.4.
print(z)
SOLUTIONS
Question 1 answers:
1.1.
81.2.
None1.3.
32 -> None1.4.
16 -> 4 -> 8 -> None