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
81 changes: 52 additions & 29 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,72 @@
# 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 if n < 0
return 1 if n == 0
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

Correct, can you do better than that?

def reverse(s)

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 s if s.length <= 1
rev_s = 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.

rev_s += s[0]
return rev_s
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s, first = 0, last = s.length - 1)

Choose a reason for hiding this comment

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

👍

return s if first >= last
temp = s[first]
s[first] = s[last]
s[last] = temp

return reverse_inplace(s, first + 1, last - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(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 2 if n == 1
return 2 + bunny(n-1)
end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s, i = 0)

Choose a reason for hiding this comment

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

  1. This isn't recursive.
  2. It won't work in all cases

string = s.split("()")
return false if s.length.odd?
return true if s.empty?

return true if s.length.even? && string[i].length == string[i+1].length
return false if s.length.even? && string[i].length != string[i+1].length
end

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

👍

return false if index >= array.length
return true if array[index] == value
return search(array, value, index + 1)
end

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

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
return 1 if n == 0 && m == 0
return 0 if n == 0 || m == 0
return 1 if n.to_s[-1] == m.to_s[-1]
end
Loading