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

India - Water #36

Open
wants to merge 4 commits 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
66 changes: 50 additions & 16 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
# 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)
Comment on lines +3 to 5

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 1
elsif n < 0
raise ArgumentError
else
return n * factorial(n-1)
end
end

puts factorial(5)

# Time complexity: ?
# Space complexity: ?
def reverse(s)
Comment on lines 17 to 19

Choose a reason for hiding this comment

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

👍 Time & space complexity?

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

return s[-1] + reverse(s[1..-2]) + s[0]
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def reverse_inplace(s)
Comment on lines +27 to 29

Choose a reason for hiding this comment

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

⚠ This method is not reversing the string in place, instead it's creating a new string with each recursive call.

Could you see a way to do it with the following method signature?

Suggested change
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def reverse_inplace(s)
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def reverse_inplace(s, low = 0, high = s.length - 1)

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

new_s = reverse_inplace(s[1..-2])
s[1..-2] = new_s
s[0] , s[-1] = s[-1], s[0]
return s
end

# Time complexity: ?
# Space complexity: ?
puts reverse_inplace("hello world")

# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
Comment on lines +42 to 44

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 + bunny(n - 1)
end

# Time complexity: ?
Expand All @@ -30,16 +52,28 @@ def nested(s)
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def search(array, value)
Comment on lines +55 to 57

Choose a reason for hiding this comment

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

👍 However can you see how to do this method with O(n) space/time complexity?

raise NotImplementedError, "Method not implemented"
if array[0] == value
return true
elsif array.length < 1
return false
else
return search(array[1..-1], value)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def is_palindrome(s)
Comment on lines +67 to 69

Choose a reason for hiding this comment

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

👍 Similar note to the above on time/space complexity.

raise NotImplementedError, "Method not implemented"
if s.length <= 1
return true
elsif s[0] != s[-1]
return false
else
return is_palindrome(s[1..-2])
end
end

Choose a reason for hiding this comment

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

Just noting the incomplete methods.

# Time complexity: ?
Expand Down
18 changes: 9 additions & 9 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
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 @@ -242,10 +242,10 @@
# Arrange
item = "x"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal false
end
Expand All @@ -254,16 +254,16 @@
# Arrange
item = "b"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal true
end
end
end

xdescribe "is_palindrome" do
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -334,7 +334,7 @@
# Assert
expect(answer).must_equal 3
end

it "returns 1 for (0, 0)" do
# Arrange
num1 = 0
Expand All @@ -346,7 +346,7 @@
# Assert
expect(answer).must_equal 1
end

it "returns 1 for (10, 20)" do
# Arrange
num1 = 10
Expand Down