From 122355d6179962b29bae506d76203f2b73124712 Mon Sep 17 00:00:00 2001 From: Adrian Guadalupe Date: Sat, 10 Oct 2020 12:53:09 -0400 Subject: [PATCH 1/3] short answers done --- Short-Answer/Algorithms_Answers.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..ce7ca91bd 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,22 @@ ## Exercise I -a) +a) O(n) +The function will run exactly n times. ex: if n = 5, the function will run 5 times, as long as a is less than 125. On the first loop a will be set to 25, the second 50, on the 5, 125 and stop. -b) +b) O(n^2) +While not exact thanks to the doubling of j, the runtime does curve ever sharper upwards as the numbers get larger as is typical of nested loops that both run until n. It is twice as slow as O(n) by the time n = 3. -c) +c) O(n) + +For an input size of n = 10, n = 15, n = 20 the algorithm does 11, 16, and 21 iteration respectively. This follows a linear line. ## Exercise II +If the goal is simply to have the least amount of dropped eggs be broken, an iterative approach where each egg is dropped from the lowest floor on up until 1 egg breaks would be the best approach, with a run time complexity of O(n). + +If the goal is to have the lowest total of eggs dropped while trying to keep broken ones minimal as well, then the divide and conquer method would likely be best. A binary search, where you drop the egg from the middle floor and then if it doesn't break, discard the bottom half and drop in the middle of the top half, halving each time to the top or bottom depending on whether the egg did or did not break. +This would have a run time complexity of O(logn) and would be far faster than the iterative option with larger buildings. From 0693982b9e7cee832758ddaa3c07932f677bcd43 Mon Sep 17 00:00:00 2001 From: Adrian Guadalupe Date: Sat, 10 Oct 2020 13:00:49 -0400 Subject: [PATCH 2/3] count_th done --- recursive_count_th/count_th.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..157a7507a 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,16 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - - pass + index = 0 + counter = 0 + def inner(word): + nonlocal index + nonlocal counter + if index > len(word) - 2: + return counter + else: + if word[0] == 't' and word[1] == 'h': + counter += 1 + return inner(word[index + 1:]) + inner(word) + return counter From 29b60d0a1984a24da32a824e0bce8e554fed7042 Mon Sep 17 00:00:00 2001 From: Adrian Guadalupe Date: Sat, 10 Oct 2020 13:49:19 -0400 Subject: [PATCH 3/3] done --- robot_sort/robot_sort.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..858a069f6 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,26 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + while self.can_move_right() : + self.swap_item() + self.move_right() + if self.compare_item() == 1: + self.set_light_on() + self.swap_item() + self.move_left() + self.swap_item() + self.move_right() + elif self.compare_item() == -1 or self.compare_item() == 0: + self.move_left() + self.swap_item() + self.move_right() + if self.can_move_right() == False: + if self.light_is_on() == False: + break + else: + while self.can_move_left() : + self.move_left() + self.set_light_off() if __name__ == "__main__":