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

Angele Z. #32

Open
wants to merge 5 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
92 changes: 65 additions & 27 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,87 @@
require 'pry'
# 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.new("BAD") if n < 0
return 1 if n == 0
return n if n == 1
return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse(s)
raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
return reverse(s[1..-1]) + 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, i = 0)

Choose a reason for hiding this comment

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

👍

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

Choose a reason for hiding this comment

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

clever

s = reverse_inplace(s, i += 1)
end

# Time complexity: ?
# Space complexity: ?
# 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 + bunny(n - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) since using .length?

Choose a reason for hiding this comment

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

.length is actually O(1) because it's an instance variable (for that explicit reason).

# 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.

Not working 😢

raise NotImplementedError, "Method not implemented"
return false if s.length % 2 != 0
return true if s == ""
return true if s == "()"
center_l = s.length / 2 - 1
center_r = center_l + 1
if s[center_l] + s[center_r] != "()"
return false
else
next_string = s[center_l - 1] + s[center_r + 1]

Choose a reason for hiding this comment

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

This means you're going from a string which could be 14 characters long immediately to a string of 2 characters...

This won't work in all cases.

end
return nested(next_string)
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.

👍
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 false if array.empty?
return true if array[0] == value
new_array = array[1..-1]
return search(array[1..-1], value)

Choose a reason for hiding this comment

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

array[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 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.length <= 1
if s[0] != s[-1]
return false
else
next_string = s[1..-2]
end
return is_palindrome(next_string)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) since it uses .length in each loop?
# 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.

👍 This works, but you have similar time/space issues with the above methods.

raise NotImplementedError, "Method not implemented"
end
n = n.to_s
m = m.to_s
return 0 if n == nil || m == nil || n.length == 0 || m.length == 0
match = n[-1] == m[-1]
if match && (n.length == 1 || m.length == 1)
return 1
elsif !match && (n.length == 1 || m.length == 1)
return 0
elsif !match
return 0 + digit_match(n[0..-2].to_i, m[0..-2].to_i)
else
return 1 + digit_match(n[0..-2].to_i, m[0..-2].to_i)
end
end
28 changes: 14 additions & 14 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end
end

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

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

xdescribe "search" do
describe "search" do
it "will return false for empty array" do
# Arrange
item = "a"
Expand All @@ -224,15 +224,15 @@
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
Expand Down Expand Up @@ -260,7 +260,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,7 +295,7 @@
end
end

xdescribe "digit_match" do
describe "digit_match" do
it "returns 4 for 1072503891 and 62530841" do
# Arrange
num1 = 1072503891
Expand Down