From 0f5b8b21861c2fd63618d621baae645bfeb7eb8a Mon Sep 17 00:00:00 2001 From: Kelson Hartle Date: Sun, 11 Oct 2020 20:25:30 -0600 Subject: [PATCH] MVP --- Short-Answer/Algorithms_Answers.md | 6 +++--- recursive_count_th/count_th.py | 14 ++++++++++++-- robot_sort/robot_sort.py | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..2cac1c685 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,13 +2,13 @@ ## Exercise I -a) +a) linear time O(n)- because the input size will stay at the same rate the whole time. Which indicates linear. -b) +b) Linearithmic time O(n log n) - because the j will continue to grow in the inner loop O(log n) even though the outer loop is linear. -c) +c) linear O(n) - The function is recursive and will call itself 'n' number of times indicating linear. And adding 2 two the recursive call is also linear which is a constant time operation. ## Exercise II diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..1b2a24de2 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +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): - # TBC + print(word[:2]) + + if len(word) <= 1: + return 0 - pass + elif word[0] == "t": + if word[1] == "h": + return 1 + count_th(word[1:]) + else: + return count_th(word[1:]) + else: + return count_th(word[1:]) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..1e84a7512 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,8 +97,23 @@ def sort(self): Sort the robot's list. """ # Fill this out - pass + if self.can_move_right(): + self.swap_item() + while self.can_move_right(): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + if not self.can_move_right(): + while self.compare_item() != None: + if self.can_move_left(): + self.move_left() + + self.swap_item() + self.move_right() + self.swap_item() + + self.swap_item() if __name__ == "__main__": # Test our your implementation from the command line