mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 19:08:02 +00:00
Fix #13, PEP8-ify notebooks.
This commit is contained in:
@@ -34,7 +34,6 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
|
||||
"* Can we assume k is a valid integer?\n",
|
||||
" * Yes\n",
|
||||
"* If k = 0, does this return the last element?\n",
|
||||
@@ -96,7 +95,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyLinkedList(LinkedList):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def kth_to_last_elem(self, k):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
@@ -131,33 +130,35 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def test_kth_to_last_elem(self):\n",
|
||||
" print('Test: Empty list')\n",
|
||||
" linked_list = MyLinkedList(None)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(0), None)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: k >= len(list)')\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(100), None)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: One element, k = 0')\n",
|
||||
" head = Node(2)\n",
|
||||
" linked_list = MyLinkedList(head)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(0), 2)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: General case')\n",
|
||||
" linked_list.insert_to_front(1)\n",
|
||||
" linked_list.insert_to_front(3)\n",
|
||||
" linked_list.insert_to_front(5)\n",
|
||||
" linked_list.insert_to_front(7)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(2), 3)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Success: test_kth_to_last_elem')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = Test()\n",
|
||||
" test.test_kth_to_last_elem()\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
|
||||
"* Can we assume k is a valid integer?\n",
|
||||
" * Yes\n",
|
||||
"* If k = 0, does this return the last element?\n",
|
||||
@@ -101,7 +100,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyLinkedList(LinkedList):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def kth_to_last_elem(self, k):\n",
|
||||
" if self.head is None:\n",
|
||||
" return\n",
|
||||
@@ -110,15 +109,15 @@
|
||||
" curr = self.head\n",
|
||||
" prev = self.head\n",
|
||||
" counter = 0\n",
|
||||
" \n",
|
||||
" # Give current a headstart, incrementing it \n",
|
||||
"\n",
|
||||
" # Give current a headstart, incrementing it\n",
|
||||
" # once for k = 1, twice for k = 2, etc\n",
|
||||
" while counter < k:\n",
|
||||
" curr = curr.next\n",
|
||||
" counter += 1\n",
|
||||
" if curr is None:\n",
|
||||
" return\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # Increment both pointers until current reaches the end\n",
|
||||
" while curr.next is not None:\n",
|
||||
" curr = curr.next\n",
|
||||
@@ -154,33 +153,35 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" def test_kth_to_last_elem(self):\n",
|
||||
" print('Test: Empty list')\n",
|
||||
" linked_list = MyLinkedList(None)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(0), None)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: k >= len(list)')\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(100), None)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: One element, k = 0')\n",
|
||||
" head = Node(2)\n",
|
||||
" linked_list = MyLinkedList(head)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(0), 2)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Test: General case')\n",
|
||||
" linked_list.insert_to_front(1)\n",
|
||||
" linked_list.insert_to_front(3)\n",
|
||||
" linked_list.insert_to_front(5)\n",
|
||||
" linked_list.insert_to_front(7)\n",
|
||||
" assert_equal(linked_list.kth_to_last_elem(2), 3)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" print('Success: test_kth_to_last_elem')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = Test()\n",
|
||||
" test.test_kth_to_last_elem()\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
]
|
||||
|
||||
@@ -2,32 +2,34 @@ from nose.tools import assert_equal
|
||||
|
||||
|
||||
class Test(object):
|
||||
|
||||
|
||||
def test_kth_to_last_elem(self):
|
||||
print('Test: Empty list')
|
||||
linked_list = MyLinkedList(None)
|
||||
assert_equal(linked_list.kth_to_last_elem(0), None)
|
||||
|
||||
|
||||
print('Test: k >= len(list)')
|
||||
assert_equal(linked_list.kth_to_last_elem(100), None)
|
||||
|
||||
|
||||
print('Test: One element, k = 0')
|
||||
head = Node(2)
|
||||
linked_list = MyLinkedList(head)
|
||||
assert_equal(linked_list.kth_to_last_elem(0), 2)
|
||||
|
||||
|
||||
print('Test: General case')
|
||||
linked_list.insert_to_front(1)
|
||||
linked_list.insert_to_front(3)
|
||||
linked_list.insert_to_front(5)
|
||||
linked_list.insert_to_front(7)
|
||||
assert_equal(linked_list.kth_to_last_elem(2), 3)
|
||||
|
||||
|
||||
print('Success: test_kth_to_last_elem')
|
||||
|
||||
|
||||
def main():
|
||||
test = Test()
|
||||
test.test_kth_to_last_elem()
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user