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
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
GEM
remote: https://rubygems.org/
specs:
ansi (1.5.0)
builder (3.2.4)
coderay (1.1.3)
method_source (1.0.0)
minitest (5.14.2)
minitest-reporters (1.4.2)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
minitest-skip (0.0.3)
minitest (~> 5.0)
minitest-spec (0.0.2.1)
minitest (>= 3.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.1)
ruby-progressbar (1.10.1)

PLATFORMS
ruby

DEPENDENCIES
minitest
minitest-reporters
minitest-skip
minitest-spec
pry
rake

BUNDLED WITH
2.1.4
73 changes: 49 additions & 24 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
# 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^2)
def factorial(n)
Comment on lines +3 to 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 But your space complexity is O(n)

raise NotImplementedError, "Method not implemented"
return raise ArgumentError if n < 0
return 1 if n == 0 || n == 1
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n^2)
def reverse(s)
Comment on lines +11 to 13

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 But your space/time complexity is O(n^2)

raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
reverse_s = reverse(s[1..-1])
reverse_s += s[0]
return reverse_s
end

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ This is not in place as with each iteration you're creating a new string.

Consider adjusting the header to be like this.

def reverse_inplace(s, low=0, high=s.length - 1)

raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
n = s[-1] + reverse_inplace(s[1..-2]) + s[0]
s.replace n # I don't know how to reverse in place without using .replace...
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
Comment on lines +28 to 30

Copy link
Copy Markdown

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n^2)
def nested(s)
Comment on lines +35 to 37

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 However the space/time complexity are both O(n^2)

raise NotImplementedError, "Method not implemented"
return true if s.length == 0
return false unless s[0] == "(" && s[-1] == ")"
return nested(s[1..-2])
end

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 However the space/time complexity are both O(n^2)

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 However the space/time complexity are both O(n^2)

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

# Time complexity: O(n)
# Space complexity: O(n^2)
def helper_digit_match(n_string, m_string, matches)
Comment on lines +59 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 However the space/time complexity are both O(n^2)

return matches if n_string[-1].nil? || n_string[-1].nil?
if n_string[-1] == m_string[-1]
helper_digit_match(n_string[0..-2], m_string[0..-2], matches += 1)
else
helper_digit_match(n_string[0..-2], m_string[0..-2], matches)
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
n_string = n.to_s
m_string = m.to_s
return helper_digit_match(n_string, m_string, 0)
end
99 changes: 71 additions & 28 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 All @@ -227,43 +227,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 @@ -298,7 +298,7 @@
end
end

xdescribe "digit_match" do
describe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
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 All @@ -359,3 +359,46 @@
expect(answer).must_equal 1
end
end



# Sam's reverse in line tests

describe "reverse_in_place" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
# Act
answer = reverse_inplace(string)
# Assert
expect(answer).must_equal "tac"
expect(string).must_equal(answer)
end
it "will reverse 'a'" do
# Arrange
string = "a"
# Act
answer = reverse_inplace(string)
# Assert
expect(answer).must_equal "a"
expect(string).must_equal(answer)
end
it "will reverse empty string " do
# Arrange
string = ""
# Act
answer = reverse_inplace(string)
# Assert
expect(answer).must_equal ""
expect(string).must_equal(answer)
end
it "will reverse 'apple'" do
# Arrange
string = "apple"
# Act
answer = reverse_inplace(string)
# Assert
expect(answer).must_equal "elppa"
expect(string).must_equal(answer)
end
end