mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Renamed unit test method to be more descriptive.
This commit is contained in:
parent
9e84d8e257
commit
df90beea91
|
@ -91,7 +91,7 @@
|
|||
"source": [
|
||||
"class MyLinkedList(LinkedList):\n",
|
||||
" \n",
|
||||
" def add(self, first_list, second_list):\n",
|
||||
" def add_reverse(self, first_list, second_list):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
|
@ -126,11 +126,11 @@
|
|||
"\n",
|
||||
"class TestAddReverse(object):\n",
|
||||
" \n",
|
||||
" def test_add(self):\n",
|
||||
" def test_add_reverse(self):\n",
|
||||
" print('Test: Empty list(s)')\n",
|
||||
" assert_equal(MyLinkedList().add(None, None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(Node(5), None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(None, Node(10)), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(None, None), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(Node(5), None), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(None, Node(10)), None)\n",
|
||||
"\n",
|
||||
" print('Test: Add values of different lengths')\n",
|
||||
" # Input 1: 6->5->None\n",
|
||||
|
@ -141,7 +141,7 @@
|
|||
" second_list = MyLinkedList(Node(9))\n",
|
||||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
" result = MyLinkedList().add_reverse(first_list, second_list)\n",
|
||||
" assert_equal(result.get_all_data(), [5, 4, 8])\n",
|
||||
"\n",
|
||||
" print('Test: Add values of same lengths')\n",
|
||||
|
@ -156,14 +156,14 @@
|
|||
" second_list = MyLinkedList(second_head)\n",
|
||||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
" result = MyLinkedList().add_reverse(first_list, second_list)\n",
|
||||
" assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
|
||||
" \n",
|
||||
" print('Success: test_add')\n",
|
||||
" print('Success: test_add_reverse')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestAddReverse()\n",
|
||||
" test.test_add()\n",
|
||||
" test.test_add_reverse()\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
|
|
|
@ -112,7 +112,7 @@
|
|||
"source": [
|
||||
"class MyLinkedList(LinkedList):\n",
|
||||
" \n",
|
||||
" def __add__(self, first_node, second_node, carry):\n",
|
||||
" def __add_reverse__(self, first_node, second_node, carry):\n",
|
||||
" if type(carry) != int and carry < 0:\n",
|
||||
" raise ValueError('Invalid int argument: carry')\n",
|
||||
" \n",
|
||||
|
@ -127,15 +127,15 @@
|
|||
" remainder = value % 10\n",
|
||||
" new_carry = 1 if value >= 10 else 0\n",
|
||||
" node = Node(remainder)\n",
|
||||
" node.next = self.__add__(first_node.next if first_node is not None else None, \n",
|
||||
" second_node.next if first_node is not None else None, \n",
|
||||
" new_carry)\n",
|
||||
" node.next = self.__add_reverse__(first_node.next if first_node is not None else None, \n",
|
||||
" second_node.next if first_node is not None else None, \n",
|
||||
" new_carry)\n",
|
||||
" return node\n",
|
||||
"\n",
|
||||
" def add(self, first_list, second_list):\n",
|
||||
" def add_reverse(self, first_list, second_list):\n",
|
||||
" if first_list is None or second_list is None:\n",
|
||||
" return None\n",
|
||||
" head = self.__add__(first_list.head, second_list.head, 0)\n",
|
||||
" head = self.__add_reverse__(first_list.head, second_list.head, 0)\n",
|
||||
" return MyLinkedList(head)"
|
||||
]
|
||||
},
|
||||
|
@ -175,11 +175,11 @@
|
|||
"\n",
|
||||
"class TestAddReverse(object):\n",
|
||||
" \n",
|
||||
" def test_add(self):\n",
|
||||
" def test_add_reverse(self):\n",
|
||||
" print('Test: Empty list(s)')\n",
|
||||
" assert_equal(MyLinkedList().add(None, None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(Node(5), None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(None, Node(10)), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(None, None), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(Node(5), None), None)\n",
|
||||
" assert_equal(MyLinkedList().add_reverse(None, Node(10)), None)\n",
|
||||
"\n",
|
||||
" print('Test: Add values of different lengths')\n",
|
||||
" # Input 1: 6->5->None\n",
|
||||
|
@ -190,7 +190,7 @@
|
|||
" second_list = MyLinkedList(Node(9))\n",
|
||||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
" result = MyLinkedList().add_reverse(first_list, second_list)\n",
|
||||
" assert_equal(result.get_all_data(), [5, 4, 8])\n",
|
||||
"\n",
|
||||
" print('Test: Add values of same lengths')\n",
|
||||
|
@ -205,14 +205,14 @@
|
|||
" second_list = MyLinkedList(second_head)\n",
|
||||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
" result = MyLinkedList().add_reverse(first_list, second_list)\n",
|
||||
" assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
|
||||
" \n",
|
||||
" print('Success: test_add')\n",
|
||||
" print('Success: test_add_reverse')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestAddReverse()\n",
|
||||
" test.test_add()\n",
|
||||
" test.test_add_reverse()\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" main()"
|
||||
|
@ -232,7 +232,7 @@
|
|||
"Test: Empty list(s)\n",
|
||||
"Test: Add values of different lengths\n",
|
||||
"Test: Add values of same lengths\n",
|
||||
"Success: test_add\n"
|
||||
"Success: test_add_reverse\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
|
@ -3,11 +3,11 @@ from nose.tools import assert_equal
|
|||
|
||||
class TestAddReverse(object):
|
||||
|
||||
def test_add(self):
|
||||
def test_add_reverse(self):
|
||||
print('Test: Empty list(s)')
|
||||
assert_equal(MyLinkedList().add(None, None), None)
|
||||
assert_equal(MyLinkedList().add(Node(5), None), None)
|
||||
assert_equal(MyLinkedList().add(None, Node(10)), None)
|
||||
assert_equal(MyLinkedList().add_reverse(None, None), None)
|
||||
assert_equal(MyLinkedList().add_reverse(Node(5), None), None)
|
||||
assert_equal(MyLinkedList().add_reverse(None, Node(10)), None)
|
||||
|
||||
print('Test: Add values of different lengths')
|
||||
# Input 1: 6->5->None
|
||||
|
@ -18,7 +18,7 @@ class TestAddReverse(object):
|
|||
second_list = MyLinkedList(Node(9))
|
||||
second_list.append(8)
|
||||
second_list.append(7)
|
||||
result = MyLinkedList().add(first_list, second_list)
|
||||
result = MyLinkedList().add_reverse(first_list, second_list)
|
||||
assert_equal(result.get_all_data(), [5, 4, 8])
|
||||
|
||||
print('Test: Add values of same lengths')
|
||||
|
@ -33,14 +33,14 @@ class TestAddReverse(object):
|
|||
second_list = MyLinkedList(second_head)
|
||||
second_list.append(8)
|
||||
second_list.append(7)
|
||||
result = MyLinkedList().add(first_list, second_list)
|
||||
result = MyLinkedList().add_reverse(first_list, second_list)
|
||||
assert_equal(result.get_all_data(), [5, 4, 2, 1])
|
||||
|
||||
print('Success: test_add')
|
||||
print('Success: test_add_reverse')
|
||||
|
||||
def main():
|
||||
test = TestAddReverse()
|
||||
test.test_add()
|
||||
test.test_add_reverse()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user