Skip to content
Open

mvp #463

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

## Exercise I

a)
a) O(n)


b)
b) O(n^2)


c)
c) O(n)

## Exercise II

the variable 'building' represents the building.
the variable 'eggs' represents the number of eggs you have to drop.

def find_highest_floor(building,eggs):

define a variable that stores the number of eggs dropped. we'll call this 'dropped'

dropped = 0

define a function to drop an egg. each time this function is called it decrements the number of eggs you have and increments the number of eggs dropped.


def drop_egg(e,d):
e -= 1
d += 1

if the egg that is dropped breaks, the drop_egg() function will return a 1, otherwise it will return a 0.

if egg breaks:
return 1
else:
return 0

on each floor, we drop one egg to see if it breaks. if it does we have our "N"
the number of eggs dropped being returned will tell us the value of "n"/which floor
otherwise if it doesnt break, go to the next floor.

for floor in building: O(n)
drop_egg(eggs,dropped) O(1)
if drop_egg() == 1:
return dropped
else:
continue


time comploexity: O(n) * O(1) = O(n)





18 changes: 13 additions & 5 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
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):
if len(word) < 2:
return 0
elif word[0:2] == 'th':
return 1 + count_th(word[1:])
else:
return count_th(word[1:])

word = input()
print(count_th(word))


57 changes: 48 additions & 9 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,59 @@ def light_is_on(self):
return self._light == "ON"

def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
if len(self._list) % 2 == 0:
for i in range(len(getattr(self,'_list'))+ 1):
self.pickup_item()
while self.can_move_right():
self.compare_item()
if self.compare_item() == -1 or self.compare_item() == 0:
self.swap_item()
self.move_right()
elif self.compare_item() == 1:
self.move_right()

getattr(self,'_list').append(getattr(self,'_item'))
setattr(self,'_position',0)
return getattr(self,'_list')
elif len(self._list) % 2 != 0:
for i in range(len(getattr(self,'_list'))):
self.pickup_item()
while self.can_move_right():
self.compare_item()
if self.compare_item() == -1 or self.compare_item() == 0:
self.swap_item()
self.move_right()
elif self.compare_item() == 1:
self.move_right()

getattr(self,'_list').append(getattr(self,'_item'))
setattr(self,'_position',0)
return getattr(self,'_list')




def pickup_item(self):
return setattr(self,'_item', getattr(self,'_list').pop(getattr(self,'_position')))


if __name__ == "__main__":
pass
# Test our your implementation from the command line
# with `python robot_sort.py`

l = [15, 41, 58, 49, 26, 4, 28, 8, 61, 60, 65, 21, 78, 14, 35, 90, 54, 5, 0, 87, 82, 96, 43, 92, 62, 97, 69, 94, 99, 93, 76, 47, 2, 88, 51, 40, 95, 6, 23, 81, 30, 19, 25, 91, 18, 68, 71, 9, 66, 1, 45, 33, 3, 72, 16, 85, 27, 59, 64, 39, 32, 24, 38, 84, 44, 80, 11, 73, 42, 20, 10, 29, 22, 98, 17, 48, 52, 67, 53, 74, 77, 37, 63, 31, 7, 75, 36, 89, 70, 34, 79, 83, 13, 57, 86, 12, 56, 50, 55, 46]
l = [15, 41, 58, 49, 26, 4, 28, 8, 61, 60, 65, 21, 78, 14, 35, 90, 54, 5, 0, 87, 82, 96, 43, 92, 62, 97, 69, 94, 99, 93, 76, 47, 2, 88, 51, 40, 95, 6, 23, 81, 30, 19, 25, 91, 18, 68, 71, 9, 66, 1, 45, 33, 3, 72, 16, 85, 27, 59, 64, 39, 32, 24, 38, 84, 44, 80, 11, 73, 42, 20, 10, 29, 22, 98, 17, 48, 52, 67, 53, 74, 77, 37, 63, 31, 7, 75, 36, 89, 70, 34, 79, 83, 13, 57, 86, 12, 56, 50, 55, 46]
small_list = [5,4,3,2,1]
robot = SortingRobot(l)

# print(robot.can_move_right())
# print(robot.can_move_left())
# print(robot._position)
# robot.pickup_item()
# print(getattr(robot,'_item'))
# print(getattr(robot,'_list'))
# print(robot.compare_item())
print(robot.sort())

robot = SortingRobot(l)
# print(getattr(robot,'_list'))

robot.sort()
print(robot._list)