-
Notifications
You must be signed in to change notification settings - Fork 53
Iris Lux - Water #34
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?
Iris Lux - Water #34
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 |
|---|---|---|
| @@ -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,107 @@ | ||
| # Authoring recursive algorithms. Add comments including time and space complexity for each method. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n), where n is size of number | ||
| # Space complexity: O(n) | ||
| def factorial(n) | ||
| raise NotImplementedError, "Method not implemented" | ||
| raise ArgumentError unless n >= 0 | ||
|
|
||
| return 1 if n == 0 | ||
|
|
||
| return n * factorial(n-1) | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
|
|
||
| # Time complexity: 0(n^2) where n is length od string | ||
| # Space complexity: 0(n^2) where n is length of string | ||
|
|
||
| def reverse(s) | ||
|
Comment on lines
+15
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" | ||
| return s if s.length < 2 | ||
| return s[-1] + reverse(s[1..-2]) + s[0] | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
|
|
||
| # Time complexity: o(n) where n is length of string | ||
| # Space complexity: o(n) | ||
| def reverse_inplace(s, first = 0, last = s.length - 1) | ||
|
Comment on lines
+24
to
+26
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 s if first == last || s.empty? | ||
| s[first], s[last] = s[last], s[first] | ||
| return reverse_inplace(s, first + 1, last - 1) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
|
|
||
| # Time complexity: O(n) where n is size of number | ||
| # Space complexity: O(n) | ||
| def bunny(n) | ||
|
Comment on lines
+33
to
35
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 n if n <= 0 | ||
|
|
||
| return bunny(n-1) + 2 | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def nested(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: o(n) where n is length of string | ||
| # Space complexity: o(n) | ||
|
|
||
| def nested(s, first = 0, sub_length = s.length) | ||
|
Comment on lines
+41
to
+44
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 false if sub_length == 1 | ||
|
|
||
| return true if first == sub_length | ||
|
|
||
| return (s[first] == '(' && s[sub_length-1] == ')') ? nested(s, first + 1, sub_length - 1) : false | ||
|
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def search(array, value) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) where n is length of array | ||
| # Space complexity: O(n) | ||
|
|
||
| def search(array, value, i = 0) | ||
|
Comment on lines
+54
to
+57
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 true if array[i] == value | ||
|
|
||
| return false if i == array.length | ||
|
|
||
| return search(array, value, i + 1) | ||
| end | ||
|
|
||
| # Time complexity: O(n) where n is the length of string | ||
| # Space complexity: O(n) where n is the length of string | ||
| # | ||
| def is_palindrome(s, first = 0, sub_length = s.length) | ||
|
Comment on lines
+65
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. 👍 |
||
|
|
||
| return true if first == sub_length || first == sub_length - 1 | ||
|
|
||
| return (s[first] == s[sub_length-1]) ? is_palindrome(s, first + 1, sub_length - 1) : false | ||
|
|
||
| end | ||
|
|
||
|
|
||
| # Time complexity: O(logn) where n is the largest number | ||
| # Space complexity: O(logn) | ||
| def digit_match(n, m, count = 0) | ||
|
Comment on lines
+77
to
+79
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 n/10 == 0 && m/10 == 0 | ||
| return n == m ? count + 1 : count | ||
| end | ||
|
|
||
| if n/10 == 0 | ||
| return m % 10 == n ? count + 1 : count | ||
| elsif m/10 == 0 | ||
| return n % 10 == m ? count + 1 : count | ||
| else | ||
| return n%10 == m%10 ? digit_match(n/10, m/10, count + 1) : digit_match(n/10, m/10, count) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def is_palindrome(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| 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. Time & space complexity? |
||
| return n if n <= 1 | ||
| return fib(n - 1) + fib(n - 2) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| # def fib_non_rec(n) | ||
| # seq = Array.new(n+2) | ||
| # | ||
| # seq[0] = 0 | ||
| # seq[1] = 1 | ||
| # | ||
| # (2..n).each { |i| seq[i] = seq[i - 1] + seq[i - 2]} | ||
| # | ||
| # return seq[n] | ||
| # 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.
👍