diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..39eaca3b7 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,14 @@ ## Exercise I -a) +a) Runtime complexity is O(n) because 'n' increases in size by one +b) Runtime complexity is O(n) because here we have nested loops. The first loop runs 'n' times and second loop is not re-iterating only using it as a comparision. -b) - -c) +c) Runtime complexity is O(n) because here it's calling the recursive function which will call itself 'n' times. We are returning a constant value and not looping over anything even if you are recursing one. ## Exercise II - +Since we are aware of the floors and are sorted, recommend using binary search, start on the middle floor and drop an egg. if the egg breaks, then move to middle of the floors below and repeat. If the egg doesn't break, move to the middle of the floors above and repeat. +O(logN) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..1acabc809 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +3,14 @@ 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): +def count_th(word, count = 0): # TBC - - pass + l = list(word) + + if len(l) <= 1: + return count + + if l[0:2] == ['t', 'h']: + count += 1 + return count_th("".join(l[1:]), count) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..4437598e0 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,8 +97,34 @@ def sort(self): Sort the robot's list. """ # Fill this out - pass + # Check if we can move right + if self.can_move_right() is False: + return self._list + # Swap the item + self.swap_item() + + # Move to the right while we can + while self.can_move_right() is True: + self.move_right() + + # Swap if we need to + if self.compare_item() == 1: + self.swap_item() + + # Move to the left while we can + while self.can_move_left() is True: + self.move_left() + + # Swap if we need to + if self.compare_item() == None: + self.swap_item() + break + + # Move to the Right + self.move_right() + # Recursivly call ourself + self.sort() if __name__ == "__main__": # Test our your implementation from the command line @@ -109,4 +135,4 @@ def sort(self): robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list)