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
14 changes: 11 additions & 3 deletions Short-Answer/Algorithms_Answers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

## Exercise I

a)
a) O(n)

The function will run exactly n times. ex: if n = 5, the function will run 5 times, as long as a is less than 125. On the first loop a will be set to 25, the second 50, on the 5, 125 and stop.

b)
b) O(n^2)

While not exact thanks to the doubling of j, the runtime does curve ever sharper upwards as the numbers get larger as is typical of nested loops that both run until n. It is twice as slow as O(n) by the time n = 3.

c)
c) O(n)

For an input size of n = 10, n = 15, n = 20 the algorithm does 11, 16, and 21 iteration respectively. This follows a linear line.

## Exercise II

If the goal is simply to have the least amount of dropped eggs be broken, an iterative approach where each egg is dropped from the lowest floor on up until 1 egg breaks would be the best approach, with a run time complexity of O(n).

If the goal is to have the lowest total of eggs dropped while trying to keep broken ones minimal as well, then the divide and conquer method would likely be best. A binary search, where you drop the egg from the middle floor and then if it doesn't break, discard the bottom half and drop in the middle of the top half, halving each time to the top or bottom depending on whether the egg did or did not break.

This would have a run time complexity of O(logn) and would be far faster than the iterative option with larger buildings.
17 changes: 13 additions & 4 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):

# TBC

pass
index = 0
counter = 0
def inner(word):
nonlocal index
nonlocal counter
if index > len(word) - 2:
return counter
else:
if word[0] == 't' and word[1] == 'h':
counter += 1
return inner(word[index + 1:])
inner(word)
return counter
22 changes: 20 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,26 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
while self.can_move_right() :
self.swap_item()
self.move_right()
if self.compare_item() == 1:
self.set_light_on()
self.swap_item()
self.move_left()
self.swap_item()
self.move_right()
elif self.compare_item() == -1 or self.compare_item() == 0:
self.move_left()
self.swap_item()
self.move_right()
if self.can_move_right() == False:
if self.light_is_on() == False:
break
else:
while self.can_move_left() :
self.move_left()
self.set_light_off()


if __name__ == "__main__":
Expand Down