From a1413b6a2a685f22d843d9b71a8beab94794fcdb Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Wed, 13 Nov 2019 22:43:39 -0800 Subject: [PATCH 1/6] reversing in place --- lib/recursive-methods.rb | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..ade3ea9 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,65 @@ # 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" + 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" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + +#All the variables that you have to change would go in the parameters. +def reverse_inplace(s, first = 0, last = s.length - 1) + #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: ? def bunny(n) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + raise NotImplementedError, "Method not implemented" +end From a2db80fb78fecf7180067bdb0b6388315d6ecce2 Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Wed, 13 Nov 2019 23:40:40 -0800 Subject: [PATCH 2/6] reverse not in place --- lib/recursive-methods.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index ade3ea9..06ca894 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -10,14 +10,26 @@ def factorial(n) 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_t(string) +# reversed_str = "" +# string.each_char do |letter| +# puts reversed_str = letter + reversed_str +# end +# reversed_str +# end + +def reverse(string, rev = "", first = 0) + if rev.length == string.length + return rev + end + rev = string[first] + rev + reverse(string, rev, first + 1) end -# Time complexity: ? -# Space complexity: ? +# 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) From 3ff47ff8362f8ee538c02a88cc903aaebb26e3b1 Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Thu, 14 Nov 2019 00:28:45 -0800 Subject: [PATCH 3/6] palidrome recursive --- lib/recursive-methods.rb | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 06ca894..7024ccb 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -64,10 +64,28 @@ def search(array, value) 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, first = 0) + if string.length == length + if results.length > 1 + return false + else + return true + end + end + hash = {} + results = [] + if string[first] == nil + hash[first] = 0 + else + hash[first] += 1 + end + + if hash[first] % 2 != 0 + results << hash[first] + end + is_palindrome(string, length + 1, first + 1 ) end # Time complexity: ? From dcfc31e5a78192551c26a0ea5b74da6fe23bb219 Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Thu, 14 Nov 2019 00:50:04 -0800 Subject: [PATCH 4/6] completed bunny recursive method --- lib/recursive-methods.rb | 26 ++++++----- test/recursion_writing_test.rb | 84 +++++++++++++++++----------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 7024ccb..f65823b 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -12,13 +12,6 @@ def factorial(n) # Time complexity: O(n) # Space complexity: O(1) -# def reverse_t(string) -# reversed_str = "" -# string.each_char do |letter| -# puts reversed_str = letter + reversed_str -# end -# reversed_str -# end def reverse(string, rev = "", first = 0) if rev.length == string.length @@ -48,8 +41,11 @@ def reverse_inplace(s, first = 0, last = s.length - 1) # Time complexity: ? # Space complexity: ? + +#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) + bunny(n) end # Time complexity: ? @@ -67,6 +63,10 @@ def search(array, value) # Time complexity: O(n) # Space complexity: O(n) def is_palindrome(string, length = 0, first = 0) + if string == "" + return true + end + if string.length == length if results.length > 1 return false @@ -74,18 +74,20 @@ def is_palindrome(string, length = 0, first = 0) return true end end + hash = {} results = [] + if string[first] == nil - hash[first] = 0 + hash[first] = 0 else - hash[first] += 1 + hash[first] += 1 end if hash[first] % 2 != 0 - results << hash[first] + results << hash[first] end - is_palindrome(string, length + 1, first + 1 ) + is_palindrome(string, length + 1, first + 1) end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..93585f5 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -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 @@ -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 @@ -38,7 +37,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -83,8 +82,7 @@ end end - -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +127,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -352,7 +350,7 @@ # Act answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 1 + # Assert + expect(answer).must_equal 1 end end From e69087504c9af30702cd1747c3a2c4c4c29d0bc2 Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Thu, 14 Nov 2019 01:29:24 -0800 Subject: [PATCH 5/6] completed nested --- lib/recursive-methods.rb | 29 +++++++++++++++++++---------- test/recursion_writing_test.rb | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index f65823b..5020418 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -50,8 +50,17 @@ def bunny(n) # Time complexity: ? # Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +def nested(s, first = 0, last = s.length - 1) + 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: ? @@ -62,7 +71,7 @@ def search(array, value) # Time complexity: O(n) # Space complexity: O(n) -def is_palindrome(string, length = 0, first = 0) +def is_palindrome(string, length = 0, letter = 0, hash = {}, results = []) if string == "" return true end @@ -74,20 +83,20 @@ def is_palindrome(string, length = 0, first = 0) return true end end - + hash = {} results = [] - if string[first] == nil - hash[first] = 0 + if string[letter] == nil + hash[letter] = 0 else - hash[first] += 1 + hash[letter] += 1 end - if hash[first] % 2 != 0 - results << hash[first] + if hash[letter] % 2 != 0 + results << hash[letter] end - is_palindrome(string, length + 1, first + 1) + is_palindrome(string, length + 1, letter + 1, hash, results) end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 93585f5..1e1f86a 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -162,7 +162,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" From 06c3cc5213bcafbc2f291858f360524fd742c1ee Mon Sep 17 00:00:00 2001 From: Stephanie Garcia Date: Thu, 14 Nov 2019 01:31:43 -0800 Subject: [PATCH 6/6] added time and space complexity to nested and bunny --- lib/recursive-methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 5020418..e510ae9 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -39,8 +39,8 @@ def reverse_inplace(s, first = 0, last = s.length - 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) @@ -48,8 +48,8 @@ def bunny(n) bunny(n) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def nested(s, first = 0, last = s.length - 1) if first >= last return true