Python question
class LinkNode:
def __init__(self,value,nxt=None):
assert isinstance(nxt, LinkNode) or nxt is None
self.value = value
self.next = nxt
Question 2.1. Empty Node
In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None.
Add a function is_empty to class LinkNode that checks whether a node is an empty node.
class LinkNode:
"""
>>> node = LinkNode(6, LinkNode(5, None))
>>> node.is_empty()
False
>>> node = LinkNode(None, None)
>>> node.is_empty()
True
"""
def __init__(self,value,nxt=None):
assert isinstance(nxt, LinkNode) or nxt is None
self.value = value
self.next = nxt
def is_empty(self):
# Your code is here
Question 2.2. Print Linked List
The representation of linked list that we used in class is not very convenient for people to comprehend. Therefore, the first function I'd like you to write
will take a linked list as an argument and will print it using box-and-pointer notation. (This function is not a part of the class)
You can assume that there is only one empty node and it will represent an empty linked list.
def print_list (lst):
""" Prints linked list in a readable format
>>> print_list(LinkNode(3, None))
3 -> None
>>> print_list(LinkNode(3))
3 -> None
>>> print_list(LinkNode(3, LinkNode(2, LinkNode(1, None))))
3 -> 2 -> 1 -> None
>>> print_list (LinkNode(None, None))
empty list
"""
# Your code is here
Question 2.3. Linked List Length
How many elements are in your linked list? Who knows...there are no function to use. Yeah..it means you have to write one. Please, write a function that returns
the length of a given linked list (number of elements in it).
You can solve this problem recursively.
def list_length(lst):
""" Returns the number of elements in the linked list
>>> list_length(LinkNode(3, None))
1
>>> list_length(LinkNode(3))
1
>>> list_length(LinkNode(None, None))
0
>>> list_length(LinkNode(3, LinkNode(2, LinkNode(1, None))))
3
"""
# Your code is here
Question 2.4. Find element at index
Linked List are not arrays. It means you have to start at the beginning of the linked list to get to a certain place, looping though one element at the time.
Write a function that loops though the list and returns a value at the given index. If index is out of bounds, it reports the error to the user. See doctests for more details.
def get_item(lst, index):
""" Returns an element at the given index
>>> get_item(LinkNode(3, LinkNode(2, LinkNode(1, None))), 1)
2
>>> get_item(LinkNode(3, LinkNode(2, LinkNode(1, None))), 0)
3
>>> get_item(LinkNode(3, LinkNode(2, LinkNode(1, LinkNode(0, LinkNode(17))))), 4)
17
>>> get_item(LinkNode(3, LinkNode(2, LinkNode(1, None))), 4)
'index is out of bounds'
>>> get_item(LinkNode(3, LinkNode(2, LinkNode(1, None))), -4)
'index is out of bounds'
>>> get_item(LinkNode(None, None), 0)
'list is empty'
"""
# Your code is here
code:
class LinkNode:
def __init__(self,value,nxt=None):
assert isinstance(nxt, LinkNode) or
nxt is None
self.value = value
self.next = nxt
def print_list(lst):
temp = lst
if(is_empty(lst)):
print("empty list")
else:
while temp:
print(temp.value, end="->")
temp =
temp.next
print ("None")
print()
def is_empty(lst):
if lst.value is None:
return True
else:
return False
def list_length(lst):
temp = lst
k=0
if(is_empty(lst)):
return 0
while temp:
k=k+1
temp = temp.next
return k
def get_item(self, data):
current = self
found = False
if data<0:
return "Index is out of
Bounds"
if (is_empty(self)):
return "list is empty"
for k in range(0,data):
if current and found is
False:
if k ==
data:
found = True
else:
current = current.next
if current is None :
return "Index is
out of Bounds"
return current.value
print()
print("Usage of is_empty function")
print(is_empty(LinkNode(6, LinkNode(5, None))))
print(is_empty(LinkNode(None,None)))
print()
print("Usage of Print_list function")
print_list(LinkNode(3, None))
print_list(LinkNode(3))
print_list(LinkNode(3, LinkNode(2, LinkNode(1, None))))
print_list (LinkNode(None, None))
print()
print("Usage of List_length function")
print(list_length(LinkNode(3, None)))
print(list_length(LinkNode(3)))
print(list_length(LinkNode(None, None)))
print(list_length(LinkNode(3, LinkNode(2, LinkNode(1, None)))))
print()
print("Usage of get_item function")
print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),1))
print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),0))
print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,LinkNode(0,LinkNode(17))))),4))
print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),4))
print(get_item(LinkNode(3,LinkNode(2,LinkNode(1,None))),-4))
print(get_item(LinkNode(None,None),0))
output:
If you have any queries, please comment below.
Please upvote , if you like this answer.
Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value...
I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...
PYTHON -------------------------------------------------------- class LinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 # Return the head element in the list def getFirst(self): if self.__size == 0: return None else: return self.__head.element # Return the last element in the list def getLast(self): if self.__size == 0: return None else: return self.__tail.element # Add an element to the beginning of the list def addFirst(self, e): newNode = Node(e) # Create a new node newNode.next = self.__head # link...
class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object): def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...
Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...
Python3 You should use the getters and setters I post in the first pic 157 158 class LinkNode: 159 160 def-initー(self.value,nxt One): assert isinstance(nxt,Lin㎾ode) or nxt is None self.value value self.next = nxt 61 162 163 164 165 166 167 168 169 170 def get_value(self): def set_value(self, value): def get_next(seif): def set_next(self,nxt): def _repr (self): return self.value self.value = value return self.next self.next = nxt return repr(self.value)+ ", "+repr(self.next) 172 173 A permutation can be considered as an injective (one-to-one)...
from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...
python3 Question 2.4. Find element at index Linked List are not arrays. It means you have to start at the beginning of the linked list to get to a certain place, looping though one element at the time. Write a function that loops though the list and returns a value at the given index. If index is out of bounds, it reports the error to the user. See doctests for more details def get item(ist, index) : Returns an element...
Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...
ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...
# Declaring Node class class Node: def __init__(self, node_address): self.address = node_address self.next = None # Creating empty list forwarding_list = Node() # Recurssive Method for add address def addAddress(node): if node.address == old_address: # Address is same if node.next == None: # Next is None node.next = Node(new_address) # Add Next print("Added") # Addedd else: # Next is not none print("Entry already exists") elif node.next != None: # Check Next Node addAddress(node.next, old_address, new_address) def addEntry(forwarding_list):# Method for add...