diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 056447256..91188090c 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -1,4 +1,5 @@ from __future__ import annotations + import math """ 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, - scores: list[int], height: float) -> int: +def minimax( + depth: int, node_index: int, is_max: bool, scores: list[int], height: float +) -> int: """ >>> import math >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423] diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 39b14c520..e45a210a1 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -47,22 +47,51 @@ class LinkedList: def __getitem__(self, index): """ 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: - raise ValueError("Negative indexes are not yet supported") + if not 0 <= index < len(self): + raise ValueError("list index out of range.") for i, node in enumerate(self): if i == index: - return node.data + return node # Used to change the data of a particular node 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 - # If list is empty - if current is None: - raise IndexError("The Linked List is empty") for i in range(index): - if current.next is None: - raise IndexError("list index out of range") current = current.next current.data = data @@ -163,8 +192,15 @@ def test_singly_linked_list() -> None: assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9) == 10 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 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(): from doctest import testmod