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
92 changes: 67 additions & 25 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,91 @@
# 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), err, does the stack take up a lot of space? theres n numbers of calls
def factorial(n)
raise NotImplementedError, "Method not implemented"
raise ArgumentError, "cant be negative number" if 0 > n
if n == 0
return 1
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n) where n is the length of the string

Choose a reason for hiding this comment

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

Since s[0...s.length-1] creates a new string, this algorithm is actually O(n^2)

# Space complexity: o(n) because it creates "temp" string to return
def reverse(s)
raise NotImplementedError, "Method not implemented"
return s if s == ""
if s.length == 1
return s
else
return temp = s[-1] + reverse(s[0...-1])
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: constant, cause its in place, although, once again, it makes s.length stack calls, so does that mean its O(n) because it takes a place in the stack that many times?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return s if s == ""
return s[-1] + reverse_inplace(s[0...-1])

Choose a reason for hiding this comment

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

This is creating new strings, so it isn't in place.

To do it in place you need to swap characters one at a time without creating a new string.

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1) (or O(n) cause it has that count to create and return?)

Choose a reason for hiding this comment

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

Since you have a system stack, it's O(n)

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) (well, O(n/2))

Choose a reason for hiding this comment

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

Due to creating a new string it's actually O(n^2)

# Space complexity: O(1)
def nested(s)
raise NotImplementedError, "Method not implemented"
# maybe note to future self s[1...-1] is the same thing as s[1..-2] so count dots
if (s.length == 2 && s[0] == "(" && s[-1] == ")") || s.length == 0
return true
elsif s[0] == "(" && s[-1] == ")"
return nested(s[1...-1])
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)

Choose a reason for hiding this comment

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

O(n^2)

# Space complexity: O(1)
def search(array, value)
raise NotImplementedError, "Method not implemented"
if array.length == 0
return false
elsif array.length >= 1 && array[0] == value

Choose a reason for hiding this comment

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

You don't need the array.length >= 1

return true
else
return search(array[1...array.length], value)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)

Choose a reason for hiding this comment

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

O(n^2)

# Space complexity: O(1)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
if (s.length == 3 && s[0] == s[-1]) || (s.length == 2 && s[0] == s[-1]) || s.length == 0

Choose a reason for hiding this comment

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

This could probably be simplified to return true if s.length <= 1

return true
elsif s[0] == s[-1]
return is_palindrome(s[1...-1])
else
return false
end
end

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

Choose a reason for hiding this comment

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

Space complexity is O(n) due to the system stack space.

def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# if (n < 10 || m < 10) && n % 10 == m % 10
# blerg don't need can go straight to
if (n == 0 || m == 0)
return 0
elsif n % 10 == m % 10
return 1 + digit_match(n / 10, m / 10)
else
return 0 + digit_match(n / 10, m / 10)
end
end