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

## Exercise I

a)
a) I beleive this is constant time (O(1)). There is no looping through a list. It's just basic math operations.


b)
b) This is O(n log n). Even though we are looping, j is exponentially getting closer to n, whish will break us out of the loop.


c)
c)The run time is O(n). Depending on how many bunnies we have, the run time will grow exactly with the size of the input.

## Exercise II

So, if dropped eggs < broken eggs, then that means that we are on a higher floor. If dropped eggs > broken eggs, than we are on a lower floor.

I would write an alogorithm along these line...

if broken_eggs == 0:
f = 0

elif dropped_eggs < broken_eggs:
f += 1

elif dropped_eggs > broken_eggs:
f -= 1

This will indicated that the higher we are, the more broken eggs we have. Conversely, if we have more dropped eggs than broken eggs, this will indicate that we are on a lower floor. If we have no broken eggs, we have to assume that we are on the ground level.

I think this will be a O(lon n) alogorithm, because the point is to find out floor we are on that minimizes the amount of broken eggs we have. Basically our f = 0 and we are trying t reduce the amount of f's.

45 changes: 42 additions & 3 deletions recursive_count_th/count_th.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,46 @@
Your function must utilize recursion. It cannot contain any loops.
'''
def count_th(word):
# see if list has more that 2 elements. If not, return None
count = 0
# splitWord = split(word) # spliting the word list

if len(word) <= 1:
return count

# start a count list to hold the occurences of th

# pointer = 0

# if the first letter in "word" is "t", check if next element is "h"
elif word[0] == 't' and word[1] == 'h':
# add to count
count += 1
# increment the list (or remove 1st item)
# word.pop(0)

# reset count to start at the next element in the list
# count = count + count_th(word[1:])

# return count
return count + count_th(word[2:])

# TBC

pass
else:
return count_th(word[1:])

# #self.t = None
# #self.h = None
# count = 0
# l = len(word)

# if l == 0:
# return 0

# if word == "t":
# count = count + 1

# return count

word = "abcthefthghith"

print(count_th(word))
7 changes: 5 additions & 2 deletions robot_sort/robot_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ def sort(self):
"""
Sort the robot's list.
"""
# Fill this out
pass
n = len(self._list)

for i in range(n-1):
for j in range(0, n-i-1):
if self._list[j] > self._list[j+1]:
self._list[j], self._list[j+1] = self._list[j+1], self._list[j]

if __name__ == "__main__":
# Test our your implementation from the command line
Expand Down