diff --git a/README.md b/README.md index 1b02ff1d3..27569a9d0 100644 --- a/README.md +++ b/README.md @@ -112,3 +112,4 @@ Uncomment the `test_stretch_times()` test in `test_robot.py`. Can you optimize y ### Passing the Sprint Score ranges for a 1, 2, and 3 are shown in the rubric above. For a student to have _passed_ a sprint challenge, they need to earn an **average of at least 2** for all items on the rubric. + \ No newline at end of file diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..826170a2d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,93 @@ ## Exercise I -a) +a) O(n) - Linear runtime, in the worst case +
+
+

Original Code

+
+a = 0
+  while (a < n * n * n):    
+    a = a + n * n 
+
+
+
+

Justification

+
+O(1) 
+O(n) 
+O(n) 
+
+
+
-b) +b) O(n^2) - Quadratic runtime, in the worst case +
+
+

Original Code

+
+sum = 0
+  for i in range(n):
+    j = 1
+    while j < n:
+      j *= 2
+      sum += 1  
+
+
+
+

Justification

+
+O(1) 
+O(n) 
+O(1) 
+O(n^2) 
+O(2n) 
+O(1) 
+
+
+
-c) +c) O(n) - Linear runtime, in the worst case -## Exercise II +
+
+

Original Code

+
+def bunnyEars(bunnies):
+  if bunnies == 0:
+    return 0
 
+  return 2 + bunnyEars(bunnies-1)
+
+
+
+

Justification

+
+ 
+  
+O(1) 
+O(2 + n) 
+
+
+
+## Exercise II +``` +Go to the middle floor of the building by dividing `n` by two: + drop the egg from current floor: + if the egg does not break + go halfway up the building (by adding current floor to total floors (n) and dividing sum by two) + repeat step 2 + if the egg does break + go up one floor + drop the egg from current floor + if the egg breaks + return current floor minus one as `f` + if the egg does not break + go to step 3 +``` +O(log n) - Logarithmic runtime, in the worst case, because the problem is halved on each pass, making the solution relatively quick to obtain. diff --git a/Short-Answer/Algorithms_Questions.md b/Short-Answer/Algorithms_Questions.md index 46f444535..86885a648 100644 --- a/Short-Answer/Algorithms_Questions.md +++ b/Short-Answer/Algorithms_Questions.md @@ -33,4 +33,4 @@ c) def bunnyEars(bunnies): 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. +Write out your proposed algorithm in plain English or pseudocode AND give the runtime complexity of your solution. \ No newline at end of file diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..667f5c838 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,19 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - - pass + + # If the overall length of the input word is less than 2, + # the word can contain no occurences of our substring + if len(word) < 2: + return 0 + + + # If the input word starts with input, add 1 to count, + # and continue to count remaining occurences of substring + elif word[:2] == "th": + return 1 + count_th(word[1:]) + + # Count all occurences of the substring + # starting from the second index + else: + return count_th(word[1:]) \ No newline at end of file diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..3023d7b68 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,8 +96,23 @@ def sort(self): """ Sort the robot's list. """ - # Fill this out - pass + # if the robot cannot move left or right, + # there is no `list` (there are no items to compare) - make no swap, + # and return 0 + # else if the robot cannot move left, but can move right, begin comparing items and moving right + # [compare next item] if held item's value is `None` compared to item in front of robot ( == `None`), + # swap currently held item with the list item in front + # and move right + # if held item's value is equal to item in front of robot (== 0), + # make no swap, + # and move right + # else if held item's value is less than the item in front of robot (== -1), + # swap currently held item with the list item in front + # and move right + # else, the held item's value must be greater than item in front of robot (== 1) + # make no swap, + # and move right + # or else the robot can move left, but cannot move right, `list` must be already sorted if __name__ == "__main__":