diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..cf5feef92 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,23 @@ ## Exercise I -a) +a)My analysis of this snippet's runtime is Linear, or O(n). We start at O(n^3), +but reduce by O(n^2) that is incrementing inside the loop. Thus making O(n). +b)My analysis of this snippet's runtime is Logarithmic, or O(log n). As the size +of the input increases, the runtime also grows at a small rate. -b) - - -c) +c)My analysis of this snippet is Linear, or O(n). We only decrease bunnies at a +rate of 1. ## Exercise II - - +In order for this to work, we want to throw the eggs off of floors below floor 'f'. + +To make this successfull, I would start with a Binary Search. This way, we start at +the middle floor and drop an egg. If the egg breaks, all floors above that can be +ignored, returning a new set of floors. Then we try again at the middle point of the +new floors, and if the egg doesn't break, the floors below that can be ignored, once +again shortening that list and repeating the process until we find where the egg +doesn't break on one floor, but it will on the floor directly above it. Due to the +continual halving of the floors, and with us using the Binary Search Tree, +the algorithm will be O(log(n)). diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..1e1471fe2 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,20 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - + # if the length of the word is less than 2: + if len(word) < 2: + # return 0 + return 0 + # if the word contains t or h + if word[0] == 't' and word[1] == 'h': + # return count of 1 + count of 'th' sliced + return 1 + count_th(word[2:]) + else: + # else return through word + return count_th(word[1:]) pass + +print(count_th("health")) +print(count_th("fairy")) +print(count_th("thanks")) +print(count_th("theatherth")) \ No newline at end of file diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..008ea98da 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,32 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + # if not able to move right + if not self.can_move_right(): + return + self.swap_item() + # while True... + while True: + # while able to move right + while self.can_move_right(): + # move right + self.move_right() + # if new item is greater than current item + if self.compare_item() == 1: + # swap them + self.swap_item() + # while items to compare is not None + while self.compare_item() != None: + # move left until done + self.move_left() + # ... swap items + self.swap_item() + # if no longer able to move right + if not self.can_move_right(): + # we're done! + return + self.move_right() + self.swap_item() if __name__ == "__main__": @@ -109,4 +133,6 @@ def sort(self): robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list) + print(robot._time) + print(robot._light) \ No newline at end of file diff --git a/robot_sort/test_robot.py b/robot_sort/test_robot.py index 57fabb5e0..16c8380ec 100644 --- a/robot_sort/test_robot.py +++ b/robot_sort/test_robot.py @@ -36,25 +36,25 @@ def test_sorting_random_list(self): robot.sort() self.assertEqual(robot._list, sorted(self.random_list)) - # def test_stretch_times(self): - # robot = SortingRobot(self.small_list) - # robot.sort() - # self.assertLess(robot._time, 110) - - # robot = SortingRobot(self.medium_list) - # robot.sort() - # print(robot._time) - # self.assertLess(robot._time, 1948) - - # robot = SortingRobot(self.large_list) - # robot.sort() - # print(robot._time) - # self.assertLess(robot._time, 27513) - - # robot = SortingRobot(self.large_varied_list) - # robot.sort() - # print(robot._time) - # self.assertLess(robot._time, 28308) + def test_stretch_times(self): + robot = SortingRobot(self.small_list) + robot.sort() + self.assertLess(robot._time, 110) + + robot = SortingRobot(self.medium_list) + robot.sort() + print(robot._time) + self.assertLess(robot._time, 1948) + + robot = SortingRobot(self.large_list) + robot.sort() + print(robot._time) + self.assertLess(robot._time, 27513) + + robot = SortingRobot(self.large_varied_list) + robot.sort() + print(robot._time) + self.assertLess(robot._time, 28308) if __name__ == '__main__':