Home > other >  Python Linked lists
Python Linked lists

Time:10-26


Don't know why I can't assert False pass
Please comment, the assert False here how do I change the function?

CodePudding user response:

Assert this statement can be deleted completely,

If you want a full reply, please post all the text of the code,

CodePudding user response:

The class Node:

Def __init__ (self, value=https://bbs.csdn.net/topics/None, nextnode=None) :
The self. The value=https://bbs.csdn.net/topics/value
The self. The next=nextnode

Def __repr__ (self) :
Return '& lt; The Node ({}) & gt; '. The format (repr (self value))

Def insert (the head, the value, the position) :
If the position==0:
Value, return the Node (the head)
The else:
Current_position=1
The node=head
Try:
While the node. The next and current_position & lt; Position:
The node=node. Next
Current_position +=1
Node. Next=node (value, node. Next)
Except IndexError:
Print (' Should have failed with the an IndexError ')
Return the head

Def pop (head, the position) :
If the head is None:
Return (None, None)
If the position==0:
Return (the head, head. Next)
The node=head
Current_position=1
If the node. The next and current_position & lt; Position:
The node=node. Next
Current_position +=1
pop_value=https://bbs.csdn.net/topics/node.next
If pop_value. Next is None:
Return (node. Next, the head)
Return (pop_value, head)

Def stringify_linked_list (head) :
If the head is None:
Return None
The else:
The node=head
List_=[]
While the node:
List_. Append (node)
The node=node. Next
Stringlist='
For I in list_ :
Stringlist +=STR (I) + '- & gt; '
Stringlist=stringlist + 'None'
Return stringlist

CodePudding user response:

This is my linked list, assert is to test the code, I don't know how to pass the assert

CodePudding user response:

Please see me to help you modify, to consult,



CodePudding user response:

Thank you very much!!!!!

CodePudding user response:

reference 5 floor HELEN_QIU reply:
thank you very much!!

A: don't mention it.

1, the problem is that your code, write their own list, no IndexError, need a custom exception,

2, test the assertion code, written in the right place,

CodePudding user response:

Ok thank you!
  • Related