forked from AdaGold/recursion-writing
-
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 - Lina #28
Open
lina5147
wants to merge
5
commits into
Ada-C14:master
Choose a base branch
from
lina5147:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Earth - Lina #28
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48f1be8
created factorial method
lina5147 357ab89
created reverse method and added time and space complexity
lina5147 28b9c24
created bunny method
lina5147 c82dfd8
created is_palindrom method
lina5147 db0a104
went over each recursion problem
lina5147 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,136 @@ | ||
# 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" | ||
|
||
raise ArgumentError if n < 0 | ||
|
||
if n == 0 #base case | ||
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
+16
to
18
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[1..-2]) + s[0] | ||
end | ||
end | ||
|
||
# another way to do reverse | ||
# if s.length <= 1 | ||
# return s | ||
# else | ||
# return reverse_inplace(s[1..-1]) + s[0] | ||
# end | ||
|
||
|
||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def reverse_inplace(s) | ||
raise NotImplementedError, "Method not implemented" | ||
def reverse_inplace(s, low = 0, high = s.length - 1) | ||
if low >= high | ||
return s | ||
else | ||
temp = s[low] | ||
s[low] = s[high] | ||
s[high] = temp | ||
return reverse_inplace(s, low + 1, high - 1) | ||
end | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def bunny(n) | ||
Comment on lines
+49
to
51
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 | ||
else | ||
return 2 + bunny(n-1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def nested(s) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
def nested(s, low = 0, high = s.length - 1) | ||
# if s.empty? | ||
# return true | ||
# elsif s[0] == s[-1] | ||
# return false | ||
# else | ||
# return nested(s[1..-2]) | ||
# end | ||
# | ||
if low > high | ||
return true | ||
elsif s[low] == s[high] | ||
return false | ||
else | ||
nested(s, low + 1, high - 1) | ||
end | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def search(array, value) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
def search(array, value, index = 0) | ||
# if array.empty? | ||
# return false | ||
# elsif array[0] == value | ||
# return true | ||
# else | ||
# return search(array[1..-1], value) | ||
# end | ||
|
||
if array.empty? || index > array.length - 1 | ||
return false | ||
elsif array[index] == value | ||
return true | ||
else | ||
search(array, value, index + 1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def is_palindrome(s) | ||
raise NotImplementedError, "Method not implemented" | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
def is_palindrome(s, left = 0, right = s.length - 1) | ||
# if s.length == 1 || s.empty? | ||
# return true | ||
# end | ||
# | ||
# if s[0] == s[-1] | ||
# is_palindrome(s[1..-2]) | ||
# else | ||
# return false | ||
# end | ||
if left > right | ||
return true | ||
elsif s[left] != s[right] | ||
return false | ||
else | ||
is_palindrome(s, left + 1, right - 1 ) | ||
end | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def digit_match(n, m) | ||
raise NotImplementedError, "Method not implemented" | ||
def digit_match(n, m, match_count = 0) | ||
|
||
raise ArgumentError if n < 0 || n < 0 | ||
|
||
match_count += 1 if n % 10 == m % 10 | ||
|
||
if n < 10 || m < 10 | ||
return match_count | ||
else | ||
return digit_match(n/10, m/10, match_count) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍