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
79 changes: 52 additions & 27 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
def factorial(n)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) -- I think all of my methods might be this space/time complexity but not positive!
# Space complexity: O(n)
def factorial(num)
raise ArgumentError if num < 0
return num * factorial(num - 1)
end
Comment on lines +5 to 8

Choose a reason for hiding this comment

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

You're missing a base case here.

def factorial(num)
  raise ArgumentError, "Cannot accept a number < 0"
  return 1 if num <= 1

  return num * factorial(num - 1)
end


# Time complexity: ?
# Space complexity: ?
# Time complexity: 0(n)
# Space complexity: O(n)
def reverse(s)
raise NotImplementedError, "Method not implemented"
if s.length > 1 then
((s[s.length - 1, 1] = s[(s.length - (s.length + 1)), 1]) + reverse_recursive(s.length - 1))

Choose a reason for hiding this comment

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

You don't have a reverse_recursive method 🤔

else
s
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
reversed_str = reverse(s[1..-1])

Choose a reason for hiding this comment

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

You're calling the reverse method in the method above here.

It's also in no way in place.

reversed_str += s[0]
reversed_str
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
raise NotImplementedError, "Method not implemented"
return 0 if n < 1
return 2 if n == 1
return 2 + bunny(n-1)
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, although since you are creating a new array with s[1..s.length-2] your time & space complexities are off.

They should be O(n2)

raise NotImplementedError, "Method not implemented"
return true if s.length == 0
if s[0] == "(" && s[-1] == ")"
return nested(s[1..s.length-2])
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def search(array, value, index = 0)

Choose a reason for hiding this comment

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

You're never calling this method with another index number, so you don't need this argument.

Alternatively you don't need to create a new array and instead call the method with an increasing index.

return false if array.empty?
return true if array[index] == value
return search(array[1..-1], value)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
return true if s.empty? || s.length == 1
return false if s[0] != s[s.length -1]
return is_palindrome(s, 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(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"
n = n.to_s
m = m.to_s
return 0 if n[-1] == nil || m[-1] == nil
if n[-1] == m[-1]
return 1 + digit_match(n[0..-2], m[0..-2])
else
return digit_match(n[0..-2], m[0..-2])
end
end
Loading