From 50df0f8544da9795562c44d69dd725a051fe0a14 Mon Sep 17 00:00:00 2001 From: Pravin Patel Date: Sat, 10 Oct 2020 13:03:53 -0500 Subject: [PATCH 1/4] update Algorithms, count and robot --- Short-Answer/Algorithms_Answers.md | 9 ++++----- recursive_count_th/count_th.py | 12 +++++++++--- robot_sort/robot_sort.py | 5 +++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..7ea195e85 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,13 @@ ## 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 I move to middle of the floors below and repeat. If the egg doesn't break, I 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..cac6b3e39 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,6 +97,11 @@ def sort(self): Sort the robot's list. """ # Fill this out + if self.light_is_on() != "ON": + self.set_light_on() + + while self.light_is_on(): + pass From 029a53eef248622326bd43c21fb6588870d21f18 Mon Sep 17 00:00:00 2001 From: Pravin Patel Date: Sat, 10 Oct 2020 13:47:50 -0500 Subject: [PATCH 2/4] update Algorithms, and robot --- Short-Answer/Algorithms_Answers.md | 2 +- robot_sort/robot_sort.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index 7ea195e85..f0190537d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -11,4 +11,4 @@ c) Runtime complexity is O(n) because here it's calling the recursive function w ## 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 I move to middle of the floors below and repeat. If the egg doesn't break, I move to the middle of the floors above and repeat. O(logN) +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/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index cac6b3e39..07e48767c 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -101,7 +101,8 @@ def sort(self): self.set_light_on() while self.light_is_on(): - + if self.compare_item() == 1: + self.swap_item() pass From 1b31a228fd767179f8d9b7bf6706c0aae1ae0df7 Mon Sep 17 00:00:00 2001 From: Pravin Patel Date: Sun, 11 Oct 2020 11:42:49 -0500 Subject: [PATCH 3/4] update Algorithms --- Short-Answer/Algorithms_Answers.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index f0190537d..39eaca3b7 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -11,4 +11,5 @@ c) Runtime complexity is O(n) because here it's calling the recursive function w ## 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) +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) From 067460b1be8e3ce61a13328005e0490db029b93e Mon Sep 17 00:00:00 2001 From: Pravin Patel Date: Mon, 12 Oct 2020 10:39:55 -0500 Subject: [PATCH 4/4] update robot --- robot_sort/robot_sort.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index 07e48767c..4437598e0 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -97,14 +97,34 @@ def sort(self): Sort the robot's list. """ # Fill this out - if self.light_is_on() != "ON": - self.set_light_on() - - while self.light_is_on(): + # 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() - pass + # 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 @@ -115,4 +135,4 @@ def sort(self): robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list)