Question: 9 test case wrong answer . sample input ? runtime error . class Node: def _ _ init _ _ ( self , value )

9 test case wrong answer.
sample input? runtime error.
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, position, value):
if position 0:
print("err_code=100")
return
new_node = Node(value)
if position ==0: #
new_node.next = self.head
self.head = new_node
self.print_list()
return
#
length =0
current = self.head
while current:
length +=1
current = current.next
if position > length: #
print("err_code=100")
return
#
if position == length:
current = self.head
while current.next:
current = current.next
current.next = new_node
self.print_list()
return
#
current_node = self.head
index =0
while current_node and index position -1:
current_node = current_node.next
index +=1
new_node.next = current_node.next
current_node.next = new_node
self.print_list()
def delete(self, value):
if self.head is None:
print("err_code=101")
return
if self.head.value == value: #
self.head = self.head.next
self.print_list()
return
current_node = self.head
while current_node.next and current_node.next.value != value:
current_node = current_node.next
if current_node.next: #
current_node.next = current_node.next.next
self.print_list()
else:
print("err_code=101") #
def reverse(self):
if self.head is None:
print("err_code=102")
return
prev = None
current = self.head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev
self.print_list()
def find(self, value):
current = self.head
index =0
while current:
if current.value == value:
print(index)
return
current = current.next
index +=1
print("err_code=103")
def sub_list(self, ps, pe):
if ps 0 or pe ps:
print("err_code=104")
return
current = self.head
index =0
while current and index ps:
current = current.next
index +=1
if not current: #
print("err_code=104")
return
temp_head = current
while temp_head and index = pe:
print(temp_head.value, end="->" if temp_head.next and index pe else "-> NULL
")
temp_head = temp_head.next
index +=1
if index = pe and temp_head is None:
print("err_code=104")
def count_frequency(self, value):
current = self.head
count =0
while current:
if current.value == value:
count +=1
current = current.next
print(count)
def sort(self):
if self.head is None or self.head.next is None: #
return
values =[]
current = self.head
while current:
values.append(current.value)
current = current.next
values.sort()
current = self.head
for value in values:
current.value = value
current = current.next
self.print_list()
def print_list(self):
current = self.head
while current:
print(current.value, end="->")
current = current.next
print("NULL")
import sys
linked_list = LinkedList()
while True:
command = sys.stdin.readline().strip()
if not command:
break
parts = command.split()
if parts[0]== "I":
linked_list.insert(int(parts[1]), int(parts[2]))
elif parts[0]=="D":
linked_list.delete(int(parts[1]))
elif parts[0]=="R":
linked_list.reverse()
elif parts[0]=="F":
linked_list.find(int(parts[1]))
elif parts[0]=="S":
linked_list.sub_list(int(parts[1]), int(parts[2]))
elif parts[0]=="C":
linked_list.count_frequency(int(parts[1]))
elif parts[0]=="T":
linked_list.sort()
9 test case wrong answer . sample input ? runtime

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!