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..d136432af 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -6,5 +6,15 @@ def count_th(word): # TBC + if word == "": + return 0 + else: + if word.find("th") == -1: + return 0 + else: + newindex = word.find("th") + newword = word[newindex+2:] + + return(1+count_th(newword)) pass 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__':