-
Notifications
You must be signed in to change notification settings - Fork 47
Leaves - Dominique #24
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?
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| require 'rake/testtask' | ||
|
|
||
|
|
||
| Rake::TestTask.new do |t| | ||
| t.libs = ["lib"] | ||
| t.warning = true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,119 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| require "pry" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError, 'Must provide a number greater than 1' unless n >= 0 | ||
|
|
||
| if n == 1 || n == 0 | ||
| return 1 | ||
| else | ||
| return n * factorial(n - 1) | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| 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 | ||
|
|
||
| last = s[-1] | ||
|
|
||
| rest = s[0...-1] | ||
|
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.
|
||
|
|
||
| return last + reverse(rest) | ||
| 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(string) | ||
|
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. 👍 |
||
| return _reverse(string, 0, string.length - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def _reverse(string, i, j) | ||
| if j - i <= 0 | ||
| return string | ||
| else | ||
| swap(string, i, j) | ||
| return _reverse(string, i += 1, j -=1) | ||
| end | ||
| end | ||
|
|
||
| def swap(string, i, j) | ||
|
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. 👍 |
||
| a = string[i] | ||
| string[i] = string[j] | ||
| string[j] = a | ||
| end | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(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. 👍 |
||
| raise NotImplementedError, "Method not implemented" | ||
| if n == 0 | ||
| return 0 | ||
| elsif n == 1 | ||
| return 2 | ||
| else | ||
| 2 + bunny(n - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
|
|
||
| def nested(string) | ||
| if string.length % 2 != 0 | ||
| return false | ||
| else | ||
| recursion(string, 0, string.length - 1) | ||
| end | ||
| end | ||
|
|
||
| def recursion(string, start_index, end_index) | ||
| if start_index > end_index | ||
| return true | ||
| elsif string[start_index] == "(" && string[end_index] == ")" | ||
| return recursion(string, start_index + 1, end_index - 1) | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if array == [] | ||
| return false | ||
| else | ||
| return search_helper(array, value, i) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search_helper(array, value, i) | ||
| if array[i] == value | ||
| return true | ||
| else | ||
| recursion(array, value, i += 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return is_palindrome(s, 0, s.length - 1 ) | ||
| end | ||
|
|
||
| def is_palindrome(s, start_index, end_index) | ||
| if start_index < end_index | ||
| return true | ||
| elsif s[0] != s[s.length - 1] | ||
| return false | ||
| else | ||
| is_palindrome(s, start_index + 1, end_index - 1) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
|
|
||
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.
👍