From 12457904ce07d461f98af5d01ed3f879aa7c7867 Mon Sep 17 00:00:00 2001 From: Matt Martindale Date: Sun, 11 Oct 2020 19:08:43 -0600 Subject: [PATCH 1/4] Initial commit --- Short-Answer/Algorithms_Answers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..f5b4fa9ef 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,6 +2,7 @@ ## Exercise I + a) From 7df84b8a9a63c328e271321b676611f012a118aa Mon Sep 17 00:00:00 2001 From: Matt Martindale Date: Sun, 11 Oct 2020 19:26:26 -0600 Subject: [PATCH 2/4] Answered exercise questions --- Short-Answer/Algorithms_Answers.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index f5b4fa9ef..0bee03972 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -3,14 +3,18 @@ ## Exercise I -a) +a) Linear time: Although we are doing ’n’ cubed in the conditional and squaring it in the loop, the size of ’n’ has no bearing on how the function will scale. We are still doing a constant calculation overall. -b) +b) n log n: The outer loop is going through an ’n’ number of times. And inside the inner while loop ‘j’ is being doubled, so the while loop would finish twice as fast as the value of ’n’. So there is a nested for loop and overall we go with the longest time complexity, which is n log n -c) +c) Linear time: The function calls itself recursively subtracting ‘bunnies’ which is ’n’ and adding 2 to each iteration until n reaches 0. So while the function is a recursive function it only runs ’n’ number of times while adding 2 (which is a constant time complexity) ## Exercise II +This sounds like a great use of using a Binary structure. + I would find out which floor is in the middle from the top and bottom floor and drop an egg. If the egg breaks I would go to the floor half way between the bottom and my current floor and repeat for every time the egg breaks. + If the egg doesn’t break for whichever floor I’m testing, I would go half way up from my current floor to the highest floor that I tested last, and repeat. If it broke on my first test, I would go halfway up from the middle floor to the top floor. And repeat the process. +The time complexity of my testing would be a Log N, as I’m cutting the required amount of times I have to drop an egg in half each time. From 6c68c221786456771e9a913ceaef6a0fc51bda08 Mon Sep 17 00:00:00 2001 From: Matt Martindale Date: Sun, 11 Oct 2020 20:15:51 -0600 Subject: [PATCH 3/4] Finished count_th.py --- recursive_count_th/count_th.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..86d5d03f5 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): - - # TBC - - pass + +def count_th(word, index=2, count=0): + if word == "": + return 0 + if index > len(word): + return count + if "th" in word[(index-2):index]: + count += 1 + + index += 1 + return count_th(word, index, count) From 383ba5f40af972d1d025b6374f705937ef8a6ea1 Mon Sep 17 00:00:00 2001 From: Matt Martindale Date: Sun, 11 Oct 2020 21:21:01 -0600 Subject: [PATCH 4/4] Finished robot sort with Stretch goal --- robot_sort/robot_sort.py | 20 ++++++++++++++++++-- robot_sort/test_robot.py | 38 +++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..7ee1b703c 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,24 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + while self.light_is_on() == False: + # turn the light ON each loop + self.set_light_on() + + while self.can_move_right() == True: + self._item = self._list[self._position] + + self.move_right() + if self.compare_item() == 1: + self.swap_item() + # if a swap happens during the loop, turn the light OFF + self.set_light_off() + self._list[self._position - 1] = self._item + elif self.compare_item() == -1: + self._list[self._position - 1] = self._item + + # restart at the beginning if the light is turned OFF + self._position = 0 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__':