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

finished assignment #50

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
6 changes: 6 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.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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.

27 changes: 27 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
84 changes: 63 additions & 21 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
# 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 == 0 || n == 1
return 1
elsif n < 0
raise ArgumentError, " Enter a number greater than 0."
else
return n * factorial(n - 1)
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n) - N being the length of string
# Space complexity: o(n) - we would need n amount of space for each character in string
def reverse(s)
raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
else
return reverse(s[1..-1]) + s[0]
end

end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n) - n being how long the string is
# Space complexity: o(n) - n ammout of space for each char in string
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
if s.length <= 1
return s
else
return s[-1] + reverse(s[1..-2]) + s[0]
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n)
# Space complexity: o(n
def bunny(n)
raise NotImplementedError, "Method not implemented"
return n if n == 0

if n == 1
return 2
else
return 2 + bunny(n - 1)
end
end

# Time complexity: ?
Expand All @@ -30,20 +52,40 @@ def nested(s)
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: o(n)
# Space complexity: o(n)
def search(array, value)
raise NotImplementedError, "Method not implemented"
if array.length == 0
return false
elsif array[0] == value
return true
else
return 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"
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)
# Space complexity: o(n)
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
n = n.to_s
m = m.to_s
if n.length == 0 || m.length == 0
return 0
elsif n[-1] == m[-1]
return 1 + digit_match(n[0..-2], m[0..-2])
else
return digit_match(n[0..-2], m[0..-2])
end
end