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
107 changes: 82 additions & 25 deletions lib/recursive-methods.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,106 @@
# 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)

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 1 if n == 0
raise ArgumentError, "must be a number greater than 0." 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: O(n)
# Space complexity: O(1)

def reverse(string, rev = "", first = 0)

Choose a reason for hiding this comment

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

👍 Nice work, however because you are creating new arrays with each iteration you have time/space complexity of O(n2)

if rev.length == string.length
return rev
end
rev = string[first] + rev

Choose a reason for hiding this comment

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

String concatenation creates a new array and copies all the individual elements over and so is O(n) by itself.

reverse(string, rev, first + 1)
end

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

#All the variables that you have to change would go in the parameters.
def reverse_inplace(s, first = 0, last = s.length - 1)

Choose a reason for hiding this comment

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

👍 However your space complexity is O(n) because of the call stack.

#This is usually the condition in the loop, reversed. Example while first < last becuase your saying when should I stop not when should i keep going.
if first >= last
return s
end
#this is the inside of the loop
temp = s[first]
s[first] = s[last]
s[last] = temp

# this is the bottom of the loop where you say i += 1
return reverse_inplace(s, first + 1, last - 1)
end

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1)
# Space complexity: O(1)

#I want to add n to itself,but I don't now what my base case would be
def bunny(n)
raise NotImplementedError, "Method not implemented"
return ears = (n + n)

Choose a reason for hiding this comment

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

Suggested change
return ears = (n + n)
ears = 2

bunny(n)

Choose a reason for hiding this comment

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

Just for readability

Suggested change
bunny(n)
return ears + bunny(n)

end

# Time complexity: ?
# Space complexity: ?
def nested(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def nested(s, first = 0, last = s.length - 1)

Choose a reason for hiding this comment

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

👍

if first >= last
return true
end

if s[first] == s[last] || s.length % 2 == 1
return false
end

#this is the inside of the loop
return nested(s, first + 1, last - 1)
end

# Time complexity: ?
# Space complexity: ?
def search(array, value)

Choose a reason for hiding this comment

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

We'll go over this in class.

raise NotImplementedError, "Method not implemented"
raise NotImplementedError, "Method not implemented"
end

# Time complexity: ?
# Space complexity: ?
def is_palindrome(s)
raise NotImplementedError, "Method not implemented"
# Time complexity: O(n)
# Space complexity: O(n)
def is_palindrome(string, length = 0, letter = 0, hash = {}, results = [])

Choose a reason for hiding this comment

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

I think you made this a little more complicated than you needed to here.

def is_palindrome(string, first = 0, last = string.length -1)
  return true if first > last
  return false if string[first] != string[last]
  return is_palindrome(string, first + 1, last -1)
end

if string == ""
return true
end

if string.length == length
if results.length > 1
return false
else
return true
end
end

hash = {}
results = []

if string[letter] == nil
hash[letter] = 0
else
hash[letter] += 1
end

if hash[letter] % 2 != 0
results << hash[letter]
end
is_palindrome(string, length + 1, letter + 1, hash, results)
end

# Time complexity: ?
# Space complexity: ?
def digit_match(n, m)
raise NotImplementedError, "Method not implemented"
end
raise NotImplementedError, "Method not implemented"
end
86 changes: 42 additions & 44 deletions test/recursion_writing_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"
require_relative '../lib/recursive-methods'
require_relative "../lib/recursive-methods"

describe "factorial" do
it "will find the factorial of 0" do
Expand All @@ -23,8 +23,7 @@
answer = factorial(num)

# Assert
expect(answer).must_equal 5*4*3*2*1

expect(answer).must_equal 5 * 4 * 3 * 2 * 1
end

it "will raise an ArgumentError if given a number not >= 0" do
Expand All @@ -38,7 +37,7 @@
end
end

xdescribe "reverse" do
describe "reverse" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -83,8 +82,7 @@
end
end


xdescribe "reverse_in_place" do
describe "reverse_in_place" do
it "will reverse 'cat'" do
# Arrange
string = "cat"
Expand Down Expand Up @@ -129,7 +127,7 @@
end
end

xdescribe "bunny" do
describe "bunny" do
it "returns 0 for 0 bunnies" do
# Arrange
count = 0
Expand Down Expand Up @@ -164,7 +162,7 @@
end
end

xdescribe "nested" do
describe "nested" do
it "will return true for empystring" do
# Arrange
string = ""
Expand Down Expand Up @@ -224,40 +222,40 @@
end

it "will return true when looking for something in the array" do
# Arrange
item = "a"
array = ["b", "c", "a"]
# Arrange
item = "a"
array = ["b", "c", "a"]

# Act
answer = search(array, item)
# Act
answer = search(array, item)

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

it "will return false when looking for something not in the array" do
# Arrange
item = "x"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal false
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]
# Act
answer = search(array, item)
# Assert
expect(answer).must_equal true
end
end

it "will return true when finding something at the front of the array" do
# Arrange
item = "b"
array = ["b", "c", "a"]

# Act
answer = search(array, item)

# Assert
expect(answer).must_equal true
end
end

xdescribe "is_palindrome" do
Expand Down Expand Up @@ -304,8 +302,8 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 0 for nonmatching numbers" do
Expand All @@ -316,8 +314,8 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 3 for 841 and 62530841" do
Expand All @@ -328,10 +326,10 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 1 for (0, 0)" do
# Arrange
num1 = 0
Expand All @@ -340,10 +338,10 @@
# Act
answer = digit_match(num1, num2)

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

it "returns 1 for (10, 20)" do
# Arrange
num1 = 10
Expand All @@ -352,7 +350,7 @@
# Act
answer = digit_match(num1, num2)

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