Skip to content
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
33 changes: 24 additions & 9 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,62 @@
# Time complexity: ?
# Space complexity: ?
def factorial(n)

Choose a reason for hiding this comment

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

👍 Missing Time/space complexity.

raise NotImplementedError, "Method not implemented"
if n < 0
raise ArgumentError
elsif (n == 1) || (n== 0)
return 1
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
def reverse(s)
raise NotImplementedError, "Method not implemented"
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.

if s.length == 0
return s
end
a = s[0]
b = s.slice(1, s.length - 1)

Choose a reason for hiding this comment

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

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

return reverse(b) + a
end

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

Choose a reason for hiding this comment

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

Ok this this is missing

raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
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"
if n == 0
return n
end

return bunny(n - 1) + 2
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"

end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end
Loading