diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..7b11eb44a 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,13 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): + if len(word) < 2: + return 0 - # TBC - - pass + if word[:2] == "th": + return 1 + count_th(word[2:]) + else: + return count_th(word[1:]) + + +print(count_th('worhit')) \ No newline at end of file diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..34bcf22ad 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,32 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + # The robot starts at index 0, so if the robot cannot move right then there is + # at most one item in the list (i.e. the list is already sorted) + if not self.can_move_right(): + return + + # Pick up the first item in the list + self.swap_item() + + while True: + # Traverse to the last item in the list, swapping as needed in order to hold the smallest item + while self.can_move_right(): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + # The robot is now at the end of the list and holding the smallest item + + # Traverse left to the index where this latest iteration started + while self.compare_item() != None: + self.move_left() + + # Drop the smallest item in place and move right one item and pick it up + self.swap_item() + if not self.can_move_right(): + return + 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..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__': diff --git a/test.py b/test.py new file mode 100644 index 000000000..da9ecbaeb --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ +# name = input('Enter your name: ') +# print('Your name is: ',name) + +# if name == 'Hamid': +# print('Welcome Admin') +# else: +# print('Sorry your not the admin') + +def recursive_fib(n): + if n < 1: + print("Incorrect input.") + elif n == 1: + return 0 + elif n == 2: + return 1 + else: + return recursive_fib(n - 1) + recursive_fib(n - 2) + +n = input(int()) +recursive_fib(n) \ No newline at end of file