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

## Exercise I

a)
a)My analysis of this snippet's runtime is Linear, or O(n). We start at O(n^3),
but reduce by O(n^2) that is incrementing inside the loop. Thus making O(n).

b)My analysis of this snippet's runtime is Logarithmic, or O(log n). As the size
of the input increases, the runtime also grows at a small rate.

b)


c)
c)My analysis of this snippet is Linear, or O(n). We only decrease bunnies at a
rate of 1.

## Exercise II


In order for this to work, we want to throw the eggs off of floors below floor 'f'.

To make this successfull, I would start with a Binary Search. This way, we start at
the middle floor and drop an egg. If the egg breaks, all floors above that can be
ignored, returning a new set of floors. Then we try again at the middle point of the
new floors, and if the egg doesn't break, the floors below that can be ignored, once
again shortening that list and repeating the process until we find where the egg
doesn't break on one floor, but it will on the floor directly above it. Due to the
continual halving of the floors, and with us using the Binary Search Tree,
the algorithm will be O(log(n)).
19 changes: 16 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):

# TBC

# if the length of the word is less than 2:
if len(word) < 2:
# return 0
return 0
# if the word contains t or h
if word[0] == 't' and word[1] == 'h':
# return count of 1 + count of 'th' sliced
return 1 + count_th(word[2:])
else:
# else return through word
return count_th(word[1:])
pass

print(count_th("health"))
print(count_th("fairy"))
print(count_th("thanks"))
print(count_th("theatherth"))
32 changes: 29 additions & 3 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,32 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
# if not able to move right
if not self.can_move_right():
return
self.swap_item()
# while True...
while True:
# while able to move right
while self.can_move_right():
# move right
self.move_right()
# if new item is greater than current item
if self.compare_item() == 1:
# swap them
self.swap_item()
# while items to compare is not None
while self.compare_item() != None:
# move left until done
self.move_left()
# ... swap items
self.swap_item()
# if no longer able to move right
if not self.can_move_right():
# we're done!
return
self.move_right()
self.swap_item()


if __name__ == "__main__":
Expand All @@ -109,4 +133,6 @@ def sort(self):
robot = SortingRobot(l)

robot.sort()
print(robot._list)
print(robot._list)
print(robot._time)
print(robot._light)
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