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

## Exercise I

a)
a) Runtime complexity is O(n) because 'n' increases in size by one

b) Runtime complexity is O(n) because here we have nested loops. The first loop runs 'n' times and second loop is not re-iterating only using it as a comparision.

b)


c)
c) Runtime complexity is O(n) because here it's calling the recursive function which will call itself 'n' times. We are returning a constant value and not looping over anything even if you are recursing one.

## Exercise II


Since we are aware of the floors and are sorted, recommend using binary search, start on the middle floor and drop an egg. if the egg breaks, then move to middle of the floors below and repeat. If the egg doesn't break, move to the middle of the floors above and repeat.
O(logN)
12 changes: 9 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
Your function should return a count of how many occurences of ***"th"*** occur within `word`. Case matters.
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):
def count_th(word, count = 0):

# TBC

pass
l = list(word)

if len(l) <= 1:
return count

if l[0:2] == ['t', 'h']:
count += 1
return count_th("".join(l[1:]), count)
30 changes: 28 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,34 @@ def sort(self):
Sort the robot's list.
"""
# Fill this out
pass
# Check if we can move right
if self.can_move_right() is False:
return self._list

# Swap the item
self.swap_item()

# Move to the right while we can
while self.can_move_right() is True:
self.move_right()

# Swap if we need to
if self.compare_item() == 1:
self.swap_item()

# Move to the left while we can
while self.can_move_left() is True:
self.move_left()

# Swap if we need to
if self.compare_item() == None:
self.swap_item()
break

# Move to the Right
self.move_right()
# Recursivly call ourself
self.sort()

if __name__ == "__main__":
# Test our your implementation from the command line
Expand All @@ -109,4 +135,4 @@ def sort(self):
robot = SortingRobot(l)

robot.sort()
print(robot._list)
print(robot._list)