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

Branches- Amal #31

Open
wants to merge 2 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
93 changes: 68 additions & 25 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,92 @@
# 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"
return 1 if n == 0
raise ArgumentError if n < 0

return n * factorial(n - 1)
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 reverse_recursive(s, reversed_string = "", index = -1)

Choose a reason for hiding this comment

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

Suggested change
return reverse_recursive(s, reversed_string = "", index = -1)
return reverse_recursive(s, "", index = -1)

end

def reverse_recursive(s, reversed_string, index)
return reversed_string if index < -s.length
reversed_string += s[index]

Choose a reason for hiding this comment

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

reversed_string += s[index] creates a new array and copies all the individual elements over and so is O(n) by itself.

return reverse_recursive(s, reversed_string, index - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n)
# Space complexity: o(n)
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return reverse_inplace_recursive(s, last = s.length - 1, index = 0)
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace_recursive(s, last, index)

Choose a reason for hiding this comment

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

👍

return s if index >= last
current_letter = s[index]
s[index] = s[last]
s[last] = current_letter
reverse_inplace_recursive(s, last - 1, index + 1)
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"
return 0 if n <= 0
return 2 if n == 1
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.empty?
return false if s[0] != "(" || s[-1] != ")"
return nested(s[1...-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.

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n)
# Space complexity: o(n)
def search(array, value)

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 search_recursive(array, value, 0)
end

def search_recursive(array, value, index)
return false if index > array.length
return true if array[index] == value

return search_recursive(array, value, index + 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n)
# Space complexity: 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.empty?
return false if s[0] != s[-1]

return is_palindrome(s[1...-1])
end

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

👍 Nice!

raise NotImplementedError, "Method not implemented"
end
if n.digits.length > m.digits.length
return digit_match_recursive(n.digits, m.digits, num_of_pairs = 0, index = 0)
else
return digit_match_recursive(m.digits, n.digits, num_of_pairs = 0, index = 0)
end
end

def digit_match_recursive(large_num, small_num, num_of_pairs, index)
return num_of_pairs if index == small_num.length
if large_num[index] == small_num[index]
num_of_pairs += 1
end
return digit_match_recursive(large_num, small_num, num_of_pairs, index + 1)
end
92 changes: 45 additions & 47 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"
require_relative '../lib/recursive-methods'
require_relative "../lib/recursive-methods"

describe "factorial" do
it "will find the factorial of 0" do
Expand All @@ -23,8 +23,7 @@
answer = factorial(num)

# Assert
expect(answer).must_equal 5*4*3*2*1

expect(answer).must_equal 5 * 4 * 3 * 2 * 1
end

it "will raise an ArgumentError if given a number not >= 0" do
Expand All @@ -38,7 +37,7 @@
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -83,8 +82,7 @@
end
end


xdescribe "reverse_in_place" do
describe "reverse_in_place" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -129,7 +127,7 @@
end
end

xdescribe "bunny" do
describe "bunny" do
it "returns 0 for 0 bunnies" do
# Arrange
count = 0
Expand Down Expand Up @@ -164,7 +162,7 @@
end
end

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

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
Expand All @@ -224,43 +222,43 @@
end

it "will return true when looking for something in the array" do
# Arrange
item = "a"
array = ["b", "c", "a"]
# Arrange
item = "a"
array = ["b", "c", "a"]

# Act
answer = search(array, item)
# Act
answer = search(array, item)

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

it "will return false when looking for something not in the array" do
# Arrange
item = "x"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal false
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]
# Act
answer = search(array, item)
# Assert
expect(answer).must_equal true
end
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal true
end
end

xdescribe "is_palindrome" do
describe "is_palindrome" do
it "will return true for emptystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -295,7 +293,7 @@
end
end

xdescribe "digit_match" do
describe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
Expand All @@ -304,8 +302,8 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 0 for nonmatching numbers" do
Expand All @@ -316,8 +314,8 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 3 for 841 and 62530841" do
Expand All @@ -328,10 +326,10 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 1 for (0, 0)" do
# Arrange
num1 = 0
Expand All @@ -340,10 +338,10 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 1 for (10, 20)" do
# Arrange
num1 = 10
Expand All @@ -352,7 +350,7 @@
# Act
answer = digit_match(num1, num2)

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