-
Notifications
You must be signed in to change notification settings - Fork 53
Water - Kayla Johnson #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3256d71
0918d0a
0507089
5880c4a
788ce3b
68883d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| ansi (1.5.0) | ||
| builder (3.2.4) | ||
| coderay (1.1.3) | ||
| method_source (1.0.0) | ||
| minitest (5.14.2) | ||
| minitest-reporters (1.4.2) | ||
| ansi | ||
| builder | ||
| minitest (>= 5.0) | ||
| ruby-progressbar | ||
| minitest-skip (0.0.3) | ||
| minitest (~> 5.0) | ||
| minitest-spec (0.0.2.1) | ||
| minitest (>= 3.0) | ||
| pry (0.13.1) | ||
| coderay (~> 1.1) | ||
| method_source (~> 1.0) | ||
| rake (13.0.1) | ||
| ruby-progressbar (1.10.1) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| minitest | ||
| minitest-reporters | ||
| minitest-skip | ||
| minitest-spec | ||
| pry | ||
| rake | ||
|
|
||
| BUNDLED WITH | ||
| 2.1.4 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,85 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) ['n' being the number of times factorial computation has to be implemented until n reaches 0] | ||
| # Space complexity: 0(n) ['n' being the number of function calls on the stack] | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError if n < 0 | ||
| return 1 if n == 0 | ||
| return n * factorial(n - 1) | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
| def reverse(s, i = 1) | ||
|
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return s if s == "" || s.length < 2 | ||
| reversed = reverse(s[1..-1]) | ||
| reversed += s[0] | ||
| return reversed | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) -still has to iterate over every element in string | ||
| # Space complexity: O(n) -stack can get very large if string is big | ||
| def reverse_inplace_helper(s, start, finish) | ||
|
Comment on lines
+21
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Really nice! |
||
| if start < finish | ||
| s[start], s[finish] = s[finish], s[start] | ||
| return reverse_inplace_helper(s, start + 1, finish - 1) | ||
| else | ||
| return s | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
|
|
||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return reverse_inplace_helper(s, 0, s.length - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
|
Comment on lines
+38
to
40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| return 0 if n <= 0 | ||
| return 2 + bunny(n - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s) | ||
|
Comment on lines
+45
to
47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the slice you're doing, this is O(n^2) for both time/space. Otherwise 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| return true if s == "" || s.length == 0 | ||
| return false if s.length.odd? | ||
| while s[0] == '(' && s[-1] == ')' | ||
| return nested(s[1..-2]) | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| # Time complexity: O(n) best case O(1) if item is in index 1? | ||
| # Space complexity: O(n) best case O(1) if item is in index 1? | ||
| def search(array, value, i = 0) | ||
|
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Well done! |
||
| return true if array[i] == value | ||
| return false if i >= array.length | ||
| return search(array, value, i + 1) | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - traversing the whole string | ||
| # Space complexity: O(n) -traversing the whole string | ||
| def is_palindrome(s) | ||
|
Comment on lines
+65
to
67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the slice this is O(n^2), but otherwise 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s == "" || s.length < 2 | ||
| return false if s[0] != s[-1] | ||
| return is_palindrome(s[1..-2]) | ||
| end | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def digit_helper(n, m, match = 0, place = 0) | ||
|
Comment on lines
+73
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Outstanding! |
||
| return 1 if n == 0 && m == 0 && place == 0 | ||
| return match if n == 0 || m == 0 | ||
| match += 1 if n % 10 == m % 10 | ||
| return digit_helper(n/10, m/10, match, place + 1) | ||
| return match | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| return digit_helper(n, m, 0, 0) | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍