From 62ed5776783233781894305c256d7e0ec4d2643f Mon Sep 17 00:00:00 2001 From: Jeff Breig Date: Sun, 11 Oct 2020 21:14:30 -0400 Subject: [PATCH 1/4] finished short answers --- Short-Answer/Algorithms_Answers.md | 9 ++++++--- recursive_count_th/count_th.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..3230afc4d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,17 @@ ## Exercise I -a) +a) This code runs while a (which is equal to a + n^2) is less than n^3. This actually results in the code running n times. For example, if n is 3, a will be 9 on the first pass, 18 on the second, and 27 on the next, terminating the loop. +This means that the code running directly corresponds to the input size, which is O(n). -b) +b) This code has 2 loops, one of which (the for loop) corresponds linearly with input size n, and the second, nested while loop will almost always run fewer times than input size n, logarithmically. This is O(n log(n)) time. -c) +c) This recursive function runs linearly. The number of times the function is called is in direct, linear correlation to the input size, which is O(n). + ## Exercise II +This situation is a good one in which to use a binary search algorithm. First, I would find the midpoint between the ground floor and the top floor. I would then drop an egg from this floor. If it breaks, I would know to pick a lower floor, so would again pick a midpoint between where I had just dropped the egg and the ground level. If it didn't break, I would pick a midpoint between where I had dropped the egg from and the top floor. I would repeat this process until I was able to determine floor f. This algorithm runs in O(log(n)) time. diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..4dfd7a486 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -6,5 +6,16 @@ def count_th(word): # TBC + count = 0 + if word == "": + return 0 + else: + if word.find("th") == -1: + return count + else: + newindex = word.find("th") + newword = word[newindex:] + count +=1 + return(count_th(newword)) pass From 4db5f932074d9f8bcb515774d64256f3f5b0d9bf Mon Sep 17 00:00:00 2001 From: Jeff Breig Date: Sun, 11 Oct 2020 21:20:09 -0400 Subject: [PATCH 2/4] finished recursive counting --- recursive_count_th/count_th.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 4dfd7a486..025684e6e 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,19 +3,19 @@ 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, count = 0): # TBC - count = 0 if word == "": - return 0 + return count else: if word.find("th") == -1: return count else: newindex = word.find("th") - newword = word[newindex:] - count +=1 - return(count_th(newword)) + newword = word[newindex+2:] + count += 1 + print(count) + return(count_th(newword, count)) pass From b6e9e85acaa522daae7818927a815d534155cd03 Mon Sep 17 00:00:00 2001 From: Jeff Breig Date: Sun, 11 Oct 2020 21:28:53 -0400 Subject: [PATCH 3/4] refactored to use one parameter --- recursive_count_th/count_th.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 025684e6e..d136432af 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,19 +3,18 @@ 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, count = 0): +def count_th(word): # TBC if word == "": - return count + return 0 else: if word.find("th") == -1: - return count + return 0 else: newindex = word.find("th") newword = word[newindex+2:] - count += 1 - print(count) - return(count_th(newword, count)) + + return(1+count_th(newword)) pass From a3a0979d7e8c29033f21e3dd188950161177aa72 Mon Sep 17 00:00:00 2001 From: Jeff Breig Date: Sun, 11 Oct 2020 22:51:34 -0400 Subject: [PATCH 4/4] working robot, not passing stretch yet, mvp --- robot_sort/robot_sort.py | 24 ++++++++++++++++++++++++ robot_sort/test_robot.py | 38 +++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..93696c3be 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,6 +97,30 @@ def sort(self): Sort the robot's list. """ # Fill this out + self.set_light_on() + + while self.light_is_on(): + self.set_light_off() + + while(self.can_move_right()): + self.swap_item() + self.move_right() + + if self.compare_item() == 1: + self.swap_item() + self.set_light_on() + self.move_left() + self.swap_item() + self.move_right() + + else: + self.move_left() + self.swap_item() + self.move_right() + + while(self.can_move_left()): + self.move_left() + pass 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__':