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

## Exercise I

a)
a) The runtime complexity is O(n). This is because with each iteration of the while loop, 'a' increases by 'n * n', so after 'n' iterations 'a' will be equal to 'n * (n * n)' which matches the while loop's conditional check of 'a < n * n * n'.


b)
b) The runtime complexity is O(n*log(n)). This is because there are nested loops: the outer 'for' loop will run 'n' times, and the inner 'while' loop will run log(n) times. The inner loop has a log(n) time complexity because the value of 'j' doubles with each iteration, so 'j' will increase quadradically and only take log(n) iterations to reach the value of 'n'.


c)
c) The runtime complexity is O(n). This is because the function is recursive and will call itself 'n' times. When it hits its base case, the function will backtrack through all 'n' function calls, adding '2' to the return value each time. Since adding '2' to another value is a constant time operation, and it is done 'n' times, the final runtime complexity is O(n).

## Exercise II

<!-- The building can be thought of as an array of floors. Because the floors of any building are inherently sorted by height, then in our array analogy we can say the array is sorted in ascending order (i.e. the lowest floor is at the bottom and the highest floor is at the top). Given a sorted array, we can use a binary search approach to find efficiently find a specific floor. This would mean first going to the middle floor and dropping an egg. - If the egg breaks, you can ignore the top half of floors and go down to the middle of the bottom half of floors (i.e. 1 quarter of the way up, or floor no. n*(1/4)) - If the egg didn't break when dropped it from the middle floor, then you would go up to the middle of top half of floors (i.e. 3 quarters of the way up, or floor no. n*(3/4))

You won't know which floor is the highest (non-breaking) floor (i.e. floor 'f') unless you test two consecutive floors resulting in one intact egg and one broken egg. Thus, you will need to continue this binary search pattern all the way to the end (or log(n) times) so that your last floor change will either be going down one floor (if your second-to-last drop resulted in a broken egg) or up one floor (if your second-to-last drop resulted in an intact egg). In either of the two previous scenarious, your last drop will guarantee that you have dropped an egg from floor 'f-1' AND from floor 'f'.

Using this binary search approach, it will always take log(n) egg drops in order to find floor 'f'. (NOTE: it will almost certainly take less than log(n) eggs if you are able to re-drop any egg that did not break. If floor 'f' happend to be the very top floor, it will still take log(n) drops, but you would end up with only one broken egg.) -->
9 changes: 6 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):
if len(word) < 2:
return 0

# TBC

pass
if word[:2] == "th":
return 1 + count_th(word[2:])
else:
return count_th(word[1:])
32 changes: 30 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,40 @@ def light_is_on(self):
"""
return self._light == "ON"

# This sort() method mimics Selection Sort
# Time Complexity: O(n^2)
# Space Complexity: O(1)
# Selection sort always performs O(n^2) comparisons, but only O(n) swaps (somewhere between 0 - n)
def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
# The robot starts at index 0, so if the robot cannot move right then there is
# at most one item in the list (i.e. the list is already sorted)
if not self.can_move_right():
return

# Pick up the first item in the list
self.swap_item()

while True:
# Traverse to the last item in the list, swapping as needed in order to hold the smallest item
while self.can_move_right():
self.move_right()
if self.compare_item() == 1:
self.swap_item()
# The robot is now at the end of the list and holding the smallest item

# Traverse left to the index where this latest iteration started
while self.compare_item() != None:
self.move_left()

# Drop the smallest item in place and move right one item and pick it up
self.swap_item()
if not self.can_move_right():
return
self.move_right()
self.swap_item()


if __name__ == "__main__":
Expand Down