forked from AdaGold/recursion-writing
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Branches- Erika #46
Open
emaust
wants to merge
7
commits into
Ada-C12:master
Choose a base branch
from
emaust: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
Branches- Erika #46
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
884be44
Question 1 completed, not passing due to arguement error
emaust 338368a
Completed reverse string (problem 2)
emaust f25854d
Added condition for empty string
emaust 7fd3658
Same method applied to reverse in place (because it reverses in place)
emaust 97f7c9c
factorial and reverse string (not in place) written
914d124
Assignment re-done, completed optionals
63a3041
Corrected space complexity of reverse_inplace
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 |
---|---|---|
@@ -1,49 +1,101 @@ | ||
# Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
# # # # # 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 unless n >= 0 | ||
return 1 if n == 1 || n == 0 | ||
|
||
n * factorial(n - 1) | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
|
||
# # # # Time complexity: O(n) | ||
# # # # Space complexity: O(n) maybe less? | ||
def reverse(s) | ||
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 | ||
reversed = reverse(s[1..-1]) | ||
reversed << s[0] | ||
|
||
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, pointer = 0) | ||
|
||
return s if pointer == s.length / 2 | ||
s[pointer], s[- 1 - pointer] = s[- 1 - pointer ], s[pointer] | ||
|
||
reverse_inplace(s, pointer + 1) | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
|
||
# # # # Time complexity: O(n) | ||
# # # # Space complexity: O(n) | ||
def bunny(n) | ||
raise NotImplementedError, "Method not implemented" | ||
|
||
return 0 if n == 0 | ||
return 2 if n == 1 | ||
bunny(n - 1) + 2 | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def nested(s) | ||
raise NotImplementedError, "Method not implemented" | ||
|
||
# # # Time complexity: O(n) | ||
# # # Space complexity: O(n) | ||
|
||
def nested(s, i = 0, j = s.length - 1) | ||
|
||
return false if s.length.odd? | ||
return false if s[i] == "(" && s[j] != ")" || (s[i] == ")" && i < j) | ||
return true if i > j || s.empty? | ||
|
||
|
||
nested(s, i + 1, j - 1) | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def search(array, value) | ||
raise NotImplementedError, "Method not implemented" | ||
|
||
# # # # Time complexity: O(n) | ||
# # # # Space complexity: O(n) | ||
def search(array, value, i = 0) | ||
|
||
return true if array[i] == value | ||
return false if i == array.length | ||
search(array, value, i + 1) | ||
|
||
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, i = 0, j = -1) | ||
|
||
return false if s[i] != s[j] | ||
return true if i > s.length - 1 | ||
|
||
is_palindrome(s, i + 1, j - 1) | ||
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def digit_match(n, m) | ||
raise NotImplementedError, "Method not implemented" | ||
end | ||
# # # Time complexity: O(n) | ||
# # # Space complexity: O(n) | ||
def digit_match(n, m, matches = 0) | ||
|
||
dig1 = n % 10 | ||
dig2 = m % 10 | ||
|
||
return matches if n == 0 || m == 0 | ||
matches += 1 if dig1 == dig2 | ||
|
||
n = n / 10 | ||
m = m / 10 | ||
digit_match(n, m, matches) | ||
|
||
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.
👍