Skip to content
Open
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
52 changes: 37 additions & 15 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.
# https://www.rubyguides.com/2015/08/ruby-recursion-and-memoization/

# Time complexity: ?
# Space complexity: ?
def factorial(n)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(1)
# Space complexity: O(n)

def factorial(m)
if m == 0 || m == 1
return 1 #bc it needs to return 1
elsif m < 0
raise ArgumentError, "sorrrry"
end

# Time complexity: ?
# Space complexity: ?
return m * factorial(m-1)
end

# Time complexity: O(n)
# Space complexity: O(n)
#number of stacks is what were gaging
# how many times are we visiting each char?
# time complexity isnt in numbers, it depends on input
# how does it scale?
def reverse(s)
raise NotImplementedError, "Method not implemented"
if s.length == 1 #basecase
return s
else
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.

Missing an end to this if statement.

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

end

# Time complexity: ?
# Space complexity: ?

# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
if count == 0
return false
elsif s.even?
s.reverse + s(count -1)

Choose a reason for hiding this comment

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

You're going to need to do something to reverse instead of using the .reverse method.

else
s + s(count - 1)
end
end

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

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