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
25 changes: 25 additions & 0 deletions Short-Answer/Algorithms_Answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,38 @@
## Exercise I

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

The runtime here is O(n).
There is a single while loop, which will perform more linear operations the larger n is.

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

The runtime here is O(n^2).
There are two loops being used here. It starts with a for loop which contains a while loop.

c)
def bunnyEars(bunnies):
if bunnies == 0:
return 0

return 2 + bunnyEars(bunnies-1)

The runtime here is O(n)
This one is using recursion. Each call will perform an if check.

## 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."

So this problem seems to be asking how to determine the value of f as soon as possible so that I don't lose too many
eggs. In this case, I would start by dropping the egg on the median floor and see if it breaks. If it does, then I
would take the halfway point between the first floor and the median and drop the egg there to once again see if it breaks. So pretty much, I would keep checking halves until I've pinpointed the last floor that the egg can survive from if it was dropped. The runtime of this would look something like O(n*n).
10 changes: 6 additions & 4 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):

# TBC

pass
th = 0
if word.find("th") == -1:
return th
else:
word.find("th")
return 1 + count_th(word.replace("th", " ", 1))
17 changes: 15 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,21 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
if self.can_move_right() == False:
return
self.swap_item()
while True:
while self.can_move_right():
self.move_right()
if self.compare_item() == 1:
self.swap_item()
while self.compare_item() != None:
self.move_left()
self.swap_item()
if not self.can_move_right():
return
self.move_right()
self.swap_item()


if __name__ == "__main__":
Expand Down