-
Notifications
You must be signed in to change notification settings - Fork 47
Branches, Kristy #29
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?
Branches, Kristy #29
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,46 @@ | ||
| def binary_search(array, to_find) | ||
| high = array.length - 1 | ||
| low = 0 | ||
| while high >= low | ||
| mid = (high + low) / 2 | ||
| if array[mid] == to_find | ||
| return mid | ||
| elsif array[mid] > to_find | ||
| high = mid - 1 | ||
| else | ||
| low = mid + 1 | ||
| end | ||
| end | ||
|
|
||
| return nil | ||
| end | ||
|
|
||
| def recursive_binary_search(array, to_find, low = 0, high = array.length - 1) | ||
| mid = (high + low) / 2 | ||
| return nil if low > high | ||
| return array[mid] if array[mid] == to_find | ||
| if to_find < array[mid] | ||
| return recursive_binary_search(array, to_find, low, mid - 1) | ||
| else | ||
| return recursive_binary_search(array, to_find, mid + 1, high) | ||
| end | ||
| end | ||
|
|
||
| class List | ||
| attr_reader :head, :tail | ||
|
|
||
| def initialize(head, tail) | ||
| @head = head | ||
| @tail = tail | ||
| end | ||
|
|
||
| def self.from_array(array) | ||
|
|
||
| end | ||
|
|
||
| def search(list = self) | ||
|
|
||
| end | ||
|
|
||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,69 @@ | ||
| # 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) | ||
|
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 raise ArgumentError if n < 0 | ||
| return 1 if n == 1 || n == 0 | ||
| return n * factorial(n-1) if n > 1 | ||
| 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 || s.empty? | ||
| return s[-1] + reverse(s[1..-2]) + s[0] | ||
|
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.
|
||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) if I had gotten it to work | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(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.
consider instead: def reverse_in_place(s, left = 0, right = s.length -1)
if left < right
temp = s[left]
s[left] = s[right]
s[right] = temp
return reverse_in_place(s, left + 1, right - 1)
end
return s
end |
||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length == 1 || s.empty? || s[0] == s[-1] | ||
| while s.length > 0 | ||
| x = s[0] | ||
| y = s[-1] | ||
| s[-1] = x | ||
| s[0] = y | ||
| return reverse_inplace(s[1..-2]) | ||
| end | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # 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" | ||
| return 0 if n == 0 | ||
| return 2 + bunny(n - 1) if n >= 1 | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(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. 👍 This works, but you have similar time/space issues with the above methods. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.empty? | ||
| return nested(s[1..-2]) if s[0] == "(" && s[-1] == ")" | ||
| return false | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def search(array, value) | ||
|
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 works, but you have similar time/space issues with the above methods. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return false if array.empty? | ||
| return true if array[0] == value | ||
| return search(array[1..-1], value) | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def is_palindrome(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. 👍 This works, but you have similar time/space issues with the above methods. |
||
| raise NotImplementedError, "Method not implemented" | ||
| return true if s.empty? || s.length == 1 | ||
| return is_palindrome(s[1..-2])if s[0] == s[-1] | ||
| return false | ||
| end | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(log(n)) | ||
| # Space complexity: O(log(n)) | ||
| def digit_match(n, m) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| return 1 if n == 0 && m == 0 | ||
| return 0 if n == 0 || m == 0 | ||
| return 1 if n.digits[0] == 0 && m.digits[0] == 0 | ||
| return 0 + digit_match(n / 10, m / 10) if n % 10 != m % 10 | ||
| return 1 + digit_match(n / 10, m / 10) if n % 10 == m % 10 | ||
|
Comment on lines
+64
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. This will fail for n = 20 and m = 20. |
||
| 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.
huh? 🤔