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

## Exercise I

a)

a) Linear time: Although we are doing ’n’ cubed in the conditional and squaring it in the loop, the size of ’n’ has no bearing on how the function will scale. We are still doing a constant calculation overall.

b)

b) n log n: The outer loop is going through an ’n’ number of times. And inside the inner while loop ‘j’ is being doubled, so the while loop would finish twice as fast as the value of ’n’. So there is a nested for loop and overall we go with the longest time complexity, which is n log n

c)

c) Linear time: The function calls itself recursively subtracting ‘bunnies’ which is ’n’ and adding 2 to each iteration until n reaches 0. So while the function is a recursive function it only runs ’n’ number of times while adding 2 (which is a constant time complexity)

## Exercise II

This sounds like a great use of using a Binary structure.
I would find out which floor is in the middle from the top and bottom floor and drop an egg. If the egg breaks I would go to the floor half way between the bottom and my current floor and repeat for every time the egg breaks.
If the egg doesn’t break for whichever floor I’m testing, I would go half way up from my current floor to the highest floor that I tested last, and repeat. If it broke on my first test, I would go halfway up from the middle floor to the top floor. And repeat the process.

The time complexity of my testing would be a Log N, as I’m cutting the required amount of times I have to drop an egg in half each time.
16 changes: 11 additions & 5 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):

# TBC

pass

def count_th(word, index=2, count=0):
if word == "":
return 0
if index > len(word):
return count
if "th" in word[(index-2):index]:
count += 1

index += 1
return count_th(word, index, count)
20 changes: 18 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,24 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
while self.light_is_on() == False:
# turn the light ON each loop
self.set_light_on()

while self.can_move_right() == True:
self._item = self._list[self._position]

self.move_right()
if self.compare_item() == 1:
self.swap_item()
# if a swap happens during the loop, turn the light OFF
self.set_light_off()
self._list[self._position - 1] = self._item
elif self.compare_item() == -1:
self._list[self._position - 1] = self._item

# restart at the beginning if the light is turned OFF
self._position = 0


if __name__ == "__main__":
Expand Down
38 changes: 19 additions & 19 deletions robot_sort/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ 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()
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