diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..9c56c3ee8 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -4,12 +4,32 @@ a) +The operation is re-iterating over a number items in n, and increasing a counts. So, the time complexity for this is O(n). b) +This has a nested while loop inside of a for loop. But, the while loop is not incrementing by 1, rather it's being multiplied by 2. Which makes it in my opinion log n --> O(n) * log n --> O(n log n) c) +The arithmetic is contant in the return, which is addin 2 for each time the function re-reruns. How many times the funcion will run recursively depends on bunnies input provided. For each addition incrementation in the input, the function will run one more time. Which makes it O(n) ## Exercise II +n --> number of story in building +f --> eggs broken if thrown from at least floor f +[f-1] --> eggs doesn't break +Challenge --> find floor #f, from which if egg is dropped then it will break + --> if dropped from zero, the egg will not break + --> if dropped from n floor the egg will break + +Solution: + I will use binary search to find the mid point by dividing (0+n)/2. and test if it breaks + if breaks, then do binary search on the left side to find lower floor + and then binary search on the right or left side + + I will use while loop, and if conditional statments to find the f + +Time Complexity: + O(log n) --> since the iteration will not be on every element of n + diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 46f444535..f4d0f9c37 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -11,12 +11,11 @@ a) a = 0 a = a + n * n ``` - ``` b) sum = 0 - for i in range(n): + for i in range(n): # O(n) j = 1 - while j < n: + while j < n: # log n j *= 2 sum += 1 ``` diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..3ea761e34 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +3,41 @@ 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 + # print(len(word)) + # print(word[-2:]) + # word = word.replace(word[-2:-1], '') + # print(len(word)) + # print(word[-2:-1]) + print(word) + ''' + start from end of the word to find last two chars if they are th + if they are th, count and remove + if they are not th + check if 2nd from last is h, + if so, remove the last char + remove the last 2 chars + ''' + if len(word) <= 1: + return 0 + elif word[-2:] == 'th': + # word = word.replace(word[-2:], '') + word = word[:-2] + print(f"word: {word} ") + return 1 + count_th(word) + elif word[-2:-1] == 'h': + # word = word.replace(word[-1:], '') + word = word[:-1] + return count_th(word) + else: + # word = word.replace(word[-2:], '') + word = word[:-2] + return count_th(word) + + +print(count_th('abcthxyz')) # 1 +print(count_th('abcthefthghith')) # 3 diff --git a/recursive_count_th/test_count_th.py b/recursive_count_th/test_count_th.py index 7c0905770..6be93f1db 100644 --- a/recursive_count_th/test_count_th.py +++ b/recursive_count_th/test_count_th.py @@ -2,6 +2,7 @@ import random from count_th import * + class Test(unittest.TestCase): def setUp(self): @@ -16,7 +17,7 @@ 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) @@ -32,6 +33,6 @@ def test_count_th_mixedcase(self): count = count_th(self.word) self.assertEqual(1, count) - + if __name__ == '__main__': unittest.main() diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..2b87dca79 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. @@ -97,16 +99,40 @@ def sort(self): Sort the robot's list. """ # Fill this out - pass + + ''' + 1. check if the item can move right + 2. compare and if return is 1, then move right + 3. compare if return is -1, move left + ''' + self.set_light_on() + # print(self.light_is_on()) + while self.light_is_on(): + self.set_light_off() # stop continuous loop + self.move_right() + self.swap_item() + if self.compare_item() != None: + self.swap_item() + while self.can_move_right(): + self.move_right() + 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, 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 = [5, 4, 3, 2, 1] robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list) + + +print('---------solution----------') +arr = [5, 4, 3, 2, 1] +print(f"sorted: {sorted(arr)} ") diff --git a/robot_sort/test_robot.py b/robot_sort/test_robot.py index 57fabb5e0..262080039 100644 --- a/robot_sort/test_robot.py +++ b/robot_sort/test_robot.py @@ -2,13 +2,17 @@ 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.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):