Question: Based on the following base Python class: from _ _ future _ _ import annotations class Node: An item in a singly

Based on the following base Python class:
from __future__ import annotations
class Node:
"""An item in a singly-linked list."""
data: int
next: Node | None
def __init__(self, data: int, next: Node | None):
"""Construct a singly linked list. Use None for 2nd argument if tail."""
self.data = data
self.next = next
def __str__(self)-> str:
"""Produce a string visualization of the linked list."""
if self.next is None:
return f"{self.data}-> None"
else:
return f"{self.data}->{self.next}"
def head(self)-> int:
"""Return the data attribute for the first element in the linked list."""
return self.data
def tail(self)-> Node | None:
"""Return a linked list of every element minus the head."""
if self.next is None:
return None
else:
return self.next
def last(self)-> int:
"""Return the data of the last element in the linked list."""
current = self
while current.next is not None:
current = current.next
return current.data
Create the following subsequent functions using recursive function calls:
value_at - Given a head Node | None and an index int as inputs, return the data of the Node stored at the given index, or raise an IndexError if the index does not exist.
max - Given a head Node, return the maximum data value in the linked list. If the linked list is empty, raise a ValueError.
linkify - Given a list[int], your linkify function should return a Linked List of Nodes with the same values, in the same order, as the input list.
scale - Given a head Node of a linked list and an int factor to scale by, return a new linked list of Nodes where each value in the original list is scaled (multiplied) by the scaling factor.
Below are the skeleton codes of each function:
def value_at(head: Node | None, index: int)-> int:
raise IndexError("Index is out of bounds on the list.")
def max(head: Node | None)-> int:
raise ValueError("Cannot call max with None")
def linkify(items: list[int])-> Node | None:
return None
def scale(head: Node | None, factor: int)-> Node | None:
return None

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 Programming Questions!