diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..f9c254ccf 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,18 @@ ## Exercise I -a) +a) O(n^3) because its length is dependent on n multiplied by itself 3 times. -b) +b) 0(n log n) because it goes one loop dependent on length of n and a second that is a subset of n. -c) +c) O(n) because it is running through "bunnies" aka our "n" once until it is 0. ## Exercise II +Divide the number of floors in half and toss the egg. +If it doesn't break repeat with taking the middle of upper half of floors (middle to last and keep changing the middle) +If it does break repeat with taking the middle of the first half of floors (first to last and keep changing the middle) +This will be 0(log n) because we are dividing n into smaller sets and not checking all n diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..95724e3ec 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -5,6 +5,13 @@ ''' def count_th(word): - # TBC + #base case, stop counting + if len(word) < 2: + return 0 - pass + #if found return 1 and count from the next letters + if word[:2] == "th": + return 1+ count_th(word[2:]) + #else return 0 and count from the next letters + else: + return 0 + count_th(word[1:]) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..dbfbbb3e9 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,35 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + + # if robot cannot move right, return + if not self.can_move_right: + return + + #pickup initial item + self.swap_item() + + while True: + #move right and keep swapping to get the lowest item in the rest of the list + while self.can_move_right(): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + + # then while we haven't reached None, move left + while self.compare_item() != None: + self.move_left() + + #drop the item into None + self.swap_item() + + #if we can't move right, we're done sorting + if not self.can_move_right(): + return + + #move right and pick up the next item + self.move_right() + self.swap_item() if __name__ == "__main__": diff --git a/robot_sort/test_robot.py b/robot_sort/test_robot.py index 57fabb5e0..60a81ba3f 100644 --- a/robot_sort/test_robot.py +++ b/robot_sort/test_robot.py @@ -36,25 +36,26 @@ 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() + print(robot._time) + 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__':