From 26381d4a44eb893dbd952048a7490a7bf06adae3 Mon Sep 17 00:00:00 2001 From: Chawn Christian Date: Sat, 10 Oct 2020 12:22:11 -0400 Subject: [PATCH 1/2] Recursive sort, complete --- recursive_count_th/count_th.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..2e761d5d7 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,11 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): + if len(word) == 0: + return 0 - # TBC - - pass + elif word[0:2] == "th": + return count_th(word[1:]) + 1 + else: + return count_th(word[1:]) + From a27065910857cf56e82dde4b0b0c7bc2c92ef59a Mon Sep 17 00:00:00 2001 From: Chawn Christian Date: Sat, 10 Oct 2020 13:41:32 -0400 Subject: [PATCH 2/2] Robot sort and answers completed --- Short-Answer/Algorithms_Answers.md | 15 ++++++++++++--- robot_sort/robot_sort.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..b952d36bc 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -3,13 +3,22 @@ ## Exercise I a) - +Single while statements causes O(n) b) - +Nested loops create (O(n^2)) c) - +Single if statements that will eventually lead to 0 creates O(n) ## Exercise II +i = 0 +while f == i: + if egg= unbroken: + i ++ + + else: + return i +this should be O(log n) + diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..9ece1ea0c 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,34 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + while not self.light_is_on(): + self.set_light_on() + + while self.can_move_right(): + self.swap_item() + self.move_right() + + if self.compare_item() == 1: + self.swap_item() + self.set_light_off() + + self.move_left() + self.swap_item() + self.move_right() + + + + while self.can_move_left(): + self.swap_item() + self.move_left() + + if self.compare_item() == -1: + self.swap_item() + self.set_light_off() + + self.move_right() + self.swap_item() + self.move_left() if __name__ == "__main__":