diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..71da6d24c 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,68 @@ ## Exercise I -a) +a) This looks like a 0_(n) runtime. The ammount of itterations cales with n -b) +b) The number of iterations climbs by a reverse factor of n. The iteration is a graph logarithmic. +0_(log n) -c) +c) This will return one iteration of "bunny ears"(n) passed to it. If "bunny ears" us n, then this is 0_(n) ## Exercise II +Eggs: + + class Humpty_Dumpty(intact=true, crack_threshold): + def __init(self): + self.intact = intact + self.crack_threshold = crack_threshold + + def crack(self): + self.intact = False + + def falls(self, floor): + if floor >= crack_threshold: + self.crack() + +Now we have Humpty_Dumpty class that reprents an egg. +If humpty dumpty falls from the floot of its crack threshold or higher he will crack + +Now to devise a test cycle to test humpty dumpty at our building +we will go to the center floor of the bulding and see if humpty dumpty cracks. If he does were either at or above the crack +threshold. We should test agaun halfway between our current floor and the ground fllor to see if we can get below and narrow +it (search lower); if not then we'll search half way between ourselves and the roof to see if humpty dumpty will break. +this allows us to cut off half the unsearched floors with each iteration (0_(log n) runtime) + +This is psuedo--y but it should get to the point: + +def find_threshold(building, carton, min = None, max = None): + if min is None and max is None: + min = 0 + max = len(building) - 1 + + if min > max: + return -1 + + if min == max: + carton[0].falls() + if carton[0] is intact: + carton.remove(0) + return "Humpty dumpty wont break from the floor" + else: + carton.remove(0) + return search_floor + + else: + search_floor = (min + max) // 2 + carton[0].falls() + + if carton[0] is intact: + carton.remove(0) + return find_threshold(building, carton,search_floor max) + elif carton[0] is not intact: + carton.remove(0) + return find_threshold(building, carton, min, search_floor) + diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..25e1ddc61 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +3,12 @@ Your function should return a count of how many occurences of ***"th"*** occur within `word`. Case matters. Your function must utilize recursion. It cannot contain any loops. ''' -def count_th(word): +def count_th(word, total = 0): + if len(word) < 2: + return total - # TBC - - pass + if word[:2] == 'th': + total += 1 + return count_th(word[2:], total) + else: + return count_th(word[1:], total) \ No newline at end of file diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..95b39ba49 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,7 +97,27 @@ def sort(self): Sort the robot's list. """ # Fill this out - pass + self.set_light_on() + while self.light_is_on(): + self.set_light_off() + self.swap_item() + + while self.can_move_right(): + self.move_right() + + if self.compare_item() == 1: + self.swap_item() + self.set_light_on() + + while self.can_move_left(): + if self.compare_item() != None: + self.move_left() + else: + break + + self.swap_item() + self.move_right() + if __name__ == "__main__": 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__':