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- Erika #46

Open
wants to merge 7 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
114 changes: 83 additions & 31 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,101 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.
# # # # # 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"

raise ArgumentError unless n >= 0
return 1 if n == 1 || n == 0

n * factorial(n - 1)

end

# Time complexity: ?
# Space complexity: ?

# # # # Time complexity: O(n)
# # # # Space complexity: O(n) maybe less?
def reverse(s)

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 s if s.length <= 1
reversed = reverse(s[1..-1])
reversed << s[0]

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, pointer = 0)

return s if pointer == s.length / 2
s[pointer], s[- 1 - pointer] = s[- 1 - pointer ], s[pointer]

reverse_inplace(s, pointer + 1)

end

# Time complexity: ?
# Space complexity: ?

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

return 0 if n == 0
return 2 if n == 1
bunny(n - 1) + 2

end

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

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

def nested(s, i = 0, j = s.length - 1)

return false if s.length.odd?
return false if s[i] == "(" && s[j] != ")" || (s[i] == ")" && i < j)
return true if i > j || s.empty?


nested(s, i + 1, j - 1)

end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"

# # # # Time complexity: O(n)
# # # # Space complexity: O(n)
def search(array, value, i = 0)

return true if array[i] == value
return false if i == array.length
search(array, value, i + 1)

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 = -1)

return false if s[i] != s[j]
return true if i > s.length - 1

is_palindrome(s, i + 1, j - 1)

end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# # # Time complexity: O(n)
# # # Space complexity: O(n)
def digit_match(n, m, matches = 0)

dig1 = n % 10
dig2 = m % 10

return matches if n == 0 || m == 0
matches += 1 if dig1 == dig2

n = n / 10
m = m / 10
digit_match(n, m, matches)

end
116 changes: 59 additions & 57 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/skip_dsl"
# require 'minitest/reporters'
# require "minitest/skip_dsl"
require_relative '../lib/recursive-methods'

# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "factorial" do
it "will find the factorial of 0" do
# Arrange
Expand Down Expand Up @@ -38,7 +40,7 @@
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -84,7 +86,7 @@
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 +131,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 +166,7 @@
end
end

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

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

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

xdescribe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
num2 = 62530841
# xdescribe "digit_match" do
# it "returns 4 for 1072503891 and 62530841" do
# # Arrange
# num1 = 1072503891
# num2 = 62530841

# Act
answer = digit_match(num1, num2)
# # Act
# answer = digit_match(num1, num2)

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

it "returns 0 for nonmatching numbers" do
# Arrange
num1 = 0
num2 = 62530841
# it "returns 0 for nonmatching numbers" do
# # Arrange
# num1 = 0
# num2 = 62530841

# Act
answer = digit_match(num1, num2)
# # Act
# answer = digit_match(num1, num2)

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

it "returns 3 for 841 and 62530841" do
# Arrange
num1 = 841
num2 = 62530841
# it "returns 3 for 841 and 62530841" do
# # Arrange
# num1 = 841
# num2 = 62530841

# Act
answer = digit_match(num1, num2)
# # Act
# answer = digit_match(num1, num2)

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

it "returns 1 for (0, 0)" do
# Arrange
num1 = 0
num2 = 0
# it "returns 1 for (0, 0)" do
# # Arrange
# num1 = 0
# num2 = 0

# Act
answer = digit_match(num1, num2)
# # Act
# answer = digit_match(num1, num2)

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

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

# Act
answer = digit_match(num1, num2)

# Assert
expect(answer).must_equal 1
end
end
# it "returns 1 for (10, 20)" do
# # Arrange
# num1 = 10
# num2 = 20

# # Act
# answer = digit_match(num1, num2)

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