mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-19 04:07:36 +00:00
Errors from ruff tests solved
This commit is contained in:
commit
01dfb178b2
@ -7,6 +7,7 @@ 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):
|
||||||
@ -16,7 +17,8 @@ class Node:
|
|||||||
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
|
||||||
@ -56,8 +58,7 @@ class SortedLinkedList:
|
|||||||
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=" ")
|
||||||
@ -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
|
||||||
|
|
||||||
@ -152,8 +154,7 @@ class SortedLinkedList:
|
|||||||
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:
|
||||||
@ -162,8 +163,7 @@ class SortedLinkedList:
|
|||||||
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:
|
||||||
@ -190,7 +190,7 @@ class SortedLinkedList:
|
|||||||
|
|
||||||
|
|
||||||
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")
|
||||||
@ -199,24 +199,18 @@ if __name__ == "__main__":
|
|||||||
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")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user