From baf9a66b596c6f56fcdf8f6ecdba9a9bf2a86b66 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 15 Aug 2020 11:25:20 -0700 Subject: [PATCH 1/8] Finished short answer and started code on the recursive count, but didn't finish. Working on robot now. --- Short-Answer/Algorithms_Answers.md | 22 +++++++++++++++++++--- recursive_count_th/count_th.py | 16 ++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index b276aca1b..a04a4772d 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,14 +2,30 @@ ## Exercise I -a) +a) -b) +b) This is O(n^2). There is a loop inside of a loop. The run time is proportional to the input size of data set. -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..e6ba50096 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,7 +4,15 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - - # TBC - - pass + start = 0 + end = len(word) - 1 + + th = "th" + + if start and end == 0: + return None + + if not word.find(th): + return None + else: + return count_th(word[1:]) \ No newline at end of file From 18bbb499bb88f2642c998b8722ddfe77a2904704 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 15 Aug 2020 11:55:43 -0700 Subject: [PATCH 2/8] working on bugs --- robot_sort/robot_sort.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index db6b1985b..d9bad9277 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(l) + for i in range(l-1): + for j in range(0, n-i-1): + if l[j] > l[j+1]: + l[j], l[j+1] = l[j+1], l[j] if __name__ == "__main__": # Test our your implementation from the command line From cb91b3468e1dedea2bd82813e2f3c02d72812cc2 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sun, 16 Aug 2020 09:08:53 -0700 Subject: [PATCH 3/8] Completed robot. Need to finish the recursive project. --- robot_sort/robot_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/robot_sort/robot_sort.py b/robot_sort/robot_sort.py index d9bad9277..0497059b1 100644 --- a/robot_sort/robot_sort.py +++ b/robot_sort/robot_sort.py @@ -96,12 +96,12 @@ def sort(self): """ Sort the robot's list. """ - n = len(l) + n = len(self._list) - for i in range(l-1): + for i in range(n-1): for j in range(0, n-i-1): - if l[j] > l[j+1]: - l[j], l[j+1] = l[j+1], l[j] + 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 From 1a87323715654d9ac0f35fc9ab3127e33c78e08f Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sun, 16 Aug 2020 11:57:08 -0700 Subject: [PATCH 4/8] Working oon count for recursive call. --- recursive_count_th/count_th.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index e6ba50096..5a0cb837c 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,15 +4,19 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - start = 0 - end = len(word) - 1 + #self.t = None + #self.h = None + count = 0 + l = len(word) - th = "th" + if l == 0: + return 0 - if start and end == 0: - return None + if word == "t": + count = count + 1 - if not word.find(th): - return None - else: - return count_th(word[1:]) \ No newline at end of file + return count + +word = "abcthefthghith" + +print(count_th(word)) \ No newline at end of file From 46500493582525f9c022aa57bc5186426f7855e6 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 10 Oct 2020 10:15:10 -0700 Subject: [PATCH 5/8] Nede to finish exercise II on short answer. --- Short-Answer/Algorithms_Answers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index a04a4772d..812f796df 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -2,10 +2,10 @@ ## 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) This is O(n^2). There is a loop inside of a loop. The run time is proportional to the input size of data set. +b) This is O(log n). Even though we are looping, j is exponentially getting closer to n, whish will break us out of the loop. 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. From c21b7d2e22a8cf52e442735d8d5fdcc217ba1a72 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 10 Oct 2020 11:08:36 -0700 Subject: [PATCH 6/8] Working on count_th. --- recursive_count_th/count_th.py | 37 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 5a0cb837c..9ab46fd50 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,19 +4,40 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - #self.t = None - #self.h = None - count = 0 - l = len(word) + # #self.t = None + # #self.h = None + # count = 0 + # l = len(word) - if l == 0: - return 0 + # if l == 0: + # return 0 - if word == "t": - count = count + 1 + # if word == "t": + # count = count + 1 + # return count + + + # see if list has more that 2 elements. If not, return None + if (len(word) < 2): + return None + + # start a count list to hold the occurences of th + count = 0 + + # if the first letter in "word" is "t", check if next element is "h" + if word[0] == 't' and word[1] == 'w': + # add to count + count += 1 + + # reset count to start at the next element in the list + count = count + count_th(word[1:]) + + # return count return count + + word = "abcthefthghith" print(count_th(word)) \ No newline at end of file From 9eb84dcb763da3cbb3a2457bea3a82e23269a5fd Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 10 Oct 2020 15:57:36 -0700 Subject: [PATCH 7/8] Still working on count_th --- recursive_count_th/count_th.py | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 9ab46fd50..6bd70a32c 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -4,39 +4,43 @@ Your function must utilize recursion. It cannot contain any loops. ''' def count_th(word): - # #self.t = None - # #self.h = None - # count = 0 - # l = len(word) - - # if l == 0: - # return 0 - - # if word == "t": - # count = count + 1 - - # return count - - # see if list has more that 2 elements. If not, return None - if (len(word) < 2): - return None + if len(word) < 2: + return 0 # start a count list to hold the occurences of th - count = 0 + + # pointer = 0 # if the first letter in "word" is "t", check if next element is "h" - if word[0] == 't' and word[1] == 'w': + else: + count = 0 + + if len(word) > 2: + if word[0] == 't' and word[1] == 'w': # add to count - count += 1 + 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:]) + # count = count + count_th(word[1:]) # return count return count + # #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" From 63c8d779fc082f9f2ac3b967582d81b7792e995c Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Mon, 12 Oct 2020 20:29:16 -0700 Subject: [PATCH 8/8] got all tests working. --- Short-Answer/Algorithms_Answers.md | 2 +- recursive_count_th/count_th.py | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Short-Answer/Algorithms_Answers.md b/Short-Answer/Algorithms_Answers.md index 812f796df..21b8eda18 100644 --- a/Short-Answer/Algorithms_Answers.md +++ b/Short-Answer/Algorithms_Answers.md @@ -5,7 +5,7 @@ a) I beleive this is constant time (O(1)). There is no looping through a list. It's just basic math operations. -b) This is O(log n). Even though we are looping, j is exponentially getting closer to n, whish will break us out of the loop. +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)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. diff --git a/recursive_count_th/count_th.py b/recursive_count_th/count_th.py index 6bd70a32c..e53a5e17b 100644 --- a/recursive_count_th/count_th.py +++ b/recursive_count_th/count_th.py @@ -5,29 +5,31 @@ ''' def count_th(word): # see if list has more that 2 elements. If not, return None - if len(word) < 2: - return 0 + 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" - else: - count = 0 - - if len(word) > 2: - if word[0] == 't' and word[1] == 'w': + elif word[0] == 't' and word[1] == 'h': # add to count - count += 1 + count += 1 # increment the list (or remove 1st item) - word.pop(0) + # word.pop(0) # reset count to start at the next element in the list # count = count + count_th(word[1:]) # return count - return count + return count + count_th(word[2:]) + + else: + return count_th(word[1:]) # #self.t = None # #self.h = None