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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is size of number
# Space complexity: O(n)
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 unless n >= 0

return 1 if n == 0

return n * factorial(n-1)

end

# Time complexity: ?
# Space complexity: ?

# Time complexity: 0(n^2) where n is length od string
# Space complexity: 0(n^2) where n is length of string

def reverse(s)
Comment on lines +15 to 18

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 < 2
return s[-1] + reverse(s[1..-2]) + s[0]
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"

# Time complexity: o(n) where n is length of string
# Space complexity: o(n)
def reverse_inplace(s, first = 0, last = s.length - 1)
Comment on lines +24 to +26

Choose a reason for hiding this comment

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

👍

return s if first == last || s.empty?
s[first], s[last] = s[last], s[first]
return reverse_inplace(s, first + 1, last - 1)
end

# Time complexity: ?
# Space complexity: ?

# Time complexity: O(n) where n is size of number
# Space complexity: O(n)
def bunny(n)
Comment on lines +33 to 35

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

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: o(n) where n is length of string
# Space complexity: o(n)

def nested(s, first = 0, sub_length = s.length)
Comment on lines +41 to +44

Choose a reason for hiding this comment

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

👍


return false if sub_length == 1

return true if first == sub_length

return (s[first] == '(' && s[sub_length-1] == ')') ? nested(s, first + 1, sub_length - 1) : false

end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n) where n is length of array
# Space complexity: O(n)

def search(array, value, i = 0)
Comment on lines +54 to +57

Choose a reason for hiding this comment

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

👍

return true if array[i] == value

return false if i == array.length

return search(array, value, i + 1)
end

# Time complexity: O(n) where n is the length of string
# Space complexity: O(n) where n is the length of string
#
def is_palindrome(s, first = 0, sub_length = s.length)
Comment on lines +65 to +68

Choose a reason for hiding this comment

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

👍


return true if first == sub_length || first == sub_length - 1

return (s[first] == s[sub_length-1]) ? is_palindrome(s, first + 1, sub_length - 1) : false

end


# Time complexity: O(logn) where n is the largest number
# Space complexity: O(logn)
def digit_match(n, m, count = 0)
Comment on lines +77 to +79

Choose a reason for hiding this comment

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

👍

if n/10 == 0 && m/10 == 0
return n == m ? count + 1 : count
end

if n/10 == 0
return m % 10 == n ? count + 1 : count
elsif m/10 == 0
return n % 10 == m ? count + 1 : count
else
return n%10 == m%10 ? digit_match(n/10, m/10, count + 1) : digit_match(n/10, m/10, count)
end
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
def fib(n)

Choose a reason for hiding this comment

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

Time & space complexity?

return n if n <= 1
return fib(n - 1) + fib(n - 2)
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# def fib_non_rec(n)
# seq = Array.new(n+2)
#
# seq[0] = 0
# seq[1] = 1
#
# (2..n).each { |i| seq[i] = seq[i - 1] + seq[i - 2]}
#
# return seq[n]
# end
33 changes: 29 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 @@ -296,9 +296,20 @@
# Assert
expect(answer).must_equal false
end

it "will return true for a even palindrome" do
# Arrange
string = "baiiab"

# Act
answer = is_palindrome(string)

# Assert
expect(answer).must_equal true
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 +370,17 @@
expect(answer).must_equal 1
end
end

describe "fib" do
it "returns 3 when passed 4" do
expect(fib(4)).must_equal 3
end

it "returns 13 when passed 7" do
expect(fib(7)).must_equal 13
end

it "returns 0 when passed 0" do
expect(fib(0)).must_equal 0
end
end