Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!-- Initial commit -->
87 changes: 83 additions & 4 deletions Short-Answer/Algorithms_Answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,93 @@

## Exercise I

a)
a) O(n) - Linear runtime, in the worst case

<div style="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -webkit-column-rule:">
<div style="display: inline-block;">
<h3>Original Code</h3>
<pre>
<code class="language-python">a = 0
while (a < n * n * n):
a = a + n * n </code>
</pre>
</div>
<div style="display: inline-block;">
<h3>Justification</h3>
<pre>
<code class="language-python">O(1) <!-- simple variable assignment --->
O(n) <!-- while loop, determined by size of `n`
since `a` is known to be 0 --->
O(n) <!-- variable assigment again --->
</code></pre>
</div>
</div>

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

<div style="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -webkit-column-rule: 1px dotted #e0e0e0; -moz-column-rule: 1px dotted #e0e0e0; column-rule: 1px dotted #e0e0e0;">
<div style="display: inline-block;">
<h3>Original Code</h3>
<pre>
<code class="language-python">sum = 0
for i in range(n):
j = 1
while j < n:
j *= 2
sum += 1 </code>
</pre>
</div>
<div style="display: inline-block;">
<h3>Justification</h3>
<pre>
<code class="language-python">O(1) <!-- simple variable assignment --->
O(n) <!-- `for loop`, determined by size of `n`
since `a` is known to be 0 --->
O(1) <!-- simple variable assignment --->
O(n^2) <!-- nested while loop --->
O(2n) <!-- value is mutliped by 2 -->
O(1) <!-- value is added to by 1 --></code>
</pre>
</div>
</div>

c)
c) O(n) - Linear runtime, in the worst case

## Exercise II
<div style="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -webkit-column-rule:">
<div style="display: inline-block;">
<h3>Original Code</h3>
<pre>
<code class="language-python">def bunnyEars(bunnies):
if bunnies == 0:
return 0

return 2 + bunnyEars(bunnies-1)</code>
</pre>
</div>
<div style="display: inline-block;">
<h3>Justification</h3>
<pre>
<code class="language-python"> <!-- funtion declaration, no runtime -->
<!-- `if statement`, no runtime -->
O(1) <!-- constant runtime -->
O(2 + n) <!-- 2 + call of recursive function, dependent on size of variable --></code>
</pre>
</div>
</div>

## 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.
2 changes: 1 addition & 1 deletion Short-Answer/Algorithms_Questions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 16 additions & 4 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
19 changes: 17 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down