From 5efc530ec1d688f161e62ccc090aa94dd9630cdb Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 25 Sep 2020 23:05:54 +0800 Subject: [PATCH 1/3] * fixed __getitem__() function * add test --- .../linked_list/singly_linked_list.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 39b14c520521..66d68910a76f 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -47,15 +47,31 @@ def __repr__(self): 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 """ if index < 0: raise ValueError("Negative indexes are not yet supported") 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 + """ current = self.head # If list is empty if current is None: @@ -163,8 +179,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 From 517f08d6ee8594b09e83b4461cec8c843aa635b8 Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 25 Sep 2020 23:25:02 +0800 Subject: [PATCH 2/3] * updated doctests * updated __setitem__() func --- .../linked_list/singly_linked_list.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 66d68910a76f..e45a210a1785 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -52,9 +52,17 @@ def __getitem__(self, index): ... 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 @@ -71,14 +79,19 @@ def __setitem__(self, index, data): >>> 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 From a70f62215d46c49cf6d07c8b299954503360d50e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 25 Sep 2020 15:28:26 +0000 Subject: [PATCH 3/3] fixup! Format Python code with psf/black push --- backtracking/minimax.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 056447256395..91188090c899 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 @@ """ -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]