From 7dc70b1edddfcae5408f3c007d204e3d63483384 Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 11:24:18 -0400 Subject: [PATCH 1/9] Completed Exercise I --- Short-Answer/Algorithms_Answers.md | 12 +++++++++--- Short-Answer/Algorithms_Questions.md | 18 +++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..bc8d7e690 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,13 +2,19 @@ ## Exercise I -a) +a) Answer: 0(n^3) +- The time complexity is 0(n^3) because on line 10 you have n^3. On line 11 you have n^2. The actual result is O(n^3 + n^2). If you were to simplify this, the final result would be 0(n^3) because when you put n^3 and n^2 together, the worst case would always be 0(n^3). -b) +b) Answer: 0(n) -c) +- In this problem you have 4 operations and 2 constants. This gives you a time complexity of 0(4n + 2). Simplified, this would be 0(n). + + +c) Answer: 0(n) + +- Like 26 & 27 are both constants. However line 28 bunnyEars increase with the amount of bunnies which is n. This leads to 0(2 + n), smplified, 0(n) ## Exercise II diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 46f444535..f6c7ce391 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -13,20 +13,20 @@ a) a = 0 ``` -b) sum = 0 - for i in range(n): - j = 1 - while j < n: - j *= 2 - sum += 1 +b) sum = 0 1 + for i in range(n) -n + j = 1 1 + while j < n: -n + j *= 2 -n + sum += 1 -n ``` ``` 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 From 7562a420817f98c26447063e59e8e2970148cb40 Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 12:25:02 -0400 Subject: [PATCH 2/9] Completed Exercise II --- Short-Answer/Algorithms_Answers.md | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index bc8d7e690..67631ca06 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -18,4 +18,59 @@ c) Answer: 0(n) ## Exercise II +1. 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. + +``` +- Ok so we have x amount of eggs +- What is higher than floor f? Does higher mean "g", "h", "i"... + +Or + +- Could f be a variable? + +- "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." --This statement implies a conditional + +If x > f return "eggs broken" +elif x < f return "no eggs broken" + +- This statement also shows that a loop is needed because we're looping over each floor # (f) + +n = How many stories are in the building -constant because this is not in proportion to the number of eggs broken + +f = floor # -n because as the floor number increases, so does the likelihood of eggs getting broken. + +``` + +2. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized. + +``` +- Ok now this part of the instruction states, "...the value of f" Therefore f is a variable. + +- Here lies the question: How do we determine which value of (f) causes eggs to break in an n-story building? Does the value of f change with the value of n? --No because in each case, n is constant. + +``` + +3. Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. + + +So here's my interpretation of what needs to be done. + +A strategy or function needs to be developed to determine the value of f that causes eggs to break + +- In reality, the only chance of an egg not breaking is if f=0. Any higher and the eggs would definitely break. + +def is_eggs_broken(n): + n = 10 + for f in range(10): + if f = 0: + return "eggs not broken" + elif f > 0: + return "eggs broken" + + return f + + + + + From bfe73e5c775343b5b58de4923e3284b4a44fe38b Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 12:33:50 -0400 Subject: [PATCH 3/9] Updated Exercise II --- Short-Answer/Algorithms_Answers.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index 67631ca06..beeed0c30 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -52,13 +52,14 @@ f = floor # -n because as the floor number increases, so does the likelihood of 3. Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. - +``` So here's my interpretation of what needs to be done. A strategy or function needs to be developed to determine the value of f that causes eggs to break - In reality, the only chance of an egg not breaking is if f=0. Any higher and the eggs would definitely break. + def is_eggs_broken(n): n = 10 for f in range(10): @@ -69,7 +70,9 @@ def is_eggs_broken(n): return f - + + ``` + - No matter which way you look at this, any floor number bigger than zero would cause the eggs to break. The size of the building plays no part in this either. This problem has a time complexity of 0(1) because no matter what the floor number is after 0, the answer remains constant From cbe80e5b14d6095733f04a2a5ea785d20972da85 Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 12:39:33 -0400 Subject: [PATCH 4/9] Updated Exercise II --- Short-Answer/Algorithms_Answers.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index beeed0c30..8d7c6f651 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -60,15 +60,13 @@ A strategy or function needs to be developed to determine the value of f that ca - In reality, the only chance of an egg not breaking is if f=0. Any higher and the eggs would definitely break. -def is_eggs_broken(n): +def is_eggs_broken(n, f): n = 10 for f in range(10): if f = 0: - return "eggs not broken" + return false elif f > 0: - return "eggs broken" - - return f + return true ``` From 0d9040090bdb3ff65fabc0d8a03fb98dc08d95db Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 14:14:18 -0400 Subject: [PATCH 5/9] Finally got Recursion test to pass --- recursive_count_th/count_th.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..64f5be349 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? + #Identify the index of where + if len(word) > 2: + return count_th(word[1: len(word)]) + count - # TBC + else: + return count - pass + + +#"word" is just a parameter. +#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 + From 38acd7ee2d429395bb63fb0491eb89eae651b77a Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 14:46:21 -0400 Subject: [PATCH 6/9] Updated Exercise III --- recursive_count_th/count_th.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 64f5be349..6310cb9b4 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -10,9 +10,9 @@ def count_th(word): 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? - #Identify the index of where + #Slice? if len(word) > 2: - return count_th(word[1: len(word)]) + count + return count_th(word[1: len(word)]) + count else: return count @@ -20,6 +20,7 @@ def count_th(word): #"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? From 965708697d31c0c1d9160f71c65535c7c8fd0e7b Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 18:36:18 -0400 Subject: [PATCH 7/9] Fixed Exercise I and III --- Short-Answer/Algorithms_Answers.md | 76 ++++++++++++---------------- Short-Answer/Algorithms_Questions.md | 16 +++--- recursive_count_th/count_th.py | 3 +- 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index 8d7c6f651..5d7a7efbb 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,75 +2,63 @@ ## Exercise I -a) Answer: 0(n^3) +a) Answer: 0(n) -- The time complexity is 0(n^3) because on line 10 you have n^3. On line 11 you have n^2. The actual result is O(n^3 + n^2). If you were to simplify this, the final result would be 0(n^3) because when you put n^3 and n^2 together, the worst case would always be 0(n^3). +``` +- The time complexity is 0(n) because (a) grows in proportion to n as long as (a) is less than n^3. -b) Answer: 0(n) +``` -- In this problem you have 4 operations and 2 constants. This gives you a time complexity of 0(4n + 2). Simplified, this would be 0(n). +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) -- Like 26 & 27 are both constants. However line 28 bunnyEars increase with the amount of bunnies which is n. This leads to 0(2 + n), smplified, 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. -1. 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. +Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. ``` -- Ok so we have x amount of eggs -- What is higher than floor f? Does higher mean "g", "h", "i"... - -Or -- Could f be a variable? +The answer is to complete a binary search through the building. -- "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." --This statement implies a conditional + 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) -If x > f return "eggs broken" -elif x < f return "no eggs broken" + The algorithm in this case would be + Example: + Suppose n = 10 + f= [ 1,2,3,4,5,6,7,8,9,10] -- This statement also shows that a loop is needed because we're looping over each floor # (f) - -n = How many stories are in the building -constant because this is not in proportion to the number of eggs broken - -f = floor # -n because as the floor number increases, so does the likelihood of eggs getting broken. - -``` +We start at floor 5 // The egg doesn't break +- This eliminates the bottom 4 floors. -2. Devise a strategy to determine the value of f such that the number of dropped + broken eggs is minimized. +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 -``` -- Ok now this part of the instruction states, "...the value of f" Therefore f is a variable. - -- Here lies the question: How do we determine which value of (f) causes eggs to break in an n-story building? Does the value of f change with the value of n? --No because in each case, n is constant. - -``` - -3. Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. - -``` -So here's my interpretation of what needs to be done. +But to make certain that is the right floor, we now only have to test 2 floors which is 6 & 7. -A strategy or function needs to be developed to determine the value of f that causes eggs to break +The time complexity here would be 0(log n) + -- In reality, the only chance of an egg not breaking is if f=0. Any higher and the eggs would definitely break. -def is_eggs_broken(n, f): - n = 10 - for f in range(10): - if f = 0: - return false - elif f > 0: - return true - ``` - - No matter which way you look at this, any floor number bigger than zero would cause the eggs to break. The size of the building plays no part in this either. This problem has a time complexity of 0(1) because no matter what the floor number is after 0, the answer remains constant diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index f6c7ce391..36f48e5bf 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -6,19 +6,19 @@ 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 1 - for i in range(n) -n - j = 1 1 - while j < n: -n - j *= 2 -n - sum += 1 -n +b) sum = 0 + for i in range(n) + j = 1 + while j < n: + j *= 2 + sum += 1 ``` ``` diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 6310cb9b4..3b2f56d0d 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -12,8 +12,7 @@ def count_th(word): #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 - + return count_th(word[1: len(word)]) + count else: return count From 7b7377af87235f30ff8d15316760039eb76af2f7 Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Fri, 17 Apr 2020 23:37:11 -0400 Subject: [PATCH 8/9] Completed robot sort --- README.md | 2 ++ Short-Answer/Algorithms_Answers.md | 2 +- robot_sort/robot_sort.py | 43 +++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) 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 5d7a7efbb..c30f90a0c 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -54,7 +54,7 @@ But to make certain that is the right floor, we now only have to test 2 floors w The time complexity here would be 0(log n) - +``` diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..84721ad26 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,10 +96,47 @@ 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 + + self.set_light_on() + + while self.light_is_on(): + self.set_light_off() + + # Pick up card + self.swap_item() + # Move right and compare as it goes. + while(self.can_move_right()): + self.move_right() + if self.compare_item() == 1: + self.swap_item() + self.set_light_on() + + # Now, move left in the list to put the holding card in the hole. + while(self.can_move_left()): + self.move_left() + if self.compare_item() == None: + self.swap_item() + break + + # Move one position over before the loop starts again. + self.move_right() + + # Stick the last card back in place + 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 +146,4 @@ def sort(self): robot = SortingRobot(l) robot.sort() - print(robot._list) \ No newline at end of file + print(robot._list) From 9da22f41e0b32bd0c8537e38970d5d4cdd1bf4c0 Mon Sep 17 00:00:00 2001 From: Code4Blessings Date: Sun, 19 Apr 2020 23:59:56 -0400 Subject: [PATCH 9/9] Updated Robot_sort --- robot_sort/robot_sort.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index 84721ad26..4ac338a57 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -107,7 +107,8 @@ def sort(self): #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(): @@ -115,27 +116,30 @@ def sort(self): # Pick up card self.swap_item() - # Move right and compare as it goes. + # 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() - # Now, move left in the list to put the holding card in the hole. while(self.can_move_left()): self.move_left() if self.compare_item() == None: self.swap_item() - break + break #Exit Loop - # Move one position over before the loop starts again. + # Move one position over before the next loop starts again. self.move_right() - # Stick the last card back in place + # 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