diff --git a/README.md b/README.md index 2c3096a9a..5b504b17a 100644 --- a/README.md +++ b/README.md @@ -81,3 +81,5 @@ Uncomment the `test_stretch_times()` test in `test_robot.py`. Can you optimize y * *1*: 0-69 * *2*: 70-89 * *3*: 90+ + +//test \ No newline at end of file diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index 51b6c6b7e..66fc7a677 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -1 +1,17 @@ -Add your answers to the Algorithms exercises here. \ No newline at end of file +Add your answers to the Algorithms exercises here. + +Exercise I + +a) O(n) -> Logarithmic. The while loop will determine how many times n will be multiplied. +b) O(n^3) -> Linear. The number of for loop nested will determine the exponential of n. +c) O(n) -> Linear. This is a constant statement. + +Exercise II +a)RUntime: O(n) +``` +building = n floorOfBuilding = f +for f in range(len(n) - 1): + if f > floorBroken: + egg = broken + +``` diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 0269d4c86..509754bee 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -7,27 +7,27 @@ pseudocode with respect to the input size n of each of the following: ``` a) a = 0 - while (a < n * n * n): + while (a < n * n * n): O(n^2) a = a + n * n ``` ``` b) sum = 0 - for i in range(n): - i += 1 - for j in range(i + 1, n): - j += 1 - for k in range(j + 1, n): - k += 1 - for l in range(k + 1, 10 + k): - l += 1 - sum += 1 + for i in range(n): O(n) + i += 1 O(1) + for j in range(i + 1, n): O(n) + j += 1 O(1) + for k in range(j + 1, n): O(n) + k += 1 O(1) + for l in range(k + 1, 10 + k): O(k / 10) + l += 1 O(1) + sum += 1 O(1) ``` ``` c) def bunnyEars(bunnies): if bunnies == 0: - return 0 + return 0 O(1) return 2 + bunnyEars(bunnies-1) ``` diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..e88841e00 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -81,11 +81,13 @@ 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. @@ -96,17 +98,46 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + while self.light_is_on() is False: # The light starts off as default. Simple while check to make sure this is correct. + print('current light status', self.light_is_on()) + self.set_light_on() # Changes the light to on + print('Robot turning on...', self.light_is_on()) + + # I would want to check if the robot can move right + while self.can_move_right(): + self.swap_item() + self.move_right() + print( + f"Robot moved right! Is the light on? {self.light_is_on()}") + if self.compare_item() == 1: + # If the value is greater, I want to swap the current item with the item infront of it. + self.swap_item() # Moves the current item forward by one. + self.move_left() # Moves back by one + self.swap_item() # Moves current item forward by one + self.move_right() # Moves right by one + self.set_light_off() # Turn off light -> program is finish iterating + if self.compare_item() == -1 or 0: # if robot number less than, do this + self.move_left() + self.swap_item() + self.move_right() + if self.compare_item() == 0: # if robot number the same, do this + self.move_left() + self.swap_item() + self.move_right() + # Returns back to the beginning of the array to continuo sorting. + if self.light_is_on() is False: + while self.can_move_left(): + self.move_left() 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] + l = [-15, -15, 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 + print(robot._list) diff --git a/robot_sort/test_robot.py b/robot_sort/test_robot.py index 57fabb5e0..a6f77828a 100644 --- a/robot_sort/test_robot.py +++ b/robot_sort/test_robot.py @@ -2,14 +2,18 @@ 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)] + 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, 11)] def test_sorting_small_list(self): robot = SortingRobot(self.small_list)