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) O(n^3) because its length is dependent on n multiplied by itself 3 times.


b)
b) 0(n log n) because it goes one loop dependent on length of n and a second that is a subset of n.


c)
c) O(n) because it is running through "bunnies" aka our "n" once until it is 0.

## Exercise II

Divide the number of floors in half and toss the egg.
If it doesn't break repeat with taking the middle of upper half of floors (middle to last and keep changing the middle)
If it does break repeat with taking the middle of the first half of floors (first to last and keep changing the middle)

This will be 0(log n) because we are dividing n into smaller sets and not checking all n
11 changes: 9 additions & 2 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
'''
def count_th(word):

# TBC
#base case, stop counting
if len(word) < 2:
return 0

pass
#if found return 1 and count from the next letters
if word[:2] == "th":
return 1+ count_th(word[2:])
#else return 0 and count from the next letters
else:
return 0 + count_th(word[1:])
31 changes: 29 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,35 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass

# if robot cannot move right, return
if not self.can_move_right:
return

#pickup initial item
self.swap_item()

while True:
#move right and keep swapping to get the lowest item in the rest of the list
while self.can_move_right():
self.move_right()
if self.compare_item() == 1:
self.swap_item()

# then while we haven't reached None, move left
while self.compare_item() != None:
self.move_left()

#drop the item into None
self.swap_item()

#if we can't move right, we're done sorting
if not self.can_move_right():
return

#move right and pick up the next item
self.move_right()
self.swap_item()


if __name__ == "__main__":
Expand Down
39 changes: 20 additions & 19 deletions robot_sort/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,26 @@ def test_sorting_random_list(self):
robot.sort()
self.assertEqual(robot._list, sorted(self.random_list))

# def test_stretch_times(self):
# robot = SortingRobot(self.small_list)
# robot.sort()
# self.assertLess(robot._time, 110)

# robot = SortingRobot(self.medium_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 1948)

# robot = SortingRobot(self.large_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 27513)

# robot = SortingRobot(self.large_varied_list)
# robot.sort()
# print(robot._time)
# self.assertLess(robot._time, 28308)
def test_stretch_times(self):
robot = SortingRobot(self.small_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 110)

robot = SortingRobot(self.medium_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 1948)

robot = SortingRobot(self.large_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 27513)

robot = SortingRobot(self.large_varied_list)
robot.sort()
print(robot._time)
self.assertLess(robot._time, 28308)


if __name__ == '__main__':
Expand Down