-
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
Wrote recursive methods #33
base: master
Are you sure you want to change the base?
Changes from all commits
e206025
2e69fb5
20caec2
3a956b2
d421ff6
bb18478
1cf0b7d
2c5f201
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,130 @@ | ||
# Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - it takes one recursion for every increase in n | ||
# Space complexity: O(n) - because of the system stack | ||
def factorial(n) | ||
raise NotImplementedError, "Method not implemented" | ||
raise ArgumentError if n < 0 | ||
return 1 if n <= 1 | ||
|
||
return n * factorial(n-1) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - It takes n recursions for each character | ||
# Space complexity: O(n^2) - n is number of characters | ||
# String objects with length n * (n-1) * (n-2) ... are made | ||
def reverse(s) | ||
Comment on lines
+12
to
15
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 your space/time complexity are both O(n^2) |
||
raise NotImplementedError, "Method not implemented" | ||
return s if s.length <= 1 | ||
|
||
return reverse(s[1..-1]) + s[0] | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - It will take n/2 swaps to reverse the string | ||
# Space complexity: O(n) - For the system stack | ||
def reverse_inplace(s) | ||
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. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
return s if s.length <= 1 | ||
|
||
return reverse_inplace_helper(s, 0, s.length-1) | ||
end | ||
|
||
def reverse_inplace_helper(s, start, fin) | ||
return s if start == fin | ||
|
||
s[start], s[fin] = s[fin], s[start] | ||
reverse_inplace_helper(s, start+1, fin - 1) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - one recursion for each bunny | ||
# Space complexity: O(n) - system stack | ||
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" | ||
return bunny_tail(n, 0) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def bunny_tail(n, ears) | ||
return ears if n == 0 | ||
|
||
return bunny_tail(n-1, ears+2) | ||
end | ||
|
||
# Time complexity: O(n) - has to look through each character | ||
# Space complexity: O(n) - system stack | ||
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. This is working if the number of "(" and ")" match, but what about ")("? |
||
raise NotImplementedError, "Method not implemented" | ||
return nested_helper(s, 0, s.length, 0) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def nested_helper(s, char, fin, parens) | ||
return true if char == fin | ||
|
||
if s[char] == '(' | ||
nested_helper(s, char+1, fin, parens + 1) | ||
else | ||
return false if parens == 0 | ||
|
||
nested_helper(s, char+1, fin, parens - 1) | ||
end | ||
end | ||
|
||
# Time complexity: O(n) - Linear search | ||
# Space complexity: O(n) - system stack | ||
def search(array, value) | ||
Comment on lines
+66
to
68
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 search_helper(array, value, 0, array.length) | ||
end | ||
|
||
def search_helper(array, value, idx, len) | ||
if idx == len | ||
return false | ||
elsif array[idx] == value | ||
return true | ||
else | ||
return search_helper(array, value, idx+1, len) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) - n/2 recursions | ||
# Space complexity: O(n) - system stack | ||
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. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
return true if s.empty? | ||
|
||
return is_palindrome_helper(s, 0, s.length - 1) | ||
end | ||
|
||
def is_palindrome_helper(s, start, fin) | ||
if start == fin | ||
return true | ||
elsif s[start] != s[fin] | ||
return false | ||
else | ||
is_palindrome_helper(s, start+1, fin-1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(logn) - Where n is the larger of n or m. | ||
# Reduces n by a factor of 10 every recursion | ||
# Space complexity: O(logn) - System stack | ||
def digit_match(n, m) | ||
Comment on lines
+100
to
103
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" | ||
end | ||
# Handle edge case where one or both numbers are zero | ||
if n == 0 || m == 0 | ||
if (n % 10) == (m % 10) | ||
return 1 | ||
else | ||
return 0 | ||
end | ||
end | ||
return digit_match_helper(n, m, 0) | ||
end | ||
|
||
def digit_match_helper(n, m, digits) | ||
return digits if n == 0 || m == 0 | ||
if (n % 10) == (m % 10) | ||
return digit_match_helper(n/10, m/10, digits+1) | ||
else | ||
return digit_match_helper(n/10, m/10, digits) | ||
end | ||
end | ||
|
||
# Time complexity: O(n) - One recursion for every n | ||
# Space compexlity: O(n) - System stack | ||
def fib(n, f0 = 0, f1 = 1, index = 1) | ||
Comment on lines
+124
to
+126
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 work on a tail recursive version of Fibonacci. |
||
return 0 if n == 0 | ||
return f1 if index == n | ||
fib(n, f1, f0+f1, index+1) | ||
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.
👍