From c2cbf58fe00576fb25b2338b70ce385a56a11db8 Mon Sep 17 00:00:00 2001 From: Alex Thompson Date: Sat, 10 Oct 2020 14:12:46 -0500 Subject: [PATCH 1/4] Recursive count --- recursive_count_th/count_th.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..eb1f0a649 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +3,15 @@ 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) + + +count_th('hooowwwhdthenkjsjkd', 5) \ No newline at end of file From 7323ad37633aa4209d58f4c2cff4f6ee4b2705d5 Mon Sep 17 00:00:00 2001 From: Alex Thompson Date: Sun, 11 Oct 2020 18:29:24 -0500 Subject: [PATCH 2/4] Implemented list sort --- robot_sort/robot_sort.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..cdcc026b3 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__": From bfdd99924a152c60abf6205ccf5a587c73d33a96 Mon Sep 17 00:00:00 2001 From: Alex Thompson Date: Sun, 11 Oct 2020 19:13:40 -0500 Subject: [PATCH 3/4] Algorithm answers --- Short-Answer/Algorithms_Answers.md | 60 ++++++++++++++++++++++++++++-- recursive_count_th/count_th.py | 5 +-- robot_sort/test_robot.py | 38 +++++++++---------- 3 files changed, 77 insertions(+), 26 deletions(-) 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 eb1f0a649..25e1ddc61 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -11,7 +11,4 @@ def count_th(word, total = 0): total += 1 return count_th(word[2:], total) else: - return count_th(word[1:], total) - - -count_th('hooowwwhdthenkjsjkd', 5) \ No newline at end of file + return count_th(word[1:], total) \ 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__': From 65f50df8703d0ddbaa1254ba259fe874ebe77ffb Mon Sep 17 00:00:00 2001 From: Alex Thompson Date: Mon, 12 Oct 2020 22:54:27 -0500 Subject: [PATCH 4/4] Fixed robot --- robot_sort/robot_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index cdcc026b3..95b39ba49 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -100,7 +100,7 @@ def sort(self): self.set_light_on() while self.light_is_on(): self.set_light_off() - self.swap_item + self.swap_item() while self.can_move_right(): self.move_right()