From 8669adf451b1395891a21ace9ed3d14f9b395e4d Mon Sep 17 00:00:00 2001 From: Steven Waldron Date: Fri, 16 Oct 2020 19:53:53 -0700 Subject: [PATCH] mvp --- Short-Answer/Algorithms_Answers.md | 46 ++++++++++++++++++++++-- recursive_count_th/count_th.py | 18 +++++++--- robot_sort/robot_sort.py | 57 +++++++++++++++++++++++++----- 3 files changed, 104 insertions(+), 17 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..d44290603 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,54 @@ ## Exercise I -a) +a) O(n) -b) +b) O(n^2) -c) +c) O(n) ## Exercise II +the variable 'building' represents the building. +the variable 'eggs' represents the number of eggs you have to drop. + +def find_highest_floor(building,eggs): + + define a variable that stores the number of eggs dropped. we'll call this 'dropped' + + dropped = 0 + + define a function to drop an egg. each time this function is called it decrements the number of eggs you have and increments the number of eggs dropped. + + + def drop_egg(e,d): + e -= 1 + d += 1 + + if the egg that is dropped breaks, the drop_egg() function will return a 1, otherwise it will return a 0. + + if egg breaks: + return 1 + else: + return 0 + + on each floor, we drop one egg to see if it breaks. if it does we have our "N" + the number of eggs dropped being returned will tell us the value of "n"/which floor + otherwise if it doesnt break, go to the next floor. + + for floor in building: O(n) + drop_egg(eggs,dropped) O(1) + if drop_egg() == 1: + return dropped + else: + continue + + +time comploexity: O(n) * O(1) = O(n) + + + + diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..3d3e5d4cd 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -3,8 +3,16 @@ 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): + if len(word) < 2: + return 0 + elif word[0:2] == 'th': + return 1 + count_th(word[1:]) + else: + return count_th(word[1:]) + +word = input() +print(count_th(word)) + + diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..3323ff627 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -93,20 +93,59 @@ def light_is_on(self): return self._light == "ON" def sort(self): - """ - Sort the robot's list. - """ - # Fill this out - pass + if len(self._list) % 2 == 0: + for i in range(len(getattr(self,'_list'))+ 1): + self.pickup_item() + while self.can_move_right(): + self.compare_item() + if self.compare_item() == -1 or self.compare_item() == 0: + self.swap_item() + self.move_right() + elif self.compare_item() == 1: + self.move_right() + + getattr(self,'_list').append(getattr(self,'_item')) + setattr(self,'_position',0) + return getattr(self,'_list') + elif len(self._list) % 2 != 0: + for i in range(len(getattr(self,'_list'))): + self.pickup_item() + while self.can_move_right(): + self.compare_item() + if self.compare_item() == -1 or self.compare_item() == 0: + self.swap_item() + self.move_right() + elif self.compare_item() == 1: + self.move_right() + + getattr(self,'_list').append(getattr(self,'_item')) + setattr(self,'_position',0) + return getattr(self,'_list') + + + + + def pickup_item(self): + return setattr(self,'_item', getattr(self,'_list').pop(getattr(self,'_position'))) if __name__ == "__main__": + pass # 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] +small_list = [5,4,3,2,1] +robot = SortingRobot(l) + + # print(robot.can_move_right()) + # print(robot.can_move_left()) + # print(robot._position) + # robot.pickup_item() + # print(getattr(robot,'_item')) + # print(getattr(robot,'_list')) + # print(robot.compare_item()) +print(robot.sort()) - robot = SortingRobot(l) + # print(getattr(robot,'_list')) - robot.sort() - print(robot._list) \ No newline at end of file