diff --git a/README.md b/README.md index ebbadaeff..31c631c0f 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,13 @@ You have been given a robot with very basic capabilities: * It can move left or right. * It can pick up an item * If it tries to pick up an item while already holding one, it will swap the items instead. + * It can compare the item it's holding to the item in front of it. * It can switch a light on its head on or off. Your task is to program this robot to sort lists using ONLY these abilities. + ##### Rules Inside the `robot_sort` directory you'll find the `robot_sort.py` file. Open it up and read through each of the robot's abilities. Once you've understood those, start filling out the `sort()` method following these rules: diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..c30f90a0c 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,64 @@ ## Exercise I -a) +a) Answer: 0(n) +``` -b) +- The time complexity is 0(n) because (a) grows in proportion to n as long as (a) is less than n^3. +``` -c) +b) 0(log n) +In this case, the first for loop says as long as the index is in range of n, j stays proportionate at 1. But even though the while loop is nested in the for loop, which would imply a time complexity of n^2, it isn't executing the same operation as it's parent. Under the while loop, j is growing twice as fast as it's parent. That would make it 0(log n) + ``` + sum = 0 1 + for i in range(n) -n + j = 1 1 + while j < n: + j *= 2 another way to say this is j = j * 2 (statement says as long as ) + sum += 1 -n +``` + + +c) Answer: 0(n) + +- Line 26 & 27 are both constants. However line 28 bunnyEars increase with the amount of bunnies which is n. This leads to 0(2 + n), simplified, 0(n) ## 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. + +``` + +The answer is to complete a binary search through the building. + + In researching the proper strategy to determine the value of f. First I realized that n-story building had no bearing on whether or not the eggs get broken. We are simply searching for which floor would cause the eggs to break. Now normally we would go through each floor and drop an egg to see if it breaks. That would take way too much time. However, if we did a binary search through the building, we could eliminate searching through half of the building that would likely break the egg. If we go halfway, and the egg doesn't break, we can eliminate the bottom half as we know the bottom half wouldn't cause breakage. Then we go to the midpoint of the upper half of the building and if the egg breaks there, then we know the right floor is no higher than the midpoint here. So that eliminates having to complete the search through the rest of the floors in the top level of the building. This process is to be repeated until there are no more options for (f) + + The algorithm in this case would be + Example: + Suppose n = 10 + f= [ 1,2,3,4,5,6,7,8,9,10] + +We start at floor 5 // The egg doesn't break +- This eliminates the bottom 4 floors. + +Now we have +f= [6,7,8,9,10] +Start at floor 8// The egg breaks +- Now we know the starting point of where the eggs break. If f > 8, the eggs will break + +But to make certain that is the right floor, we now only have to test 2 floors which is 6 & 7. + +The time complexity here would be 0(log n) + +``` + + + + + + diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 46f444535..36f48e5bf 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -6,27 +6,27 @@ 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 = 0 1 + 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 +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 + if bunnies == 0: 1 + return 0 1 - return 2 + bunnyEars(bunnies-1) + return 2 + bunnyEars(bunnies-1) n ``` ## Exercise II diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..3b2f56d0d 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,28 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): + count = 0 + #Look at what is being tested. Where are instances of "th" located? + # 2/3 tested words start with "th" + if word.startswith("th"): + count = 1 # Count "th" as 1 instance + #How do we account for the tested word that has "th" in the middle? + #Slice? + if len(word) > 2: + return count_th(word[1: len(word)]) + count + else: + return count - # TBC - pass + +#"word" is just a parameter. +#Can't use a loop +#What is the base case? How do we get the function to stop counting? +#Stop after word.lenth is reached +#Look at what is being tested. Where are instances of "th" located? +# 2/3 tested words start with "th" +#Count "th" as 1 instance +#How do we account for the tested word that has "th" in the middle? +#if word contains "th" return total += count +#If + diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..4ac338a57 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,10 +96,51 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + + #Describe Bubble Sort + ##Bubble sort works by comparing each element to it's neigbor + #If the second index is smaller than the first index, swap the two indices + #If you reach the end of the array, start again + #Process - Move right to the second index, pick up card, COMPARE. If second index is greater than the first index, swap. If second index is less than the first, do nothing + + #Continue process and when end of the array is reached, start again + #Use on light to move the robot. Use off light to stop the robot + + #When robot's light is on, the list is unsorted. + self.set_light_on() + + while self.light_is_on(): + self.set_light_off() + + # Pick up card + self.swap_item() + # While robot is moving right, compare items + while(self.can_move_right()): + self.move_right() + #Compare the held item in front of the robot. If the held item's value is greater, return 1 and swap. + if self.compare_item() == 1: + self.swap_item() + #List is still unsorted, turn light back on all the way to the end, move left, and then swap + self.set_light_on() + + while(self.can_move_left()): + self.move_left() + if self.compare_item() == None: + self.swap_item() + break #Exit Loop + + # Move one position over before the next loop starts again. + self.move_right() + + # Swap the last card + while(self.can_move_right()): + self.move_right() + self.swap_item() + + + if __name__ == "__main__": # Test our your implementation from the command line # with `python robot_sort.py` @@ -109,4 +150,4 @@ def sort(self): robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list)