-
Notifications
You must be signed in to change notification settings - Fork 53
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
Earth - Beauttie #38
base: master
Are you sure you want to change the base?
Earth - Beauttie #38
Changes from 2 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.1) | ||
minitest-reporters (1.4.2) | ||
ansi | ||
builder | ||
minitest (>= 5.0) | ||
ruby-progressbar | ||
minitest-skip (0.0.1) | ||
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,110 @@ | ||
# Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def factorial(n) | ||
raise NotImplementedError, "Method not implemented" | ||
if n < 0 | ||
raise ArgumentError | ||
elsif n == 0 | ||
return 1 | ||
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 | ||
else | ||
return s[-1] + reverse(s[0..-2]) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def reverse_inplace(s) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def reverse_inplace(s, first = 0, last = s.length - 1) | ||
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. 👍 |
||
if last <= first | ||
return s | ||
else | ||
s[first], s[last] = s[last], s[first] | ||
return reverse_inplace(s, first + 1, last - 1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def bunny(n) | ||
Comment on lines
+36
to
38
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 | ||
elsif n == 1 | ||
return 2 | ||
else | ||
return 2 + bunny(n - 1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
def nested(s) | ||
Comment on lines
+48
to
50
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. 👍 , but could you do this with O(n) space/time complexity? |
||
raise NotImplementedError, "Method not implemented" | ||
if s.empty? | ||
return true | ||
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
+60
to
62
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. 👍 , but could you do this with O(n) space/time complexity? |
||
raise NotImplementedError, "Method not implemented" | ||
if array.empty? | ||
return false | ||
elsif value == array.first | ||
return true | ||
else | ||
return search(array[1..-1], value) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def is_palindrome(s) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def is_palindrome(s, first = 0, last = s.length - 1) | ||
Comment on lines
+72
to
+74
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! |
||
if last <= first | ||
return true | ||
elsif s[first] != s[last] | ||
return false | ||
else | ||
return is_palindrome(s, first + 1, last - 1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def digit_match(n, m) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(log n) | ||
# Space complexity: O(log n) | ||
def digit_match(n, m, num_matches = 0) | ||
Comment on lines
+84
to
+86
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 ArgumentError if n < 0 || m < 0 | ||
|
||
num_matches += 1 if n % 10 == m % 10 | ||
|
||
if n < 10 || m < 10 | ||
return num_matches | ||
else | ||
return digit_match(n/10, m/10, num_matches) | ||
end | ||
end | ||
|
||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def fib(n) | ||
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. 👍 Nice getting it written, but the time complexity is O(2^n)! |
||
raise ArgumentError if n < 0 | ||
|
||
if n == 0 | ||
return 0 | ||
elsif n == 1 | ||
return 1 | ||
else | ||
return fib(n - 1) + fib(n - 2) | ||
end | ||
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.
👍