Skip to content
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 - Michaela #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 67 additions & 24 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,92 @@
# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method not implemented"
raise ArgumentError.new "N must be greater than or equal to 0" if n < 0
return 1 if n <= 1
return n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
This works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.

raise NotImplementedError, "Method not implemented"
return "" if s.empty?
return s[0] if s.length == 1

return reverse(s[1..-1]) + s[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s[1..-1] creates a new array and copies all the individual elements over and so is O(n) by itself.

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Brilliant, I love the helper!

raise NotImplementedError, "Method not implemented"
return helper_reverse(s)
end

# Time complexity: ?
# Space complexity: ?
def helper_reverse(s, start_index = 0, finish_index = (s.length - 1))
return s if start_index >= finish_index
temp = s[start_index]
s[start_index] = s[finish_index]
s[finish_index] = temp
return helper_reverse(s, start_index + 1, finish_index - 1)
end

# Time complexity: O(log(n))
# Space complexity: O(log(n))
def bunny(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method not implemented"
return 0 if n == 0
return n + bunny(n / 2.0 )
# This passes the tests, though technically it never hits 0 (just approaches 0)
# An alternate solution is to return 1 + bunny(n - 0.5) but that would have a slower time complexity O(n)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This works, but you have similar time/space issues with the reverse method.

raise NotImplementedError, "Method not implemented"
return true if s.empty?
return false if s.length.odd?

return false if s[0] != "(" || s[-1] != ")"
return nested(s[1...-1])
# To do this without creating new strings, make a helper method
# that accepts additional parameters for left_char and right_char and increments
# them inward each time, like I did for reverse_in_place with helper_reverse
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def search(array, value)

Choose a reason for hiding this comment

The 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 array[-1] == value
return false if array.length <= 1
return search(array[0...-1], value)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)

Choose a reason for hiding this comment

The 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 false if s[0] != s[-1]
return is_palindrome(s[1...-1])

# To do this without creating new strings, make a helper method
# that accepts additional parameters for left_char and right_char and increments
# them inward each time, like I did for reverse_in_place with helper_reverse
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(log(n))
# Space complexity: O(log(n))
def digit_match(n, m)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Method not implemented"
if (n / 10) < 1 || (m / 10) < 1
if n % 10 != m % 10
return 0
else
return 1
end
end

if n % 10 == m % 10
return 1 + digit_match((n / 10), (m / 10))
else
return digit_match((n / 10), (m / 10))
end
end
Loading