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

Water - Jessica #51

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
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
110 changes: 86 additions & 24 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,111 @@
# 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)
raise NotImplementedError, "Method not implemented"
raise ArgumentError if n < 0
if n <= 1
return 1
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)
raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
else
return s[-1] + reverse(s.chop)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
reverse_helper(0, s.length - 1, s)
return s
end

# Time complexity: ?
# Space complexity: ?
def reverse_helper(i,j, string)
if i < j
temp = string[i]
string[i] = string[j]
string[j] = temp
reverse_helper(i + 1, j - 1, string)
end
end

# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
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)
def nested(s)
raise NotImplementedError, "Method not implemented"
nested_helper(0, s.length - 1, s)
end

def nested_helper(i,j, string)
if i > j
return true
elsif string[i] == '(' && string[j] == ')'
return nested_helper(i + 1, j - 1, string)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def search(array, value)
raise NotImplementedError, "Method not implemented"
return false if array.length == 0
if array[0] == value
return true
else
return search(array[1..-1], value)
end
return false
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
if s.length < 2
return true
else
palindrome_helper(0,s.length - 1, s)
end
end

def palindrome_helper(i,j,string)
if i > j
return true
elsif string[i] == string[j]
palindrome_helper(i+1, j-1, string)
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(logn)
# Space complexity: O(logn)
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
if n == 0 && m == 0
return 1
elsif n <= 1 || m <= 1
return 0
end
if n%10 == m%10
return 1 + digit_match(n/10, m/10)
else
return digit_match(n/10, m/10)
end
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