diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..e64e15591 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,22 @@ ## Exercise I -a) +a) This would be O(n^3) because it is dependent on the size of n. It has nothing to do with n*n*n because that's just the operation that is being ran. -b) +b) O(n^2) it is going through 2 different loops -c) +c) O(n) it's iterating through n or in this case bunnies till it goes down to 0. ## Exercise II +1) Divide the number of floors in half and throw out the egg. + +2) If the egg doesn't break take number of floors from middle to the top split. Repeat step 1 + +3) If the egg breaks. Take the number of floors from 0 to the middle. Repeat step 1 + +In this scenario we would have O(log n) eggs thrown out the window + diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..773d10de5 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,18 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - - pass + #start the counter at 0 + count = 0 + #start searching at the beginning + letter_index = 0 + + #base case - We run out of letters + if len(word) <= 1: + return 0 + # if the current index plus one matches increase count and move on to the next letter + if word[letter_index : letter_index + 2] == "th": + count += 1 + count_th(word[letter_index + 1: ]) + # if it doesn't match , then move on to the next letter without increasing our count + if word[letter_index: letter_index + 2] != "th": + count = count_th(word[letter_index + 1: ]) + return count diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..02c7268b9 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,15 +96,43 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + self.set_light_on() + + # 1) repeat until the light turns off + while self.light_is_on: + print(robot._list) + # 2) Pick up the first card + self.swap_item() + #print(robot._list) + # 3) Make sure we aren't at the end + while self.can_move_right(): + # a)Go down the line compare to each pick up the lowest + self.move_right() + if self.compare_item() == 1: + self.swap_item() + print(robot._list) + # 4) When at the end (can't move right) - move left all the way to the empty spot + while self.compare_item() != None and self.can_move_left(): + self.move_left() + # 5) put the card down + self.swap_item() + print(robot._list) + # 6) move one to the right + if self.can_move_right(): + self.move_right() + # if can't move right anymore trigger light off + else: + self.set_light_off() + break + print("finished") 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, 9, 3] + # 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)