Skip to content
Open

MVP #442

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

## Exercise I

a)
a) This looks like a 0_(n) runtime. The ammount of itterations cales with n


b)
b) The number of iterations climbs by a reverse factor of n. The iteration is a graph logarithmic.
0_(log n)


c)
c) This will return one iteration of "bunny ears"(n) passed to it. If "bunny ears" us n, then this is 0_(n)

## Exercise II
Eggs:

class Humpty_Dumpty(intact=true, crack_threshold):
def __init(self):
self.intact = intact
self.crack_threshold = crack_threshold

def crack(self):
self.intact = False

def falls(self, floor):
if floor >= crack_threshold:
self.crack()

Now we have Humpty_Dumpty class that reprents an egg.
If humpty dumpty falls from the floot of its crack threshold or higher he will crack

Now to devise a test cycle to test humpty dumpty at our building
we will go to the center floor of the bulding and see if humpty dumpty cracks. If he does were either at or above the crack
threshold. We should test agaun halfway between our current floor and the ground fllor to see if we can get below and narrow
it (search lower); if not then we'll search half way between ourselves and the roof to see if humpty dumpty will break.
this allows us to cut off half the unsearched floors with each iteration (0_(log n) runtime)

This is psuedo--y but it should get to the point:

def find_threshold(building, carton, min = None, max = None):
if min is None and max is None:
min = 0
max = len(building) - 1

if min > max:
return -1

if min == max:
carton[0].falls()
if carton[0] is intact:
carton.remove(0)
return "Humpty dumpty wont break from the floor"
else:
carton.remove(0)
return search_floor

else:
search_floor = (min + max) // 2
carton[0].falls()

if carton[0] is intact:
carton.remove(0)
return find_threshold(building, carton,search_floor max)
elif carton[0] is not intact:
carton.remove(0)
return find_threshold(building, carton, min, search_floor)



12 changes: 8 additions & 4 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
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, total = 0):
if len(word) < 2:
return total

# TBC

pass
if word[:2] == 'th':
total += 1
return count_th(word[2:], total)
else:
return count_th(word[1:], total)
22 changes: 21 additions & 1 deletion robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,27 @@ def sort(self):
Sort the robot's list.
"""
# Fill this out
pass
self.set_light_on()
while self.light_is_on():
self.set_light_off()
self.swap_item()

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

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

while self.can_move_left():
if self.compare_item() != None:
self.move_left()
else:
break

self.swap_item()
self.move_right()



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