diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..1ebaaba90 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,18 @@ ## Exercise I -a) +a) The runtime complexity is O(n). This is because with each iteration of the while loop, 'a' increases by 'n * n', so after 'n' iterations 'a' will be equal to 'n * (n * n)' which matches the while loop's conditional check of 'a < n * n * n'. -b) +b) The runtime complexity is O(n*log(n)). This is because there are nested loops: the outer 'for' loop will run 'n' times, and the inner 'while' loop will run log(n) times. The inner loop has a log(n) time complexity because the value of 'j' doubles with each iteration, so 'j' will increase quadradically and only take log(n) iterations to reach the value of 'n'. -c) +c) The runtime complexity is O(n). This is because the function is recursive and will call itself 'n' times. When it hits its base case, the function will backtrack through all 'n' function calls, adding '2' to the return value each time. Since adding '2' to another value is a constant time operation, and it is done 'n' times, the final runtime complexity is O(n). ## Exercise II + diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..5931c9d69 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,10 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): + if len(word) < 2: + return 0 - # TBC - - pass + if word[:2] == "th": + return 1 + count_th(word[2:]) + else: + return count_th(word[1:]) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..9e243aafa 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -92,12 +92,40 @@ def light_is_on(self): """ return self._light == "ON" + # This sort() method mimics Selection Sort + # Time Complexity: O(n^2) + # Space Complexity: O(1) + # Selection sort always performs O(n^2) comparisons, but only O(n) swaps (somewhere between 0 - n) def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + # The robot starts at index 0, so if the robot cannot move right then there is + # at most one item in the list (i.e. the list is already sorted) + if not self.can_move_right(): + return + + # Pick up the first item in the list + self.swap_item() + + while True: + # Traverse to the last item in the list, swapping as needed in order to hold the smallest item + while self.can_move_right(): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + # The robot is now at the end of the list and holding the smallest item + + # Traverse left to the index where this latest iteration started + while self.compare_item() != None: + self.move_left() + + # Drop the smallest item in place and move right one item and pick it up + self.swap_item() + if not self.can_move_right(): + return + self.move_right() + self.swap_item() if __name__ == "__main__":