From 7956f5ed00570daf7e03dec5dbfcda7b0aa34ff6 Mon Sep 17 00:00:00 2001 From: aidanvu1992 <52683477+aidanvu1992@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:31:26 -0700 Subject: [PATCH] Completed Sprint Challenge --- Short-Answer/Algorithms_Answers.md | 30 +-- Short-Answer/Algorithms_Questions.md | 72 +++--- .../__pycache__/count_th.cpython-37.pyc | Bin 0 -> 629 bytes recursive_count_th/count_th.py | 25 +- recursive_count_th/test_count_th.py | 74 +++--- .../__pycache__/robot_sort.cpython-37.pyc | Bin 0 -> 4566 bytes robot_sort/robot_sort.py | 243 ++++++++++-------- robot_sort/test_robot.py | 122 ++++----- 8 files changed, 296 insertions(+), 270 deletions(-) create mode 100644 recursive_count_th/__pycache__/count_th.cpython-37.pyc create mode 100644 robot_sort/__pycache__/robot_sort.cpython-37.pyc diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..d5f78875d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -1,15 +1,15 @@ -#### Please add your answers to the ***Analysis of Algorithms*** exercises here. - -## Exercise I - -a) - - -b) - - -c) - -## Exercise II - - +#### Please add your answers to the ***Analysis of Algorithms*** exercises here. + +## Exercise I + +a) It's a linear complexity, O(n) since there is one input and output + + +b) It's a quadratic complexity, O(n^2) as the for while iterates n * the number of for loops. n * n + + +c) It's a linear complexity as O(n) the function is called recursively n times before it reaches its base case + +## Exercise II + + diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 46f444535..b5a8d3bb9 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -1,36 +1,36 @@ -# Analysis of Algorithms - -## Exercise I - -Give an analysis of the running time of each snippet of -pseudocode with respect to the input size n of each of the following: - -```python -a) a = 0 - while (a < n * n * n): - a = a + n * n -``` - - -``` -b) sum = 0 - for i in range(n): - j = 1 - while j < n: - j *= 2 - sum += 1 -``` - -``` -c) def bunnyEars(bunnies): - if bunnies == 0: - return 0 - - return 2 + bunnyEars(bunnies-1) -``` - -## Exercise II - -Suppose that you have an n-story building and plenty of eggs. Suppose also that an egg gets broken if it is thrown off floor f or higher, and doesn't get broken if dropped off a floor less than floor f. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized. - -Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. +# Analysis of Algorithms + +## Exercise I + +Give an analysis of the running time of each snippet of +pseudocode with respect to the input size n of each of the following: + +```python +a) a = 0 + while (a < n * n * n): + a = a + n * n +``` + + +``` +b) sum = 0 + for i in range(n): + j = 1 + while j < n: + j *= 2 + sum += 1 +``` + +``` +c) def bunnyEars(bunnies): + if bunnies == 0: + return 0 + + return 2 + bunnyEars(bunnies-1) +``` + +## Exercise II + +Suppose that you have an n-story building and plenty of eggs. Suppose also that an egg gets broken if it is thrown off floor f or higher, and doesn't get broken if dropped off a floor less than floor f. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized. + +Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. diff --git a/recursive_count_th/__pycache__/count_th.cpython-37.pyc b/recursive_count_th/__pycache__/count_th.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4062b5b420095bd59146c459a8ded10cd8d5c03d GIT binary patch literal 629 zcmZuvO=}cE5bd6w=o%8(gP`7uoZN-oA0SaQd-7li3?2*vlV-ZJGwe+F(p}?{;4S9I z}}rldmT(s8~>+-Sw*L(I0CCkcYL&@h3~;#_E{lds}j z>QN@9>GhBOPp%fS3bkD|hBnHp*0AyP5;gXy(#6(!eKGw% zf;Zgx2vEAtGPz5t+?HBpugH~U=Z!5*h~?RAcEr^Y+`|EFYpyW*0ewMB6%6_qv%}(_ zS#1}XIfc^OT=l6S;kC@Tb#!G&qOBvu4UjF{O+CJ$07Gn(M#@SHBI_aV{AVj zXeqdw%07)e-U&a(t)CytSNX?)UGp<+1;2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^4hLcwfb+FD2sjKl2HK4o%%^S8kH9k;|P$8^e0AO8mUS9T6LMgEoa9LUx1!=~pc z9JW*ya0PH4fR`u~BlQXZCo}aS07qAK69DE_4S?*W-T*8D-UsMJhzy|?0H{#vTfkYs z5&)S``GAiB4*_++H-Jw8?*Q~JU!wOe;4}bMQ8=tC1d#e1a1sF86nIi!13;zv2yh#K zuNZZUA>r061130B!(q*ikM3woq>akXzIk;64BbQFj4n0Ps15 z?;7 0 - - def move_right(self): - """ - If the robot can move to the right, it moves to the right and - returns True. Otherwise, it stays in place and returns False. - This will increment the time counter by 1. - """ - self._time += 1 - if self._position < len(self._list) - 1: - self._position += 1 - return True - else: - return False - - def move_left(self): - """ - If the robot can move to the left, it moves to the left and - returns True. Otherwise, it stays in place and returns False. - This will increment the time counter by 1. - """ - self._time += 1 - if self._position > 0: - self._position -= 1 - return True - else: - return False - - def swap_item(self): - """ - The robot swaps its currently held item with the list item in front - of it. - This will increment the time counter by 1. - """ - self._time += 1 - # Swap the held item with the list item at the robot's position - self._item, self._list[self._position] = self._list[self._position], self._item - - def compare_item(self): - """ - Compare the held item with the item in front of the robot: - If the held item's value is greater, return 1. - If the held item's value is less, return -1. - If the held item's value is equal, return 0. - If either item is None, return None. - """ - if self._item is None or self._list[self._position] is None: - return None - elif self._item > self._list[self._position]: - return 1 - elif self._item < self._list[self._position]: - return -1 - else: - return 0 - - def set_light_on(self): - """ - Turn on the robot's light - """ - self._light = "ON" - def set_light_off(self): - """ - Turn off the robot's light - """ - self._light = "OFF" - def light_is_on(self): - """ - Returns True if the robot's light is on and False otherwise. - """ - return self._light == "ON" - - def sort(self): - """ - Sort the robot's list. - """ - # Fill this out - pass - - -if __name__ == "__main__": - # Test our your implementation from the command line - # with `python robot_sort.py` - - l = [15, 41, 58, 49, 26, 4, 28, 8, 61, 60, 65, 21, 78, 14, 35, 90, 54, 5, 0, 87, 82, 96, 43, 92, 62, 97, 69, 94, 99, 93, 76, 47, 2, 88, 51, 40, 95, 6, 23, 81, 30, 19, 25, 91, 18, 68, 71, 9, 66, 1, 45, 33, 3, 72, 16, 85, 27, 59, 64, 39, 32, 24, 38, 84, 44, 80, 11, 73, 42, 20, 10, 29, 22, 98, 17, 48, 52, 67, 53, 74, 77, 37, 63, 31, 7, 75, 36, 89, 70, 34, 79, 83, 13, 57, 86, 12, 56, 50, 55, 46] - - robot = SortingRobot(l) - - robot.sort() +class SortingRobot: + def __init__(self, l): + """ + SortingRobot takes a list and sorts it. + """ + self._list = l # The list the robot is tasked with sorting + self._item = None # The item the robot is holding + self._position = 0 # The list position the robot is at + self._light = "OFF" # The state of the robot's light + self._time = 0 # A time counter (stretch) + + def can_move_right(self): + """ + Returns True if the robot can move right or False if it's + at the end of the list. + """ + return self._position < len(self._list) - 1 + + def can_move_left(self): + """ + Returns True if the robot can move left or False if it's + at the start of the list. + """ + return self._position > 0 + + def move_right(self): + """ + If the robot can move to the right, it moves to the right and + returns True. Otherwise, it stays in place and returns False. + This will increment the time counter by 1. + """ + self._time += 1 + if self._position < len(self._list) - 1: + self._position += 1 + return True + else: + return False + + def move_left(self): + """ + If the robot can move to the left, it moves to the left and + returns True. Otherwise, it stays in place and returns False. + This will increment the time counter by 1. + """ + self._time += 1 + if self._position > 0: + self._position -= 1 + return True + else: + return False + + def swap_item(self): + """ + The robot swaps its currently held item with the list item in front + of it. + This will increment the time counter by 1. + """ + self._time += 1 + # Swap the held item with the list item at the robot's position + self._item, self._list[self._position] = self._list[self._position], self._item + + def compare_item(self): + """ + Compare the held item with the item in front of the robot: + If the held item's value is greater, return 1. + If the held item's value is less, return -1. + If the held item's value is equal, return 0. + If either item is None, return None. + """ + if self._item is None or self._list[self._position] is None: + return None + elif self._item > self._list[self._position]: + return 1 + elif self._item < self._list[self._position]: + return -1 + else: + return 0 + + def set_light_on(self): + """ + Turn on the robot's light + """ + self._light = "ON" + def set_light_off(self): + """ + Turn off the robot's light + """ + self._light = "OFF" + def light_is_on(self): + """ + Returns True if the robot's light is on and False otherwise. + """ + return self._light == "ON" + + def sort(self): + """ + Sort the robot's list. + """ + self.set_light_on() + + while self.light_is_on(): + self.set_light_off + self.swap_item() + + while self.can_move_right(): + self.move_right() + if self.compare_item() in [-1, 0]: + self.swap_item() + else: + self.set_light_on() + + self.swap_item() + + while self.can_move_left(): + self.move_left() + + if ((self.compare_item() in [1,0]) or (self.compare_item() == None)): + self.swap_item() + else: + self.set_light_on() + pass + + +if __name__ == "__main__": + # Test our your implementation from the command line + # with `python robot_sort.py` + + l = [15, 41, 58, 49, 26, 4, 28, 8, 61, 60, 65, 21, 78, 14, 35, 90, 54, 5, 0, 87, 82, 96, 43, 92, 62, 97, 69, 94, 99, 93, 76, 47, 2, 88, 51, 40, 95, 6, 23, 81, 30, 19, 25, 91, 18, 68, 71, 9, 66, 1, 45, 33, 3, 72, 16, 85, 27, 59, 64, 39, 32, 24, 38, 84, 44, 80, 11, 73, 42, 20, 10, 29, 22, 98, 17, 48, 52, 67, 53, 74, 77, 37, 63, 31, 7, 75, 36, 89, 70, 34, 79, 83, 13, 57, 86, 12, 56, 50, 55, 46] + + robot = SortingRobot(l) + + robot.sort() print(robot._list) \ No newline at end of file diff --git a/robot_sort/test_robot.py b/robot_sort/test_robot.py index 57fabb5e0..d1e499211 100644 --- a/robot_sort/test_robot.py +++ b/robot_sort/test_robot.py @@ -1,61 +1,61 @@ -import unittest -import random -from robot_sort import SortingRobot - -class Test(unittest.TestCase): - - def setUp(self): - self.small_list = [5, 4, 3, 2, 1] - self.medium_list = [11, 13, 7, 17, 9, 20, 1, 21, 2, 4, 22, 16, 15, 10, 23, 19, 8, 3, 5, 14, 6, 0, 24, 12, 18] - self.large_list = [20, 77, 45, 16, 15, 91, 12, 6, 24, 89, 53, 19, 85, 56, 13, 74, 48, 98, 9, 92, 25, 35, 54, 44, 50, 5, 75, 34, 2, 42, 87, 49, 76, 52, 43, 23, 7, 80, 66, 14, 46, 90, 88, 40, 97, 10, 57, 63, 1, 18, 67, 79, 96, 27, 73, 28, 32, 61, 30, 8, 17, 93, 26, 51, 60, 55, 62, 31, 47, 64, 39, 22, 99, 95, 83, 70, 38, 69, 36, 41, 37, 65, 84, 3, 29, 58, 0, 94, 4, 11, 33, 86, 21, 81, 72, 82, 59, 71, 68, 78] - self.large_varied_list = [1, -38, -95, 4, 23, -73, -65, -36, 85, 2, 58, -26, -55, 96, 55, -76, 64, 45, 69, 36, 69, 47, 29, -47, 13, 89, -57, -88, -87, 54, 60, 56, -98, -78, 59, 93, -41, -74, 73, -35, -23, -79, -35, 46, -18, -18, 37, -64, 14, -57, -2, 15, -85, 45, -73, -2, 79, -87, -100, 21, -51, 22, 26, -59, 81, 59, -24, 24, -81, 43, 61, 52, 38, -88, -95, 87, -57, -37, -65, -47, -3, 21, -77, 98, 25, 1, -36, 39, 78, 47, -35, -40, -69, -81, 11, -47, 21, 25, -53, -31] - self.random_list = [random.randint(0, 100) for i in range(0, 100)] - - def test_sorting_small_list(self): - robot = SortingRobot(self.small_list) - robot.sort() - self.assertEqual(robot._list, sorted(self.small_list)) - - def test_sorting_medium_list(self): - robot = SortingRobot(self.medium_list) - robot.sort() - self.assertEqual(robot._list, sorted(self.medium_list)) - - def test_sorting_large_list(self): - robot = SortingRobot(self.large_list) - robot.sort() - self.assertEqual(robot._list, sorted(self.large_list)) - - def test_sorting_large_varied_list(self): - robot = SortingRobot(self.large_varied_list) - robot.sort() - self.assertEqual(robot._list, sorted(self.large_varied_list)) - - def test_sorting_random_list(self): - robot = SortingRobot(self.random_list) - 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) - - -if __name__ == '__main__': - unittest.main() +import unittest +import random +from robot_sort import SortingRobot + +class Test(unittest.TestCase): + + def setUp(self): + self.small_list = [5, 4, 3, 2, 1] + self.medium_list = [11, 13, 7, 17, 9, 20, 1, 21, 2, 4, 22, 16, 15, 10, 23, 19, 8, 3, 5, 14, 6, 0, 24, 12, 18] + self.large_list = [20, 77, 45, 16, 15, 91, 12, 6, 24, 89, 53, 19, 85, 56, 13, 74, 48, 98, 9, 92, 25, 35, 54, 44, 50, 5, 75, 34, 2, 42, 87, 49, 76, 52, 43, 23, 7, 80, 66, 14, 46, 90, 88, 40, 97, 10, 57, 63, 1, 18, 67, 79, 96, 27, 73, 28, 32, 61, 30, 8, 17, 93, 26, 51, 60, 55, 62, 31, 47, 64, 39, 22, 99, 95, 83, 70, 38, 69, 36, 41, 37, 65, 84, 3, 29, 58, 0, 94, 4, 11, 33, 86, 21, 81, 72, 82, 59, 71, 68, 78] + self.large_varied_list = [1, -38, -95, 4, 23, -73, -65, -36, 85, 2, 58, -26, -55, 96, 55, -76, 64, 45, 69, 36, 69, 47, 29, -47, 13, 89, -57, -88, -87, 54, 60, 56, -98, -78, 59, 93, -41, -74, 73, -35, -23, -79, -35, 46, -18, -18, 37, -64, 14, -57, -2, 15, -85, 45, -73, -2, 79, -87, -100, 21, -51, 22, 26, -59, 81, 59, -24, 24, -81, 43, 61, 52, 38, -88, -95, 87, -57, -37, -65, -47, -3, 21, -77, 98, 25, 1, -36, 39, 78, 47, -35, -40, -69, -81, 11, -47, 21, 25, -53, -31] + self.random_list = [random.randint(0, 100) for i in range(0, 100)] + + def test_sorting_small_list(self): + robot = SortingRobot(self.small_list) + robot.sort() + self.assertEqual(robot._list, sorted(self.small_list)) + + def test_sorting_medium_list(self): + robot = SortingRobot(self.medium_list) + robot.sort() + self.assertEqual(robot._list, sorted(self.medium_list)) + + def test_sorting_large_list(self): + robot = SortingRobot(self.large_list) + robot.sort() + self.assertEqual(robot._list, sorted(self.large_list)) + + def test_sorting_large_varied_list(self): + robot = SortingRobot(self.large_varied_list) + robot.sort() + self.assertEqual(robot._list, sorted(self.large_varied_list)) + + def test_sorting_random_list(self): + robot = SortingRobot(self.random_list) + 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) + + +if __name__ == '__main__': + unittest.main()