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

## Exercise I

a)
a) This code runs while a (which is equal to a + n^2) is less than n^3. This actually results in the code running n times. For example, if n is 3, a will be 9 on the first pass, 18 on the second, and 27 on the next, terminating the loop.
This means that the code running directly corresponds to the input size, which is O(n).


b)
b) This code has 2 loops, one of which (the for loop) corresponds linearly with input size n, and the second, nested while loop will almost always run fewer times than input size n, logarithmically. This is O(n log(n)) time.


c)
c) This recursive function runs linearly. The number of times the function is called is in direct, linear correlation to the input size, which is O(n).


## Exercise II
This situation is a good one in which to use a binary search algorithm. First, I would find the midpoint between the ground floor and the top floor. I would then drop an egg from this floor. If it breaks, I would know to pick a lower floor, so would again pick a midpoint between where I had just dropped the egg and the ground level. If it didn't break, I would pick a midpoint between where I had dropped the egg from and the top floor. I would repeat this process until I was able to determine floor f. This algorithm runs in O(log(n)) time.


10 changes: 10 additions & 0 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
def count_th(word):

# TBC
if word == "":
return 0
else:
if word.find("th") == -1:
return 0
else:
newindex = word.find("th")
newword = word[newindex+2:]

return(1+count_th(newword))

pass
24 changes: 24 additions & 0 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ def sort(self):
Sort the robot's list.
"""
# Fill this out
self.set_light_on()

while self.light_is_on():
self.set_light_off()

while(self.can_move_right()):
self.swap_item()
self.move_right()

if self.compare_item() == 1:
self.swap_item()
self.set_light_on()
self.move_left()
self.swap_item()
self.move_right()

else:
self.move_left()
self.swap_item()
self.move_right()

while(self.can_move_left()):
self.move_left()

pass


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