diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..21b8eda18 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -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. diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 07456a00b..e53a5e17b 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -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)) \ No newline at end of file diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..0497059b1 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -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