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
Binary file added .DS_Store
Binary file not shown.
104 changes: 79 additions & 25 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,103 @@
# 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 == 1 || n == 0
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?

# Time complexity: O(n/2) >> O(n)
# Space complexity: O(n)
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.

raise NotImplementedError, "Method not implemented"
if s.length >= 2
temp_char = s[0]
s[0] = s[-1]
s[-1] = temp_char
return s[0] + reverse(s[1...-1]) + s[-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.

else
return s
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n/2) >> O(n)
# Space complexity: O(n)
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 has similar time/space complexity to the above reverse... as in the exact same.

raise NotImplementedError, "Method not implemented"
if s.length >= 2
temp_char = s[0]
s[0] = s[-1]
s[-1] = temp_char
return s[0] + reverse(s[1...-1]) + s[-1]
else
return s
end
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"
raise ArgumentError if n < 0
return 0 if n == 0
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, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
return true if s.length == 0
return false if s.length % 2 != 0

if s[0] == '(' && s[-1] == ')'
nested(s[1..-2])
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - worst case
# Space complexity: O(n) - worst case
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.

raise NotImplementedError, "Method not implemented"
return false if array[0] == nil

return true if array[0] == value

search(array[1..-1], value)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n/2) => O(n)
# Space complexity: O(n/2) => O(n)
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.

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

if s[0] == s[-1]
is_palindrome(s[1..-2])
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) n is the length of the shortest array
# 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.

👍 Unusual approach, bu tit works.

raise NotImplementedError, "Method not implemented"
end
if n.class == Integer
n = n.to_s.split("") # [1, 0, 7, 2, 5, 0, 3, 8, 9, 1]
end

if m.class == Integer
m = m.to_s.split("") # [6, 2, 5, 3 ,0, 8, 4, 1]
end

return n.length if n[-1] == nil || n[-1] == "match"

return m.length if m[-1] == nil || m[-1] == "match"

if n[-1] == m[-1]
n.insert(0, "match")
m.insert(0, "match")
end

digit_match(n[0..-2], m[0..-2])

end
Loading