From 703fdecaadced645dad43772333502e01b75f38d Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Sun, 19 Jul 2020 06:17:41 -0400 Subject: [PATCH 1/3] refactor(omit_first_n.py): Condense omit_first_n and add non-public methods --- .../1_omit_first_n/omit_first_n.py | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py index d318c2bf..a0e8e120 100644 --- a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py +++ b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py @@ -1,20 +1,33 @@ +from typing import Optional + # A LinkedList is either: # None # Node + class Node: # first: int # rest: LinkedList - def __init__(self, first, rest = None): + def __init__(self, first: int, rest = None) -> None: self.first = first self.rest = rest + self._length = 0 + + def _calculate_length(self) -> int: + self._length = 0 + node = self + + while node is not None: + self._length += 1 + node = node.rest + + return self._length + + def __len__(self) -> int: + return self._length if self._length else self._calculate_length() + -# Write a function called omit_first_n that consumes a LinkedList ll and an integer n. The function returns a LinkedList with the first n elements omitted or None if n > len(ll) +# Write a function called omit_first_n that consumes a LinkedList linked_list and an integer n. The function returns a LinkedList with the first n elements omitted or None if n > len(ll) # omit_first_n(Node(1, Node(2, Node(3, None))), 2) -> Node(3, None) -def omit_first_n(ll, n): - if n == 0: - return ll - elif ll == None: - return None - else: - return omit_first_n(ll.rest, n-1) +def omit_first_n(linked_list: Optional[Node], n: int) -> Optional[Node]: + return linked_list if n == 0 or linked_list is None else omit_first_n(linked_list.rest, n - 1) From f87bf618c75bac4db5d76fcb583e866ebf63390b Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Sun, 19 Jul 2020 06:27:42 -0400 Subject: [PATCH 2/3] style(omit_first_n.py): Use recursion --- .../linked_lists/1_omit_first_n/omit_first_n.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py index a0e8e120..7d7ea1c7 100644 --- a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py +++ b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py @@ -13,21 +13,20 @@ def __init__(self, first: int, rest = None) -> None: self.rest = rest self._length = 0 - def _calculate_length(self) -> int: - self._length = 0 - node = self - - while node is not None: - self._length += 1 - node = node.rest - + def _calculate_length(self, node) -> int: + self._length = 0 if node is None else 1 + self._calculate_length(node.rest) return self._length def __len__(self) -> int: - return self._length if self._length else self._calculate_length() + return self._length if self._length else self._calculate_length(self) # Write a function called omit_first_n that consumes a LinkedList linked_list and an integer n. The function returns a LinkedList with the first n elements omitted or None if n > len(ll) # omit_first_n(Node(1, Node(2, Node(3, None))), 2) -> Node(3, None) def omit_first_n(linked_list: Optional[Node], n: int) -> Optional[Node]: return linked_list if n == 0 or linked_list is None else omit_first_n(linked_list.rest, n - 1) + + +linked_list = Node(1, Node(2)) +assert len(linked_list) == 2 +assert omit_first_n(linked_list, 2) == None From 4c9e3e8535e4584a564efc497f23b331f842d078 Mon Sep 17 00:00:00 2001 From: Lester Kim Date: Sun, 19 Jul 2020 06:47:18 -0400 Subject: [PATCH 3/3] refactor(omit_first_n.py): Refactor __len__ --- .../linked_lists/1_omit_first_n/omit_first_n.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py index 7d7ea1c7..aeb1cbca 100644 --- a/data_structures/linked_lists/1_omit_first_n/omit_first_n.py +++ b/data_structures/linked_lists/1_omit_first_n/omit_first_n.py @@ -11,22 +11,12 @@ class Node: def __init__(self, first: int, rest = None) -> None: self.first = first self.rest = rest - self._length = 0 - - def _calculate_length(self, node) -> int: - self._length = 0 if node is None else 1 + self._calculate_length(node.rest) - return self._length def __len__(self) -> int: - return self._length if self._length else self._calculate_length(self) + return 1 if self.rest is None else 1 + len(self.rest) # Write a function called omit_first_n that consumes a LinkedList linked_list and an integer n. The function returns a LinkedList with the first n elements omitted or None if n > len(ll) # omit_first_n(Node(1, Node(2, Node(3, None))), 2) -> Node(3, None) def omit_first_n(linked_list: Optional[Node], n: int) -> Optional[Node]: return linked_list if n == 0 or linked_list is None else omit_first_n(linked_list.rest, n - 1) - - -linked_list = Node(1, Node(2)) -assert len(linked_list) == 2 -assert omit_first_n(linked_list, 2) == None