Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive methods with passing tests #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.Gemfile.lock
106 changes: 80 additions & 26 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,103 @@
# frozen_string_literal: true

# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) Linear - Method is called n times. Time to complete the work grows in a 1 to 1 relation to input size.
# Space complexity: O(n) Linear - Stack grows with each iteration so space is linearly proportional to the size of the input.
def factorial(n)
raise NotImplementedError, "Method not implemented"
raise ArgumentError unless n >= 0
return 1 if n.zero? || n == 1

n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def reverse(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) Linear - Method is called n times. Time to complete the work grows in a 1 to 1 relation to input size.
# Space complexity: O(n) Linear - Stack grows with each iteration so space is linearly proportional to the size of the input.
def reverse(s, i = 0, j = s.length - 1)
# raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
return '' if s.empty?

if i < j
temp = s[i]
s[i] = s[j]
s[j] = temp
return reverse(s, i + 1, j - 1)
else
return s
end
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, i = 0, j = s.length - 1)
if i < j
temp = s[i]
s[i] = s[j]
s[j] = temp
return reverse_inplace(s, i + 1, j - 1)
else
return s
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) Linear - Time to complete the work grows in a 1 to 1 relation to input size.
# Space complexity: O(n) Linear - Stack grows with each iteration so space is linearly proportional to the size of the input.
def bunny(n)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
return 0 if n <= 0
return 2 if n == 1

2 + bunny(n - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n2)
# Space complexity: O(n2)
def nested(s)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, "Method not implemented"
return true if s.empty?
return false unless s[0] == "(" && s[-1] == ")"

return nested(s[1..-2])
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) method is called n times, this depends on array length
# Space complexity: O(n) Stack grows with each iteration so space is linearly proportional to the size of the input.
def search(array, value, i = 0)
# raise NotImplementedError, "Method not implemented"
return false if array.empty?

if i < array.length
return true if array[i] == value

return search(array, value, i + 1)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s, i = 0, j = s.length - 1)
# raise NotImplementedError, "Method not implemented"
return true if s.empty?
return true if s.length == 1

if i < j
return false unless s[i] == s[j]

return is_palindrome(s, i + 1, j - 1)
else
return true
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
# raise NotImplementedError, 'Method not implemented'
if (n < 0 || m < 0) || (n == 0 && m == 0)
return 1
end
# need to think about this one but turning in what I have for now.
end
6 changes: 3 additions & 3 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
end
end

xdescribe "nested" do
describe "nested" do
it "will return true for empystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -213,7 +213,7 @@
end
end

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
Expand Down Expand Up @@ -263,7 +263,7 @@
end
end

xdescribe "is_palindrome" do
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down