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 singlylinked list."""
data: int
next: Node None
def initself data: int, next: Node None:
Construct a singly linked list. Use None for nd argument if tail."""
self.data data
self.next next
def strself str:
Produce a string visualization of the linked list."""
if self.next is None:
return fselfdata None"
else:
return fselfdataselfnext
def headself int:
Return the data attribute for the first element in the linked list."""
return self.data
def tailself Node None:
Return a linked list of every element minus the head."""
if self.next is None:
return None
else:
return self.next
def lastself 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:
valueat 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 listint 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 valueathead: Node None, index: int int:
raise IndexErrorIndex is out of bounds on the list."
def maxhead: Node None int:
raise ValueErrorCannot call max with None"
def linkifyitems: listint Node None:
return None
def scalehead: 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
