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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def factorial(n)

Choose a reason for hiding this comment

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

This is not recursive 🤔

raise NotImplementedError, "Method not implemented"
if n == 0 || n == 1
return 1
elsif n < 0
raise ArgumentError
end

until n == 1 do
return n * factorial(n-1)
end
end

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

Choose a reason for hiding this comment

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

  1. Recursion means you have some space complexity by the system stack.
  2. You are creating a new string with each recursive call so this is actually O(n2)

raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
end

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.

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

reversed_str += s[0]
reversed_str
end

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

Choose a reason for hiding this comment

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

  1. This is not in place
  2. This is doing the same as the above method.

raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
end

return s[s.length-1] + reverse_inplace(s[0, s.length-1])
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
def bunny(n)

Choose a reason for hiding this comment

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

👍 But you're not taking the stack into account for space complexity.

raise NotImplementedError, "Method not implemented"
if n == 0
return 0
elsif n == 1
return 2
else
return 2 + bunny(n - 1)
end

return
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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 above methods due to creating new arrays.

raise NotImplementedError, "Method not implemented"
if s.length.odd?
return false
elsif
s.length == 0
return true
end
if s[0] != s[-1]
return nested(s[1..-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(1)

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 due to deleting elements.

if array == []
return false
elsif array[0] == value
return true
else
array.delete_at(0)

Choose a reason for hiding this comment

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

delete_at(0) makes all the elements shift over 1 index costing O(n) for just this one line.

return search(array, value)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)
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 due to creating new arrays.

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

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


# Time complexity: O(n + m)
# Space complexity: O(1)

def digit_match(n, m, c = 0)

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 issues with the above methods due to using chop.

You're also not considering the cost of the stack in space.

if n.class != String || m.class != String
n = n.to_s
m = m.to_s
end

checker = c

until n.length == 0 || m.length == 0
# binding.pry
if n[-1] == m[-1]
return digit_match(n.chop!, m.chop!, checker + 1)
else
return digit_match(n.chop!, m.chop!, checker)
end
end
return checker
end
Loading