From 0adf6c4be0e88cceb3802924cbf6802af698cd13 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 5 Jul 2015 09:48:05 -0400 Subject: [PATCH] Tweaked add reverse challenge. --- .../add_reverse/add_reverse_challenge.ipynb | 4 +-- .../add_reverse/add_reverse_solution.ipynb | 26 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/linked_lists/add_reverse/add_reverse_challenge.ipynb b/linked_lists/add_reverse/add_reverse_challenge.ipynb index 345784a..d44c956 100644 --- a/linked_lists/add_reverse/add_reverse_challenge.ipynb +++ b/linked_lists/add_reverse/add_reverse_challenge.ipynb @@ -38,8 +38,8 @@ "\n", "* Do you expect the return to be in reverse order too?\n", " * Yes\n", - "* What if one of the inputs is NULL?\n", - " * Return NULL for an invalid operation\n", + "* What if one of the inputs is None?\n", + " * Return None for an invalid operation\n", "* How large are these numbers--can they fit in memory?\n", " * Yes\n", "* Can we assume we already have a linked list class that can be used for this problem?\n", diff --git a/linked_lists/add_reverse/add_reverse_solution.ipynb b/linked_lists/add_reverse/add_reverse_solution.ipynb index 5d19963..e2db2a3 100644 --- a/linked_lists/add_reverse/add_reverse_solution.ipynb +++ b/linked_lists/add_reverse/add_reverse_solution.ipynb @@ -37,8 +37,8 @@ "\n", "* Do you expect the return to be in reverse order too?\n", " * Yes\n", - "* What if one of the inputs is NULL?\n", - " * Return NULL for an invalid operation\n", + "* What if one of the inputs is None?\n", + " * Return None for an invalid operation\n", "* How large are these numbers--can they fit in memory?\n", " * Yes\n", "* Can we assume we already have a linked list class that can be used for this problem?\n", @@ -73,17 +73,17 @@ " * if first and second lists are NULL AND carry is zero\n", " * Return NULL\n", "* Recursive case:\n", - " * value = carry\n", - " * value += first.data + second.data\n", - " * remainder = value % 10\n", - " * new_carry = 1 if value >= 10, else 0\n", - " * Create a node with the remainder\n", - " * node.next = self.add(first.next, second.next, new_carry)\n", - " * Return node\n", + " * Set `value` to `carry`\n", + " * Add both nodes' `data` to `value`\n", + " * Set the `new_carry` to 1 if `value >= 10, else 0`\n", + " * Set the `remainder` to `value % 10`\n", + " * Create a `node` with the `remainder`\n", + " * Set `node.next` to a recursive call on the `next` nodes, passing in the `carry`\n", + " * Return `node`\n", "\n", "Complexity:\n", "* Time: O(n)\n", - "* Space: O(n), extra space for result and recursion depth\n", + "* Space: O(m), extra space for result and recursion depth\n", "\n", "Notes:\n", "* Careful with adding if the lists differ\n", @@ -131,12 +131,12 @@ " value = carry\n", " value += first_node.data if first_node is not None else 0\n", " value += second_node.data if second_node is not None else 0\n", - " remainder = value % 10\n", " new_carry = 1 if value >= 10 else 0\n", + " remainder = value % 10\n", " node = Node(remainder)\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", + " second_node.next if first_node is not None else None, \n", + " new_carry)\n", " return node\n", "\n", " def add_reverse(self, first_list, second_list):\n",