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

Wrote recursive methods #33

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - it takes one recursion for every increase in n
# Space complexity: O(n) - because of the system stack
def factorial(n)
Comment on lines +3 to 5

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

return n * factorial(n-1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - It takes n recursions for each character
# Space complexity: O(n^2) - n is number of characters
# String objects with length n * (n-1) * (n-2) ... are made
def reverse(s)
Comment on lines +12 to 15

Choose a reason for hiding this comment

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

👍 , however your space/time complexity are both O(n^2)

raise NotImplementedError, "Method not implemented"
return s if s.length <= 1

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - It will take n/2 swaps to reverse the string
# Space complexity: O(n) - For the system stack
def reverse_inplace(s)
Comment on lines +21 to 23

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

return reverse_inplace_helper(s, 0, s.length-1)
end

def reverse_inplace_helper(s, start, fin)
return s if start == fin

s[start], s[fin] = s[fin], s[start]
reverse_inplace_helper(s, start+1, fin - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - one recursion for each bunny
# Space complexity: O(n) - system stack
def bunny(n)
Comment on lines +36 to 38

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 bunny_tail(n, 0)
end

# Time complexity: ?
# Space complexity: ?
def bunny_tail(n, ears)
return ears if n == 0

return bunny_tail(n-1, ears+2)
end

# Time complexity: O(n) - has to look through each character
# Space complexity: O(n) - system stack
def nested(s)
Comment on lines +48 to 50

Choose a reason for hiding this comment

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

This is working if the number of "(" and ")" match, but what about ")("?

raise NotImplementedError, "Method not implemented"
return nested_helper(s, 0, s.length, 0)
end

# Time complexity: ?
# Space complexity: ?
def nested_helper(s, char, fin, parens)
return true if char == fin

if s[char] == '('
nested_helper(s, char+1, fin, parens + 1)
else
return false if parens == 0

nested_helper(s, char+1, fin, parens - 1)
end
end

# Time complexity: O(n) - Linear search
# Space complexity: O(n) - system stack
def search(array, value)
Comment on lines +66 to 68

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 search_helper(array, value, 0, array.length)
end

def search_helper(array, value, idx, len)
if idx == len
return false
elsif array[idx] == value
return true
else
return search_helper(array, value, idx+1, len)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - n/2 recursions
# Space complexity: O(n) - system stack
def is_palindrome(s)
Comment on lines +82 to 84

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 true if s.empty?

return is_palindrome_helper(s, 0, s.length - 1)
end

def is_palindrome_helper(s, start, fin)
if start == fin
return true
elsif s[start] != s[fin]
return false
else
is_palindrome_helper(s, start+1, fin-1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(logn) - Where n is the larger of n or m.
# Reduces n by a factor of 10 every recursion
# Space complexity: O(logn) - System stack
def digit_match(n, m)
Comment on lines +100 to 103

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"
end
# Handle edge case where one or both numbers are zero
if n == 0 || m == 0
if (n % 10) == (m % 10)
return 1
else
return 0
end
end
return digit_match_helper(n, m, 0)
end

def digit_match_helper(n, m, digits)
return digits if n == 0 || m == 0
if (n % 10) == (m % 10)
return digit_match_helper(n/10, m/10, digits+1)
else
return digit_match_helper(n/10, m/10, digits)
end
end

# Time complexity: O(n) - One recursion for every n
# Space compexlity: O(n) - System stack
def fib(n, f0 = 0, f1 = 1, index = 1)
Comment on lines +124 to +126

Choose a reason for hiding this comment

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

Nice work on a tail recursive version of Fibonacci.

return 0 if n == 0
return f1 if index == n
fib(n, f1, f0+f1, index+1)
end
22 changes: 18 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 Expand Up @@ -359,3 +359,17 @@
expect(answer).must_equal 1
end
end

describe "fib" do
it 'returns 0 for 0' do
expect(fib(0)).must_equal 0
end

it 'returns 3 for 4' do
expect(fib(4)).must_equal 3
end

it 'returns 89 for 11' do
expect(fib(11)).must_equal 89
end
end