diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..4fccf096d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -3,13 +3,38 @@ ## Exercise I a) + a = 0 + while (a < n * n * n): + a = a + n * n +The runtime here is O(n). +There is a single while loop, which will perform more linear operations the larger n is. b) + sum = 0 + for i in range(n): + j = 1 + while j < n: + j *= 2 + sum += 1 +The runtime here is O(n^2). +There are two loops being used here. It starts with a for loop which contains a while loop. c) + def bunnyEars(bunnies): + if bunnies == 0: + return 0 + + return 2 + bunnyEars(bunnies-1) + +The runtime here is O(n) +This one is using recursion. Each call will perform an if check. ## Exercise II +"Suppose that you have an n-story building and plenty of eggs. Suppose also that an egg gets broken if it is thrown off floor f or higher, and doesn't get broken if dropped off a floor less than floor f. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized." +So this problem seems to be asking how to determine the value of f as soon as possible so that I don't lose too many +eggs. In this case, I would start by dropping the egg on the median floor and see if it breaks. If it does, then I +would take the halfway point between the first floor and the median and drop the egg there to once again see if it breaks. So pretty much, I would keep checking halves until I've pinpointed the last floor that the egg can survive from if it was dropped. The runtime of this would look something like O(n*n). \ No newline at end of file diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..52dffe106 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,9 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - - pass + th = 0 + if word.find("th") == -1: + return th + else: + word.find("th") + return 1 + count_th(word.replace("th", " ", 1)) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..43eb13477 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,21 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + if self.can_move_right() == False: + return + self.swap_item() + while True: + while self.can_move_right(): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + while self.compare_item() != None: + self.move_left() + self.swap_item() + if not self.can_move_right(): + return + self.move_right() + self.swap_item() if __name__ == "__main__":