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
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rake/testtask'


Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
Expand Down
116 changes: 94 additions & 22 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,119 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
require "pry"
# 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, 'Must provide a number greater than 1' unless n >= 0

if n == 1 || n == 0
return 1
else
return n * factorial(n - 1)
end

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: 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"

return s if s.length <= 1

last = s[-1]

rest = s[0...-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.


return last + reverse(rest)
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(string)

Choose a reason for hiding this comment

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

👍

return _reverse(string, 0, string.length - 1)
end

# Time complexity: ?
# Space complexity: ?
def _reverse(string, i, j)
if j - i <= 0
return string
else
swap(string, i, j)
return _reverse(string, i += 1, j -=1)
end
end

def swap(string, i, j)

Choose a reason for hiding this comment

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

👍

a = string[i]
string[i] = string[j]
string[j] = a
end

# 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"
if n == 0
return 0
elsif n == 1
return 2
else
2 + bunny(n - 1)
end
end

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

def nested(string)
if string.length % 2 != 0
return false
else
recursion(string, 0, string.length - 1)
end
end

def recursion(string, start_index, end_index)
if start_index > end_index
return true
elsif string[start_index] == "(" && string[end_index] == ")"
return recursion(string, start_index + 1, end_index - 1)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
if array == []
return false
else
return search_helper(array, value, i)
end
end

# Time complexity: ?
# Space complexity: ?
def search_helper(array, value, i)
if array[i] == value
return true
else
recursion(array, value, i += 1)
end
end

# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
return is_palindrome(s, 0, s.length - 1 )
end

def is_palindrome(s, start_index, end_index)
if start_index < end_index
return true
elsif s[0] != s[s.length - 1]
return false
else
is_palindrome(s, start_index + 1, end_index - 1)
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
Expand Down
Loading