Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.5k
dibs: implement simple-linked-list#385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
58efa25927f13629800e5bd03ee7cfd852452d24df6c3ea1e7f2e8ca795a81b14a219c4e81131e2f6b839ca4ff271687b7ed992fc44b2bf98c031c06bef50093321b9File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| class Element(object): | ||
| def __init__(self, value): | ||
| self.value = value | ||
| self.next = None | ||
| class LinkedList(object): | ||
| def __init__(self, head=None): | ||
| self.head = head | ||
| self.size = 0 | ||
| def push(self, new_element): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming needs to be improved. Right now one can push values, than the parameter should be named accordingly or the method should accept Elements instead. | ||
| new_e = Element(new_element) | ||
| if self.empty(): | ||
| self.head = new_e | ||
| else: | ||
| new_e.next = self.head | ||
| self.head = new_e | ||
| self.size += 1 | ||
| def pop(self): | ||
| if self.empty(): | ||
| return None | ||
| else: | ||
| e = self.head | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for undescriptive short variable names, there is enough space for | ||
| self.head = self.head.next | ||
| self.size -= 1 | ||
| return e.value | ||
| def empty(self): | ||
| return self.size == 0 | ||
| def get_peek(self): | ||
| if self.empty(): | ||
| return None | ||
| else: | ||
| return self.head.value | ||
| def reverse(self): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The convention is that reverse/sort/etc work change the actual instance, while reversed/sorted/etc would return a new changed instance leaving the other instance unchanged. | ||
| new_list = LinkedList() | ||
| if self.size > 0: | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to check size and the current element, one check is sufficient. It makes the code more understandable: defreversed(self):
new_list=LinkedList()
current=self.headwhilecurrent:
new_list.push(current.value)
current=current.nextreturnnew_list | ||
| current = self.head | ||
| while current.next: | ||
| new_list.push(current.value) | ||
| current = current.next | ||
| new_list.push(current.value) | ||
| return new_list | ||
| def to_array(self): | ||
| arr = [] | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method also doesn't need two checks for the same thing. There is also no need in cutting variable names short. defto_array(self):
array= []
current=self.headwhilecurrent:
array.append(current.value)
current=current.nextreturnarray | ||
| if self.empty(): | ||
| return [] | ||
| current = self.head | ||
| while current.next: | ||
| arr.append(current.value) | ||
| current = current.next | ||
| arr.append(current.value) | ||
| return arr | ||
| def from_array(self, arr=[]): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would either implement the | ||
| if len(arr) == 0: | ||
| return self | ||
| else: | ||
| for i in arr: | ||
| self.push(i) | ||
| return self | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import unittest | ||
| from simple_linked_list import LinkedList | ||
| class LinkedListTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.slist = LinkedList() | ||
| def test_size(self): | ||
| self.slist.push(0) | ||
| self.assertEqual(1, self.slist.size) | ||
| # notice if size is increased | ||
| self.slist.push(1) | ||
| self.assertEqual(2, self.slist.size) | ||
| self.slist.push(2) | ||
| self.assertEqual(3, self.slist.size) | ||
| def test_pop(self): | ||
| # with cero element | ||
| self.assertIsNone(self.slist.pop()) | ||
| # with multiple elements | ||
| self.slist.push(1) | ||
| self.slist.push(2) | ||
| self.slist.push(3) | ||
| self.assertEqual(3, self.slist.pop()) | ||
| self.assertEqual(2, self.slist.pop()) | ||
| self.assertEqual(1, self.slist.pop()) | ||
| self.assertIsNone(self.slist.pop()) | ||
| def test_reverse(self): | ||
| # empty reversed LinkedList | ||
| empty_reversed = self.slist.reverse() | ||
| self.assertEqual(0, empty_reversed.size) | ||
| self.assertIsNone(empty_reversed.head) | ||
| # push_elements | ||
| self.slist.push(1) | ||
| self.slist.push(2) | ||
| self.slist.push(3) | ||
| self.slist.push(4) | ||
| reversed_list = self.slist.reverse() | ||
| self.assertEqual(1, reversed_list.head.value) | ||
| self.assertEqual(1, reversed_list.get_peek()) | ||
| def test_to_array(self): | ||
| # in : empty list out : empty array | ||
| self.assertListEqual([], self.slist.to_array()) | ||
| # push_elements | ||
| self.slist.push(1) | ||
| self.slist.push(2) | ||
| self.slist.push(3) | ||
| self.slist.push(4) | ||
| self.assertListEqual([4, 3, 2, 1], self.slist.to_array()) | ||
| def test_from_array(self): | ||
| # in : empty array out : empty list | ||
| self.assertIsNone(self.slist.from_array([]).head) | ||
| # push_elements | ||
| new_arr = [1, 2, 3, 4] | ||
| self.assertEqual(4, self.slist.from_array(new_arr).head.value) | ||
| self.assertEqual(4, self.slist.from_array(new_arr).get_peek()) | ||
| if __name__ == '__main__': | ||
| unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
headargument is never used. Maybe it could be used for the from array list creation instead.