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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.rakeTasks
112 changes: 89 additions & 23 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,115 @@
# Authoring recursive algorithms. Add comments including time and space complexity for each method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: n
# Space complexity: 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 1 if n == 1
return n * factorial(n - 1)
end

# Time complexity: ?
# Space complexity: ?
def reverse(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: n^2
# Space complexity: n^2
def reverse(s) #was the answer to this supposed to be different from the one below??
Comment on lines +12 to +14

Choose a reason for hiding this comment

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

👍

if s.length == 0
return s
else
return reverse(s[1..-1]) +s[0]
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: n
# Space complexity: n
def reverse_inplace(s)
Comment on lines +22 to 24

Choose a reason for hiding this comment

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

👍 This is the same as the previous method and it's not in place.

Consider passing the front and rear indexes and swapping elements at each index.

def reverse_inplace(s, front = 0, last = s.length - 1)
  return s if front >= last

  temp = s[last]
  s[last] = s[front]
  s[front] = temp

  return reverse_inplace(s, front + 1, last - 1)
end

raise NotImplementedError, "Method not implemented"
#fix - add a few default arguments
#add optional arguments
if s.length == 0
return s
else
return reverse(s[1..-1]) + s[0]
end

#Try Iterator to Recusion...
# reverse_index = -1
# count = 0
# length = s.length
#
# while count < length / 2
# temp = s[count]
# s[count] = s[reverse_index]
# s[reverse_index] = temp
#
# count += 1
# reverse_index -= 1
# end
# return s
end

# Time complexity: ?
# Space complexity: ?
#***
# Could not for the life of me
# figure out how to do this recursively based
# on the tests provided.
# Didn't even use a loop to convert...>_<
#***
# Time complexity: n
# Space complexity: n
def bunny(n)
Comment on lines +55 to 57

Choose a reason for hiding this comment

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

👍 This works.

raise NotImplementedError, "Method not implemented"
return 0 if n == 0
if n > 0
return bunny(n - 1) + 2
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: n^2
# Space complexity: n^2
def nested(s)
Comment on lines +64 to 66

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 true if s.length == 0
return false if s.length % 2 != 0
if s[0] != s[-1]
return nested(s[1..-2])
else
return false
end
end


# Time complexity: ?
# Space complexity: ?
def search(array, value)
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.

This works with O(n^2) space/time complexity.

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
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: n
# Space complexity: n
def is_palindrome(s)
Comment on lines +88 to 90

Choose a reason for hiding this comment

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

👍 But time/space complexity is O(n^2)

raise NotImplementedError, "Method not implemented"
return true if s.length == 0
if s[0] == s[-1]
return is_palindrome(s[1..-2])
else
return false
end
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: n
# Space complexity: n
#
# how many calls
# how expensive is each call
def digit_match(n, m)
Comment on lines +99 to 104

Choose a reason for hiding this comment

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

👍 This is O(log n) if n is the size of the smaller of the two numbers (basically you divide by 10 each recursive call).

raise NotImplementedError, "Method not implemented"
if n < 10 || m < 10
last_digit_m = m % 10
last_digit_n = n % 10
return last_digit_m == last_digit_n ? 1 : 0
else
last_digit_m = m % 10
last_digit_n = n % 10
match = last_digit_m == last_digit_n ? 1 : 0
return digit_match(n / 10, m / 10) + match
end
end
32 changes: 26 additions & 6 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
end
end

xdescribe "nested" do
it "will return true for empystring" do
describe "nested" do
it "will return true for empty string" 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 All @@ -311,7 +311,7 @@
expect(answer).must_equal 4
end

it "returns 0 for nonmatching numbers" do
it "returns 0 for non matching numbers" do
# Arrange
num1 = 0
num2 = 62530841
Expand Down Expand Up @@ -358,4 +358,24 @@
# Assert
expect(answer).must_equal 1
end

it "returns 0 for (107, 6)" do
# Arrange

# Act
expect(digit_match(107, 6)).must_equal 0
expect(digit_match(6, 107)).must_equal 0
expect(digit_match(0, 0)).must_equal 1
expect(digit_match(2, 2)).must_equal 1
expect(digit_match(7, 6)).must_equal 0
expect(digit_match(10, 0)).must_equal 1
expect(digit_match(10, 1)).must_equal 0

expect(digit_match(1007, 16)).must_equal 0
expect(digit_match(1007, 27)).must_equal 1


# Assert
# expect(answer).must_equal 0
end
end