Fixed linked list bug (#2481)

* * fixed __getitem__() function
* add test

* * updated doctests
* updated __setitem__() func

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Du Yuanchao 2020-09-26 00:08:57 +08:00 committed by GitHub
parent 18f1dcd48a
commit b81fcef66b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 10 deletions

View File

@ -1,4 +1,5 @@
from __future__ import annotations from __future__ import annotations
import math import math
""" Minimax helps to achieve maximum score in a game by checking all possible moves """ Minimax helps to achieve maximum score in a game by checking all possible moves
@ -10,8 +11,9 @@ import math
""" """
def minimax(depth: int, node_index: int, is_max: bool, def minimax(
scores: list[int], height: float) -> int: depth: int, node_index: int, is_max: bool, scores: list[int], height: float
) -> int:
""" """
>>> import math >>> import math
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423] >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]

View File

@ -47,22 +47,51 @@ class LinkedList:
def __getitem__(self, index): def __getitem__(self, index):
""" """
Indexing Support. Used to get a node at particular position Indexing Support. Used to get a node at particular position
>>> linked_list = LinkedList()
>>> for i in range(0, 10):
... linked_list.insert_nth(i, i)
>>> all(str(linked_list[i]) == str(i) for i in range(0, 10))
True
>>> linked_list[-10]
Traceback (most recent call last):
...
ValueError: list index out of range.
>>> linked_list[len(linked_list)]
Traceback (most recent call last):
...
ValueError: list index out of range.
""" """
if index < 0: if not 0 <= index < len(self):
raise ValueError("Negative indexes are not yet supported") raise ValueError("list index out of range.")
for i, node in enumerate(self): for i, node in enumerate(self):
if i == index: if i == index:
return node.data return node
# Used to change the data of a particular node # Used to change the data of a particular node
def __setitem__(self, index, data): def __setitem__(self, index, data):
"""
>>> linked_list = LinkedList()
>>> for i in range(0, 10):
... linked_list.insert_nth(i, i)
>>> linked_list[0] = 666
>>> linked_list[0]
666
>>> linked_list[5] = -666
>>> linked_list[5]
-666
>>> linked_list[-10] = 666
Traceback (most recent call last):
...
ValueError: list index out of range.
>>> linked_list[len(linked_list)] = 666
Traceback (most recent call last):
...
ValueError: list index out of range.
"""
if not 0 <= index < len(self):
raise ValueError("list index out of range.")
current = self.head current = self.head
# If list is empty
if current is None:
raise IndexError("The Linked List is empty")
for i in range(index): for i in range(index):
if current.next is None:
raise IndexError("list index out of range")
current = current.next current = current.next
current.data = data current.data = data
@ -163,8 +192,15 @@ def test_singly_linked_list() -> None:
assert linked_list.delete_head() == 0 assert linked_list.delete_head() == 0
assert linked_list.delete_nth(9) == 10 assert linked_list.delete_nth(9) == 10
assert linked_list.delete_tail() == 11 assert linked_list.delete_tail() == 11
assert len(linked_list) == 9
assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
assert all(linked_list[i] == i + 1 for i in range(0, 9)) is True
for i in range(0, 9):
linked_list[i] = -i
assert all(linked_list[i] == -i for i in range(0, 9)) is True
def main(): def main():
from doctest import testmod from doctest import testmod