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 000000000..4062b5b42 Binary files /dev/null and b/recursive_count_th/__pycache__/count_th.cpython-37.pyc differ diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..1aa4cad82 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -1,10 +1,15 @@ -''' -Your function should take in a single parameter (a string `word`) -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 +''' +Your function should take in a single parameter (a string `word`) +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 + if len(word) <= 1: + return 0 + elif word[0] == "t" and word[1] == "h": + return count_th(word[1:]) + 1 + else: + return count_th(word[1:]) + pass diff --git a/recursive_count_th/test_count_th.py b/recursive_count_th/test_count_th.py index 7c0905770..5af52c00e 100644 --- a/recursive_count_th/test_count_th.py +++ b/recursive_count_th/test_count_th.py @@ -1,37 +1,37 @@ -import unittest -import random -from count_th import * - -class Test(unittest.TestCase): - - def setUp(self): - self.word = "" - - def test_count_th_empty(self): - self.word = "" - count = count_th(self.word) - self.assertEqual(0, count) - - def test_count_th_single(self): - self.word = "abcthxyz" - count = count_th(self.word) - self.assertEqual(1, count) - - def test_count_th_multiple(self): - self.word = "abcthefthghith" - count = count_th(self.word) - self.assertEqual(3, count) - - def test_count_backwards(self): - self.word = "thhtthht" - count = count_th(self.word) - self.assertEqual(2, count) - - def test_count_th_mixedcase(self): - self.word = "THtHThth" - count = count_th(self.word) - self.assertEqual(1, count) - - -if __name__ == '__main__': - unittest.main() +import unittest +import random +from count_th import * + +class Test(unittest.TestCase): + + def setUp(self): + self.word = "" + + def test_count_th_empty(self): + self.word = "" + count = count_th(self.word) + self.assertEqual(0, count) + + def test_count_th_single(self): + self.word = "abcthxyz" + count = count_th(self.word) + self.assertEqual(1, count) + + def test_count_th_multiple(self): + self.word = "abcthefthghith" + count = count_th(self.word) + self.assertEqual(3, count) + + def test_count_backwards(self): + self.word = "thhtthht" + count = count_th(self.word) + self.assertEqual(2, count) + + def test_count_th_mixedcase(self): + self.word = "THtHThth" + count = count_th(self.word) + self.assertEqual(1, count) + + +if __name__ == '__main__': + unittest.main() diff --git a/robot_sort/__pycache__/robot_sort.cpython-37.pyc b/robot_sort/__pycache__/robot_sort.cpython-37.pyc new file mode 100644 index 000000000..dcca0541a Binary files /dev/null and b/robot_sort/__pycache__/robot_sort.cpython-37.pyc differ diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..f0a3d1cab 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -1,112 +1,133 @@ -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. - """ - # 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()