-
Notifications
You must be signed in to change notification settings - Fork 53
Earth - Emily #31
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?
Earth - Emily #31
Changes from all commits
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,112 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n is the length | ||
| # Space complexity: O(n), uses up some space in the call stack until it reaches the base case | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 1 | ||
| elsif n <= 0 | ||
| raise ArgumentError | ||
| else | ||
| return n * factorial(n-1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
| def reverse(s) | ||
|
Comment on lines
+15
to
17
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" | ||
| if s.length <= 1 | ||
| return s | ||
| end | ||
|
|
||
| return s[-1] + reverse(s[1..-2]) + s[0] | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) | ||
|
Comment on lines
+25
to
27
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" | ||
| first = 0 | ||
| last = s.length - 1 | ||
| return reverse_inplace_helper(s, first, last) | ||
| end | ||
|
|
||
| def reverse_inplace_helper(s, first = 0, last = s.length - 1) | ||
| if first > last | ||
| return s | ||
| end | ||
|
|
||
| temp = s[first] | ||
| s[first] = s[last] | ||
| s[last] = temp | ||
|
|
||
| return reverse_inplace_helper(s, first + 1, last - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
|
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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| end | ||
|
|
||
| return 2 + bunny(n-1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
| def nested(s) | ||
|
Comment on lines
+55
to
57
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. 👍 However can you see a way, like with reverse_inplace to do it with O(n) time/space complexity? |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length < 1 | ||
| return true | ||
| elsif s == "(" || s == ")" | ||
| return false | ||
| elsif s[0] == "(" && s[-1] == ")" | ||
| return nested(s[1..-2]) | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
| def search(array, value) | ||
|
Comment on lines
+70
to
72
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. 👍 However can you see a way, like with reverse_inplace to do it with O(n) time/space complexity? |
||
| raise NotImplementedError, "Method not implemented" | ||
| if array.empty? | ||
| return false | ||
| elsif value == array[0] | ||
| return true | ||
| else | ||
| return search(array[1..-1], value) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n^2) | ||
| def is_palindrome(s) | ||
|
Comment on lines
+82
to
84
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. 👍 However can you see a way, like with reverse_inplace to do it with O(n) time/space complexity? |
||
| raise NotImplementedError, "Method not implemented" | ||
| if s.length < 1 | ||
| return true | ||
| elsif s[0] != s[-1] | ||
| return false | ||
| else | ||
| return is_palindrome(s[1..-2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2)? | ||
| # Space complexity: O(n^2)? | ||
| def digit_match(n, m) | ||
|
Comment on lines
+94
to
96
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. 👍 However can you see a way, like with reverse_inplace to do it with O(n) time/space complexity? |
||
| raise NotImplementedError, "Method not implemented" | ||
| n = n.to_s | ||
| m = m.to_s | ||
|
|
||
| count = 0 | ||
| return digit_match_counter(n, m, count) | ||
| end | ||
|
|
||
| def digit_match_counter(n, m, count) | ||
| if n.length == 0 || m.length == 0 | ||
| return count | ||
| elsif n[-1] == m[-1] | ||
| count += 1 | ||
| end | ||
|
|
||
| return digit_match_counter(n[0..-2], m[0..-2], count) | ||
| 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.
👍