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
111 changes: 87 additions & 24 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,112 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the length
# Space complexity: O(n), uses up some space in the call stack until it reaches the base case
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.

👍

raise NotImplementedError, "Method not implemented"
if n == 0
return 1
elsif n <= 0
raise ArgumentError
else
return 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 +15 to 17

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"
if s.length <= 1
return s
end

return s[-1] + reverse(s[1..-2]) + s[0]
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)
Comment on lines +25 to 27

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"
first = 0
last = s.length - 1
return reverse_inplace_helper(s, first, last)
end

def reverse_inplace_helper(s, first = 0, last = s.length - 1)
if first > last
return s
end

temp = s[first]
s[first] = s[last]
s[last] = temp

return reverse_inplace_helper(s, first + 1, last - 1)
end

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

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"
if n == 0
return 0
end

return 2 + bunny(n-1)
end

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

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 can you see a way, like with reverse_inplace to do it with O(n) time/space complexity?

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

end

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

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 can you see a way, like with reverse_inplace to do it with O(n) time/space complexity?

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

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

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 can you see a way, like with reverse_inplace to do it with O(n) time/space complexity?

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)?
# Space complexity: O(n^2)?
def digit_match(n, m)
Comment on lines +94 to 96

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 can you see a way, like with reverse_inplace to do it with O(n) time/space complexity?

raise NotImplementedError, "Method not implemented"
n = n.to_s
m = m.to_s

count = 0
return digit_match_counter(n, m, count)
end

def digit_match_counter(n, m, count)
if n.length == 0 || m.length == 0
return count
elsif n[-1] == m[-1]
count += 1
end

return digit_match_counter(n[0..-2], m[0..-2], count)
end
8 changes: 4 additions & 4 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 Down Expand Up @@ -263,7 +263,7 @@
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