From 4abb2815aced0547c896152fb6c2aea3c433e6ef Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 11 Nov 2019 20:17:33 -0800 Subject: [PATCH 1/4] latest commit --- Rakefile | 1 + lib/recursive-methods.rb | 46 +++- test/recursion_writing_test.rb | 458 +++++++++++++++++---------------- 3 files changed, 268 insertions(+), 237 deletions(-) diff --git a/Rakefile b/Rakefile index 0c2d13f..21a8bd9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require 'rake/testtask' + Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..686fa9f 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,33 +1,61 @@ # 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, 'Must provide a number greater than 1' unless n >= 0 + + if n == 1 || n == 0 + return 1 + else + return n * factorial(n - 1) + end + 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 + + last = s[-1] + + rest = s[0...-1] + + return last + reverse(rest) end +# i = 0 + +# mid = s.length/2.to_i +# while i < mid +# a = s[i] +# s[i] = s[s.length - 1 - i] +# s[s.length - 1 - i] = a +# i += 1 +# end + # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + end # Time complexity: ? # Space complexity: ? def bunny(n) - raise NotImplementedError, "Method not implemented" + if n <= 1 + return n* 2 + else + return 2 + bunny(n - 1) + end end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..5162319 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -3,6 +3,8 @@ require "minitest/skip_dsl" require_relative '../lib/recursive-methods' +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + describe "factorial" do it "will find the factorial of 0" do # Arrange @@ -38,7 +40,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -84,275 +86,275 @@ end -xdescribe "reverse_in_place" do - it "will reverse 'cat'" do - # Arrange - string = "cat" +# xdescribe "reverse_in_place" do +# it "will reverse 'cat'" do +# # Arrange +# string = "cat" - # Act - answer = reverse_inplace(string) +# # Act +# answer = reverse_inplace(string) - # Assert - expect(answer).must_equal "tac" - end +# # Assert +# expect(answer).must_equal "tac" +# end - it "will reverse 'a'" do - # Arrange - string = "a" +# it "will reverse 'a'" do +# # Arrange +# string = "a" - # Act - answer = reverse_inplace(string) +# # Act +# answer = reverse_inplace(string) - # Assert - expect(answer).must_equal "a" - end +# # Assert +# expect(answer).must_equal "a" +# end - it "will reverse empty string " do - # Arrange - string = "" +# it "will reverse empty string " do +# # Arrange +# string = "" - # Act - answer = reverse_inplace(string) +# # Act +# answer = reverse_inplace(string) - # Assert - expect(answer).must_equal "" - end - it "will reverse 'apple'" do - # Arrange - string = "apple" +# # Assert +# expect(answer).must_equal "" +# end +# it "will reverse 'apple'" do +# # Arrange +# string = "apple" - # Act - answer = reverse_inplace(string) +# # Act +# answer = reverse_inplace(string) - # Assert - expect(answer).must_equal "elppa" - end -end +# # Assert +# expect(answer).must_equal "elppa" +# end +# end -xdescribe "bunny" do - it "returns 0 for 0 bunnies" do - # Arrange - count = 0 +# xdescribe "bunny" do +# it "returns 0 for 0 bunnies" do +# # Arrange +# count = 0 - # Act - answer = bunny(count) +# # Act +# answer = bunny(count) - # Assert - expect(answer).must_equal 0 - end +# # Assert +# expect(answer).must_equal 0 +# end - it "returns 2 for 1 bunny" do - # Arrange - count = 1 +# it "returns 2 for 1 bunny" do +# # Arrange +# count = 1 - # Act - answer = bunny(count) +# # Act +# answer = bunny(count) - # Assert - expect(answer).must_equal 2 - end +# # Assert +# expect(answer).must_equal 2 +# end - it "returns 100 for 50 bunnies" do - # Arrange - count = 50 +# it "returns 100 for 50 bunnies" do +# # Arrange +# count = 50 - # Act - answer = bunny(count) +# # Act +# answer = bunny(count) - # Assert - expect(answer).must_equal 100 - end -end - -xdescribe "nested" do - it "will return true for empystring" do - # Arrange - string = "" +# # Assert +# expect(answer).must_equal 100 +# end +# end - # Act - answer = nested(string) +# xdescribe "nested" do +# it "will return true for empystring" do +# # Arrange +# string = "" - # Assert - expect(answer).must_equal true - end +# # Act +# answer = nested(string) - it "will return true for a nested series of parens" do - # Arrange - string = "((()))" +# # Assert +# expect(answer).must_equal true +# end - # Act - answer = nested(string) +# it "will return true for a nested series of parens" do +# # Arrange +# string = "((()))" - # Assert - expect(answer).must_equal true - end +# # Act +# answer = nested(string) - it "will return false for a nested series of parens" do - # Arrange - string = "(()))" +# # Assert +# expect(answer).must_equal true +# end - # Act - answer = nested(string) +# it "will return false for a nested series of parens" do +# # Arrange +# string = "(()))" - # Assert - expect(answer).must_equal false - end +# # Act +# answer = nested(string) - it "will return false for an even length improperly nested series of parens" do - # Arrange - string = "(())))" +# # Assert +# expect(answer).must_equal false +# end - # Act - answer = nested(string) +# it "will return false for an even length improperly nested series of parens" do +# # Arrange +# string = "(())))" - # Assert - expect(answer).must_equal false - end -end +# # Act +# answer = nested(string) -xdescribe "search" do - it "will return false for empty array" do - # Arrange - item = "a" - array = [] +# # Assert +# expect(answer).must_equal false +# end +# end - # Act - answer = search(array, item) +# xdescribe "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 +# # 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"] +# it "will return true when looking for something in the array" do +# # Arrange +# item = "a" +# 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 - it "will return false when looking for something not in the array" do - # Arrange - item = "x" - array = ["b", "c", "a"] +# 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) +# # 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"] +# # 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) +# # Act +# answer = search(array, item) - # Assert - expect(answer).must_equal true - end -end - -xdescribe "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 -end - -xdescribe "digit_match" do - it "returns 4 for 1072503891 and 62530841" do - # Arrange - num1 = 1072503891 - num2 = 62530841 - - # Act - answer = digit_match(num1, num2) - - # 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 - 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 - end +# # Assert +# expect(answer).must_equal true +# end +# end + +# xdescribe "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 +# end + +# xdescribe "digit_match" do +# it "returns 4 for 1072503891 and 62530841" do +# # Arrange +# num1 = 1072503891 +# num2 = 62530841 + +# # Act +# answer = digit_match(num1, num2) + +# # 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 +# 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 +# end - it "returns 1 for (0, 0)" do - # Arrange - num1 = 0 - num2 = 0 +# it "returns 1 for (0, 0)" do +# # Arrange +# num1 = 0 +# num2 = 0 - # Act - answer = digit_match(num1, num2) +# # Act +# answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 1 - end +# # 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 - end -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 +# end +# end From 5495f5672f1a46e56019ba26443a2ccebdc48859 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 11 Nov 2019 20:51:51 -0800 Subject: [PATCH 2/4] reverse in place --- lib/recursive-methods.rb | 35 +++++++++-------- test/recursion_writing_test.rb | 68 +++++++++++++++++----------------- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 686fa9f..fc3ced5 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,5 +1,5 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. - +require "pry" # Time complexity: O(n) # Space complexity: O(n) def factorial(n) @@ -26,27 +26,32 @@ def reverse(s) return last + reverse(rest) end -# i = 0 +# Time complexity: O(n) +# Space complexity: O(n) +def reverse_inplace(string) + return _reverse(string, 0, string.length - 1) +end -# mid = s.length/2.to_i -# while i < mid -# a = s[i] -# s[i] = s[s.length - 1 - i] -# s[s.length - 1 - i] = a -# i += 1 -# end +def _reverse(string, i, j) + if j - i <= 0 + return string + else + swap(string, i, j) + return _reverse(string, i += 1, j -=1) + end +end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - -end +def swap(string, i, j) + a = string[i] + string[i] = string[j] + string[j] = a +end # Time complexity: ? # Space complexity: ? def bunny(n) if n <= 1 - return n* 2 + return n * 2 else return 2 + bunny(n - 1) end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 5162319..9a68888 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -86,50 +86,50 @@ end -# xdescribe "reverse_in_place" do -# it "will reverse 'cat'" do -# # Arrange -# string = "cat" +describe "reverse_in_place" do + it "will reverse 'cat'" do + # Arrange + string = "cat" -# # Act -# answer = reverse_inplace(string) + # Act + answer = reverse_inplace(string) -# # Assert -# expect(answer).must_equal "tac" -# end + # Assert + expect(answer).must_equal "tac" + end -# it "will reverse 'a'" do -# # Arrange -# string = "a" + it "will reverse 'a'" do + # Arrange + string = "a" -# # Act -# answer = reverse_inplace(string) + # Act + answer = reverse_inplace(string) -# # Assert -# expect(answer).must_equal "a" -# end + # Assert + expect(answer).must_equal "a" + end -# it "will reverse empty string " do -# # Arrange -# string = "" + it "will reverse empty string " do + # Arrange + string = "" -# # Act -# answer = reverse_inplace(string) + # Act + answer = reverse_inplace(string) -# # Assert -# expect(answer).must_equal "" -# end -# it "will reverse 'apple'" do -# # Arrange -# string = "apple" + # Assert + expect(answer).must_equal "" + end + it "will reverse 'apple'" do + # Arrange + string = "apple" -# # Act -# answer = reverse_inplace(string) + # Act + answer = reverse_inplace(string) -# # Assert -# expect(answer).must_equal "elppa" -# end -# end + # Assert + expect(answer).must_equal "elppa" + end +end # xdescribe "bunny" do # it "returns 0 for 0 bunnies" do From 2eb90fce3d5e156e803af0497f742b35800b5007 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 11 Nov 2019 21:15:27 -0800 Subject: [PATCH 3/4] bunny ears --- lib/recursive-methods.rb | 8 ++- test/recursion_writing_test.rb | 120 ++++++++++++++++----------------- 2 files changed, 67 insertions(+), 61 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fc3ced5..c03a3c7 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -59,10 +59,16 @@ def bunny(n) # Time complexity: ? # Space complexity: ? -def nested(s) + +def nested(string) + end +def open_parenthesis?(string, i, j) + +end + # Time complexity: ? # Space complexity: ? def search(array, value) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 9a68888..f6c7514 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -131,86 +131,86 @@ end end -# xdescribe "bunny" do -# it "returns 0 for 0 bunnies" do -# # Arrange -# count = 0 +describe "bunny" do + it "returns 0 for 0 bunnies" do + # Arrange + count = 0 -# # Act -# answer = bunny(count) + # Act + answer = bunny(count) -# # Assert -# expect(answer).must_equal 0 -# end + # Assert + expect(answer).must_equal 0 + end -# it "returns 2 for 1 bunny" do -# # Arrange -# count = 1 + it "returns 2 for 1 bunny" do + # Arrange + count = 1 -# # Act -# answer = bunny(count) + # Act + answer = bunny(count) -# # Assert -# expect(answer).must_equal 2 -# end + # Assert + expect(answer).must_equal 2 + end -# it "returns 100 for 50 bunnies" do -# # Arrange -# count = 50 + it "returns 100 for 50 bunnies" do + # Arrange + count = 50 -# # Act -# answer = bunny(count) + # Act + answer = bunny(count) -# # Assert -# expect(answer).must_equal 100 -# end -# end + # Assert + expect(answer).must_equal 100 + end +end -# xdescribe "nested" do -# it "will return true for empystring" do -# # Arrange -# string = "" +describe "nested" do + it "will return true for empystring" do + # Arrange + string = "" -# # Act -# answer = nested(string) + # Act + answer = nested(string) -# # Assert -# expect(answer).must_equal true -# end + # Assert + expect(answer).must_equal true + end -# it "will return true for a nested series of parens" do -# # Arrange -# string = "((()))" + it "will return true for a nested series of parens" do + # Arrange + string = "((()))" -# # Act -# answer = nested(string) + # Act + answer = nested(string) -# # Assert -# expect(answer).must_equal true -# end + # Assert + expect(answer).must_equal true + end -# it "will return false for a nested series of parens" do -# # Arrange -# string = "(()))" + it "will return false for a nested series of parens" do + # Arrange + string = "(()))" -# # Act -# answer = nested(string) + # Act + answer = nested(string) -# # Assert -# expect(answer).must_equal false -# end + # Assert + expect(answer).must_equal false + end -# it "will return false for an even length improperly nested series of parens" do -# # Arrange -# string = "(())))" + it "will return false for an even length improperly nested series of parens" do + # Arrange + string = "(())))" -# # Act -# answer = nested(string) + # Act + answer = nested(string) -# # Assert -# expect(answer).must_equal false -# end -# end + # Assert + expect(answer).must_equal false + end +end # xdescribe "search" do # it "will return false for empty array" do From ffff36b12189c6c7f06b9e6df2e591cd0e915b81 Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 13 Nov 2019 20:10:07 -0800 Subject: [PATCH 4/4] number 7 --- lib/recursive-methods.rb | 63 ++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index c03a3c7..0bd8e98 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -47,40 +47,73 @@ def swap(string, i, j) string[j] = a end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - if n <= 1 - return n * 2 + if n == 0 + return 0 + elsif n == 1 + return 2 else - return 2 + bunny(n - 1) + 2 + bunny(n - 1) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def nested(string) - - + if string.length % 2 != 0 + return false + else + recursion(string, 0, string.length - 1) + end end -def open_parenthesis?(string, i, j) - +def recursion(string, start_index, end_index) + if start_index > end_index + return true + elsif string[start_index] == "(" && string[end_index] == ")" + return recursion(string, start_index + 1, end_index - 1) + else + return false + end end # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + if array == [] + return false + else + return search_helper(array, value, i) + end end -# Time complexity: ? -# Space complexity: ? +def search_helper(array, value, i) + if array[i] == value + return true + else + recursion(array, value, i += 1) + end +end + +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return is_palindrome(s, 0, s.length - 1 ) end +def is_palindrome(s, start_index, end_index) + if start_index < end_index + return true + elsif s[0] != s[s.length - 1] + return false + else + is_palindrome(s, start_index + 1, end_index - 1) + end +end + # Time complexity: ? # Space complexity: ? def digit_match(n, m)