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

Fire: Ayesha Recursion writing methods #42

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .floo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://floobits.com/ayesha/recursion-writing"
}
6 changes: 6 additions & 0 deletions .flooignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern
node_modules
tmp
vendor
.idea/workspace.xml
.idea/misc.xml
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .idea/recursion-writing.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
127 changes: 101 additions & 26 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,124 @@
# frozen_string_literal: true

# 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"
if n.negative?
raise ArgumentError, 'n must be greater than or equal to 0.'
elsif n.zero?
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)
raise NotImplementedError, "Method not implemented"
if s.length == 0 || s.length == 1
return s
end
return s[s.length-1] + reverse(s[0, s.length - 1])
end

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

if s.length.zero? || s.length == 1
return s
end
length = s.length - 1
return pointers(s, 0, length)

end

def pointers(s, start_point, end_point)
if start_point >= end_point
return s
end
temp = s[start_point]
s[start_point] = s[end_point]
s[end_point] = temp
return pointers(s, (start_point + 1), (end_point - 1))
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
raise NotImplementedError, "Method not implemented"
if n.negative?
raise ArgumentError, 'n must be greater than or equal to 0.'
elsif n.zero?
0
else
bunny(n - 1) + 2
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s)
raise NotImplementedError, "Method not implemented"
right_parenthesis = ')'
left_parenthesis = '('
if s.nil? || s.length.zero?
true
elsif s.length > 1
if s[0] == left_parenthesis && s[-1] == right_parenthesis
nested(s[1...-1])
else
false
end
else
false
end
end

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

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
s = s.gsub(/[^[:alnum:]]/, '').downcase
if s.nil? || s.length.zero?
true
else
if s[0] == s[-1]
return is_palindrome(s[1...-1])
else
return false
end
false
end
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
# Time complexity: O(n^2)
# Space complexity: O(n^2)
def digit_match(n, m);
if n.nil? || m.nil?
return 0
elsif n.to_s.empty? || m.to_s.empty?
return 0
elsif n.to_s[-1] == m.to_s[-1]
return 1 + digit_match(n.to_s[0..-2], m.to_s[0..-2])
else
return 0 + digit_match(n.to_s[0..-2], m.to_s[0..-2])
end
end

# Time complexity: O(n^2)
# Space complexity: O(n^2)



23 changes: 23 additions & 0 deletions recursion-writing.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUBY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="rbenv: 2.6.5" jdkType="RUBY_SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="ansi (v1.5.0, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="builder (v3.2.4, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="coderay (v1.1.3, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="method_source (v1.0.0, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="minitest (v5.14.2, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="minitest-reporters (v1.4.2, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="minitest-skip (v0.0.3, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="minitest-spec (v0.0.2.1, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="pry (v0.13.1, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rake (v13.0.1, rbenv: 2.6.5) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.10.1, rbenv: 2.6.5) [gem]" level="application" />
</component>
<component name="RubyModuleProperties">
<option name="autoCreated" value="true" />
</component>
</module>
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