diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 48d1bde..e1ba390 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -30,10 +30,11 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with pytest run: | - pytest --continue-on-collection-errors + git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/* + git diff origin/main HEAD --name-only -- practice | xargs dirname | sort | uniq | xargs pytest - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics \ No newline at end of file diff --git a/practice/simple-linked-list/simple_linked_list.py b/practice/simple-linked-list/simple_linked_list.py index cbf120e..cb229ee 100644 --- a/practice/simple-linked-list/simple_linked_list.py +++ b/practice/simple-linked-list/simple_linked_list.py @@ -1,33 +1,61 @@ class Node: - def __init__(self, value): - pass + def __init__(self, value, next_node=None): + self._value = value + self._next_node = next_node def value(self): - pass + return self._value def next(self): - pass - - + return self._next_node + class LinkedList: def __init__(self, values=[]): - pass + self._head = None + self._length = 0 + # Initialize in normal order + for value in values: + self.push(value) def __len__(self): - pass + count = 0 + node = self._head + while node: + count += 1 + node = node.next() + return count def head(self): - pass + if not self._head: + raise EmptyListException("The list is empty.") + return self._head def push(self, value): - pass + new_node = Node(value, self._head) + self._head = new_node + self._length += 1 def pop(self): - pass + if self._length == 0: + raise EmptyListException("The list is empty.") + value = self._head.value() + self._head = self._head.next() + self._length -= 1 + return value def reversed(self): - pass - - + return LinkedList(list(self)) + + def __iter__(self): + node = self._head + while node: + yield node.value() + node = node.next() + class EmptyListException(Exception): + def __init__(self, message): + self.message = message pass + + +