-
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
Yasmin- Leaves #34
base: master
Are you sure you want to change the base?
Yasmin- Leaves #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 |
---|---|---|
@@ -1,41 +1,76 @@ | ||
# 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 NotImplementedError, "Method not implemented" | ||
if n < 0 | ||
raise ArgumentError | ||
elsif n <= 1 | ||
return 1 | ||
else | ||
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. Correct on the time/space complexities. Can you think of a way to do better? |
||
raise NotImplementedError, "Method not implemented" | ||
# raise NotImplementedError, "Method not implemented" | ||
if s.length <= 1 | ||
return s | ||
else | ||
return s[-1] + reverse(s[0..-2]) | ||
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 | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: | ||
# Space complexity: | ||
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. We'll go over this in class. |
||
raise NotImplementedError, "Method not implemented" | ||
|
||
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" | ||
# raise NotImplementedError, "Method not implemented" | ||
if n == 0 | ||
return 0 | ||
else | ||
return 2 + bunny(n-1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
|
||
# Time complexity: o(n) | ||
# Space complexity: o(n^2) | ||
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. 👍 Time complexity is also O(n^2) because of |
||
raise NotImplementedError, "Method not implemented" | ||
# raise NotImplementedError, "Method not implemented" | ||
if s == "" | ||
return true | ||
elsif s[0] == "(" && s[-1] == ")" && nested(s[1...-1]) | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def search(array, value) | ||
Comment on lines
60
to
62
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. 👍 Big-O is O(n^2) due to the repeated creation of new arrays. |
||
raise NotImplementedError, "Method not implemented" | ||
# raise NotImplementedError, "Method not implemented" | ||
if array.length == 0 | ||
return false | ||
elsif value == array[0] | ||
return true | ||
else | ||
return search(array[1..-1], value) | ||
end | ||
end | ||
|
||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def is_palindrome(s) | ||
|
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.
👍