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

Yasmin- Leaves #34

Open
wants to merge 1 commit 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
65 changes: 50 additions & 15 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,76 @@
# 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 NotImplementedError, "Method not implemented"
if n < 0
raise ArgumentError
elsif n <= 1
return 1
else
n * factorial(n-1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n^2)
# Space complexity: o(n^2)
def reverse(s)
Comment on lines +16 to 18

Choose a reason for hiding this comment

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

Correct on the time/space complexities. Can you think of a way to do better?

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

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
end

# Time complexity: ?
# Space complexity: ?
# Time complexity:
# Space complexity:
def reverse_inplace(s)

Choose a reason for hiding this comment

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

We'll go over this in class.

raise NotImplementedError, "Method not implemented"

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"
# raise NotImplementedError, "Method not implemented"
if n == 0
return 0
else
return 2 + bunny(n-1)
end
end

# Time complexity: ?
# Space complexity: ?

# Time complexity: o(n)
# Space complexity: o(n^2)
def nested(s)

Choose a reason for hiding this comment

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

👍 Time complexity is also O(n^2) because of s[1...-1]

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

# Time complexity: ?
# Space complexity: ?
def search(array, value)
Comment on lines 60 to 62

Choose a reason for hiding this comment

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

👍 Big-O is O(n^2) due to the repeated creation of new arrays.

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


# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
Expand Down
8 changes: 4 additions & 4 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 @@ -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 Down