From e868b7aeadb54c2c7959c70299c7ac881fc93684 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Nov 2019 19:38:15 -0800 Subject: [PATCH 1/4] added mores methods --- test/recursion_writing_test.rb | 219 +++++++++++++++++---------------- 1 file changed, 110 insertions(+), 109 deletions(-) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..af8235b 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -1,5 +1,6 @@ require 'minitest/autorun' require 'minitest/reporters' +require 'minitest/pride' require "minitest/skip_dsl" require_relative '../lib/recursive-methods' @@ -7,30 +8,30 @@ it "will find the factorial of 0" do # Arrange num = 0 - + # Act answer = factorial(num) - + # Assert expect(answer).must_equal 1 end - + it "will find the factorial of 5" do # Arrange num = 5 - + # Act answer = factorial(num) - + # Assert expect(answer).must_equal 5*4*3*2*1 - + end - + it "will raise an ArgumentError if given a number not >= 0" do # Arrange num = -1 - + # Act-Assert expect { answer = factorial(num) @@ -38,258 +39,258 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "tac" end - + it "will reverse 'a'" do # Arrange string = "a" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "a" end - + it "will reverse empty string " do # Arrange string = "" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "" end it "will reverse 'apple'" do # Arrange string = "apple" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "elppa" end end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "tac" end - + it "will reverse 'a'" do # Arrange string = "a" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "a" end - + it "will reverse empty string " do # Arrange string = "" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "" end it "will reverse 'apple'" do # Arrange string = "apple" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "elppa" end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 0 end - + it "returns 2 for 1 bunny" do # Arrange count = 1 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 2 end - + it "returns 100 for 50 bunnies" do # Arrange count = 50 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 100 end end -xdescribe "nested" do +Xdescribe "nested" do it "will return true for empystring" do # Arrange string = "" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal true end - + it "will return true for a nested series of parens" do # Arrange string = "((()))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal true end - + it "will return false for a nested series of parens" do # Arrange string = "(()))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal false end - + it "will return false for an even length improperly nested series of parens" do # Arrange string = "(())))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal false end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" array = [] - + # Act answer = search(array, item) - + # Assert expect(answer).must_equal false end - + it "will return true when looking for something in the array" do - # Arrange - item = "a" - array = ["b", "c", "a"] - - # Act - answer = search(array, item) - - # Assert - expect(answer).must_equal true + # Arrange + item = "a" + array = ["b", "c", "a"] + + # Act + answer = search(array, item) + + # 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"] + 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) + # Act + answer = search(array, item) - # Assert - expect(answer).must_equal true - end + # Assert + expect(answer).must_equal true + end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" - + # Act answer = is_palindrome(string) - + # Assert expect(answer).must_equal true end - + it "will return true for a palindrome" do # Arrange string = "racecar" - + # Act answer = is_palindrome(string) - + # Assert expect(answer).must_equal true end - + it "will return false for a nonpalindrome" do # Arrange string = "raecar" - + # Act answer = is_palindrome(string) - + # Assert expect(answer).must_equal false end @@ -300,59 +301,59 @@ # Arrange num1 = 1072503891 num2 = 62530841 - + # 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 # Arrange num1 = 0 num2 = 62530841 - + # 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 # Arrange num1 = 841 num2 = 62530841 - + # 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 num2 = 0 - + # 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 num2 = 20 - + # Act answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 1 + + # Assert + expect(answer).must_equal 1 end end From 39d8469ffc23a51796a52f51711f7b81f09816e2 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Nov 2019 19:39:13 -0800 Subject: [PATCH 2/4] added mores methods --- lib/recursive-methods.rb | 70 ++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..58978bd 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" + raise ArgumentError if n < 0 + return 1 if n == 0 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + rev_s = reverse(s[1..-1]) + rev_s += s[0] + rev_s end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def reverse_inplace(s, first = 0, last = s.length - 1) + return s if first >= last + temp = s[first] + s[first] = s[last] + s[last] = temp + + return reverse_inplace(s, first + 1, last - 1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return 0 if n <= 0 + return 2 if n == 1 + return 2 + bunny(n-1) end # Time complexity: ? # Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" -end +# def nested(s) +# raise NotImplementedError, "Method not implemented" +# end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" -end +# Time complexity: O(n) +# Space complexity: O(n) +def search(array, value, index = 0) + return false if index >= array.length + return true if array[index] == value + return search(array, value, index + 1) +end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.empty? + return false if s[0] != s[-1] + return is_palindrome(s[1...-1]) end # Time complexity: ? # Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file +# def digit_match(n, m) +# raise NotImplementedError, "Method not implemented" +# end From 1e06bd4808bd62621cb2e4bde4c34c999f092f3e Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Nov 2019 20:45:36 -0800 Subject: [PATCH 3/4] added more methods --- lib/recursive-methods.rb | 15 ++++++++++----- test/recursion_writing_test.rb | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 58978bd..c182d2b 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -36,11 +36,16 @@ def bunny(n) return 2 + bunny(n-1) end -# Time complexity: ? -# Space complexity: ? -# def nested(s) -# raise NotImplementedError, "Method not implemented" -# end +# Time complexity: O(n) +# Space complexity: O(n) +def nested(s, i = 0) + string = s.split("()") + return false if s.length.odd? + return true if s.empty? + + return true if s.length.even? && string[i].length == string[i + 1].length + return false if s.length.even? && string[i].length != string[i + 1].length +end # Time complexity: O(n) # Space complexity: O(n) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index af8235b..d4121ad 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -165,7 +165,7 @@ end end -Xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" From e553dcbedecc6a3049bb0bba99d0296516d46960 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Nov 2019 21:43:00 -0800 Subject: [PATCH 4/4] final adjustments --- lib/recursive-methods.rb | 28 +++++++++++++++------------- test/recursion_writing_test.rb | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index c182d2b..d03f06d 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -8,13 +8,13 @@ def factorial(n) return n * factorial(n-1) end -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(n^2) +# Space complexity: O(n^2) def reverse(s) return s if s.length <= 1 rev_s = reverse(s[1..-1]) rev_s += s[0] - rev_s + return rev_s end # Time complexity: O(n) @@ -43,8 +43,8 @@ def nested(s, i = 0) return false if s.length.odd? return true if s.empty? - return true if s.length.even? && string[i].length == string[i + 1].length - return false if s.length.even? && string[i].length != string[i + 1].length + return true if s.length.even? && string[i].length == string[i+1].length + return false if s.length.even? && string[i].length != string[i+1].length end # Time complexity: O(n) @@ -55,16 +55,18 @@ def search(array, value, index = 0) return search(array, value, index + 1) end -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(n^2) +# Space complexity: O(n^2) def is_palindrome(s) - return true if s.empty? + return true if s.length <= 1 return false if s[0] != s[-1] return is_palindrome(s[1...-1]) end -# Time complexity: ? -# Space complexity: ? -# def digit_match(n, m) -# raise NotImplementedError, "Method not implemented" -# end +# Time complexity: O(n) +# Space complexity: O(n) +def digit_match(n, m) + return 1 if n == 0 && m == 0 + return 0 if n == 0 || m == 0 + return 1 if n.to_s[-1] == m.to_s[-1] +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index d4121ad..1d8cb2b 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -296,7 +296,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891