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
82 changes: 52 additions & 30 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
# 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)
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 == 0
return n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def reverse(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n^2), where n is the length of the string
# Space complexity: O(n^2), where n is the length of the string
def reverse(s, pointer = s.length - 1)
Comment on lines +11 to +13

Choose a reason for hiding this comment

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

👍

return s if s == ""
return s[pointer] if pointer == 0
return s[pointer] + reverse(s, pointer - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the length of the string. (More precisely, n/2)
# Space complexity: O(n), where n is the length of the string. (More precisely, n/2)
def reverse_inplace(s)
Comment on lines +19 to 21

Choose a reason for hiding this comment

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

👍 Nicely done!

raise NotImplementedError, "Method not implemented"
return s if s.length == 0 || s.length == 1
length = s.length - 1
return reverse_inplace_recursive(s, 0, length)
end

# Time complexity: ?
# Space complexity: ?
def reverse_inplace_recursive(s, left, right)
return s if left >= right
holder = s[left]
s[left] = s[right]
s[right] = holder
return reverse_inplace_recursive(s, left + 1, right - 1)
end

# Time complexity: O(n)
# Space complexity: O(n)
def bunny(n)
Comment on lines +35 to 37

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

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), where n is the length of the string
def nested(s, parens_counter = 0, pointer = 0)
Comment on lines +43 to +45

Choose a reason for hiding this comment

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

👍

return false if parens_counter < 0
return true if pointer == s.length && parens_counter == 0
return false if pointer == s.length && parens_counter != 0
return nested(s, parens_counter + 1, pointer + 1) if s[pointer] == "("
return nested(s, parens_counter - 1, pointer + 1) if s[pointer] == ")"
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), where n is the length of the string
# Space complexity: O(n), where n is the length of the string
def search(array, value, pointer = 0)
Comment on lines +53 to +55

Choose a reason for hiding this comment

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

👍

return false if pointer == array.length
return true if array[pointer] == value
return search(array, value, pointer + 1)
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n), where n is the length of the string (More precisely, n/2)
# Space complexity: O(n), where n is the length of the string (More precisely, n/2)
def is_palindrome(s, length = s.length, pointer = 0)
Comment on lines +61 to +63

Choose a reason for hiding this comment

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

👍 Well done!

return true if pointer > length / 2
return false if s[pointer] != s[length - 1 - pointer]
return is_palindrome(s, length, pointer + 1)
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
def digit_match(n, m, pointer = -1, matches = 0)

end