Errors from ruff tests solved

This commit is contained in:
mjk22071998 2024-10-01 13:25:18 +05:00
commit 01dfb178b2

View File

@ -3,25 +3,27 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
""" """
This is a sorted linked list class that This is a sorted linked list class that
creates a sorted linked list of integer datatype creates a sorted linked list of integer datatype
""" """
@dataclass @dataclass
class Node: class Node:
def __init__(self, data): def __init__(self, data):
self.data: int = data self.data: int = data
self.next_node: Node | None = None self.next_node: Node | None = None
def __repr__(self): def __repr__(self):
return f"Node({self.data}, {self.next_node})" return f"Node({self.data}, {self.next_node})"
class SortedLinkedList:
class SortedLinedList:
def __init__(self): def __init__(self):
self.numNodes : int = 0 self.numNodes: int = 0
self.head: Node | None = None self.head: Node | None = None
self.tail: Node | None = None self.tail: Node | None = None
def __repr__(self): def __repr__(self):
nodes = [] nodes = []
temp = self.head temp = self.head
@ -29,12 +31,12 @@ class SortedLinkedList:
nodes.append(str(temp.data)) nodes.append(str(temp.data))
temp = temp.next_node temp = temp.next_node
return f"SortedLinkedList({', '.join(nodes)})" return f"SortedLinkedList({', '.join(nodes)})"
def insert(self, data: int): def insert(self, data: int):
"""This Function inserts node in it's sorted position """This Function inserts node in it's sorted position
This function can be re written for any data type but This function can be re written for any data type but
the comparator her must have to be changed the comparator her must have to be changed
Args: Args:
data (int): the data of linked list data (int): the data of linked list
""" """
@ -54,21 +56,20 @@ class SortedLinkedList:
if new_node.next_node is None: if new_node.next_node is None:
self.tail = new_node self.tail = new_node
self.numNodes += 1 self.numNodes += 1
def display(self): def display(self):
"""This function displays whole list """This function displays whole list"""
""" temp = self.head
temp=self.head
while temp: while temp:
print(temp.data,end=" ") print(temp.data, end=" ")
temp=temp.next_node temp = temp.next_node
print() print()
def delete(self, data: int) -> bool: def delete(self, data: int) -> bool:
"""This Function deletes first appearance of node with """This Function deletes first appearance of node with
data from it's sorted position data from it's sorted position
This function can be re written for any data type but This function can be re written for any data type but
the comparator her must have to be changed the comparator her must have to be changed
Args: Args:
@ -96,9 +97,9 @@ class SortedLinkedList:
temp_node = temp_node.next_node temp_node = temp_node.next_node
return False return False
def search(self, data: int) -> bool: def search(self, data: int) -> bool:
"""This function searches the data given input from user """This function searches the data given input from user
and return whether the data exists or not and return whether the data exists or not
Args: Args:
@ -113,6 +114,7 @@ class SortedLinkedList:
return True return True
temp = temp.next_node temp = temp.next_node
return False return False
def is_empty(self) -> bool: def is_empty(self) -> bool:
"""This function will check whether the list is empty or not """This function will check whether the list is empty or not
@ -120,8 +122,8 @@ class SortedLinkedList:
bool: flag indicating whether list is empty or not bool: flag indicating whether list is empty or not
""" """
return self.head is None return self.head is None
def length (self) -> int: def length(self) -> int:
"""This function returns the length of the linked list """This function returns the length of the linked list
@ -129,7 +131,7 @@ class SortedLinkedList:
int: The length of linked list int: The length of linked list
""" """
return numNodes return numNodes
def min_value(self) -> int | None: def min_value(self) -> int | None:
"""This function will return minimum value """This function will return minimum value
@ -139,7 +141,7 @@ class SortedLinkedList:
if self.head is None: if self.head is None:
return None return None
return self.head.data return self.head.data
def max_value(self) -> int | None: def max_value(self) -> int | None:
"""This function will return maximum value """This function will return maximum value
@ -150,20 +152,18 @@ class SortedLinkedList:
if self.tail is None: if self.tail is None:
return None return None
return self.tail.data return self.tail.data
def remove_duplicates(self): def remove_duplicates(self):
"""This Function will remove the duplicates from the list """This Function will remove the duplicates from the list"""
"""
temp = self.head temp = self.head
while temp and temp.next_node: while temp and temp.next_node:
if temp.data == temp.next_node.data: if temp.data == temp.next_node.data:
temp.next_node = temp.next_node.next_node temp.next_node = temp.next_node.next_node
else: else:
temp = temp.next_node temp = temp.next_node
def reverse(self): def reverse(self):
"""This function will reveres the list """This function will reveres the list"""
"""
prev = None prev = None
current = self.head current = self.head
while current: while current:
@ -187,36 +187,30 @@ class SortedLinkedList:
return return
self.tail.next_node = other_list.head self.tail.next_node = other_list.head
self.tail = other_list.tail self.tail = other_list.tail
if __name__ == "__main__": if __name__ == "__main__":
linked_list=SortedLinkedList() linkedList = SortedLinedList()
while True: while True:
print("Enter") print("Enter")
print("1. Insert") print("1. Insert")
print("2. Display") print("2. Display")
print("3. Delete") print("3. Delete")
print("4. Exit") print("4. Exit")
choice= input("Enter your choice: ") choice = input("Enter your choice: ")
if choice == '1': if choice == "1":
data = int(input("Enter a number: ")) data = int(input("Enter a number: "))
linked_list.insert(data) linkedList.insert(data)
elif choice == '2': elif choice == "2":
linked_list.display() linkedList.display()
elif choice == '3': elif choice == "3":
data = int(input("Enter the data to delete: ")) data = int(input("Enter the data to delete: "))
if linked_list.delete(data): if linked_list.delete(data):
print(f"Node with data {data} deleted successfully") print(f"Node with data {data} deleted successfully")
else: else:
print(f"Node with data {data} not found in the list") print("Node with data {} not found in the list".format(data))
elif choice == '4': elif choice == "4":
break break
else: else:
print("Wrong input") print("Wrong input")