From 6150d996ad2496cda7e210e7b533835b77a22553 Mon Sep 17 00:00:00 2001 From: Mariya Burrows Date: Wed, 13 Nov 2019 20:57:27 -0800 Subject: [PATCH] added some recursion methods --- lib/recursive-methods.rb | 33 +++- test/recursion_writing_test.rb | 336 ++++++++++++++++----------------- 2 files changed, 192 insertions(+), 177 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..a3c28e6 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -3,47 +3,62 @@ # Time complexity: ? # Space complexity: ? def factorial(n) - raise NotImplementedError, "Method not implemented" + if n < 0 + raise ArgumentError + elsif (n == 1) || (n== 0) + return 1 + else + return n * factorial(n - 1) + end end # Time complexity: ? # Space complexity: ? -def reverse(s) - raise NotImplementedError, "Method not implemented" +def reverse(s) + if s.length == 0 + return s + end + a = s[0] + b = s.slice(1, s.length - 1) + return reverse(b) + a end # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return n + end + + return bunny(n - 1) + 2 end # Time complexity: ? # Space complexity: ? def nested(s) - 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" + raise NotImplementedError, "Method not implemented" end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..f8f781c 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -38,7 +38,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -84,52 +84,52 @@ 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 +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -210,149 +210,149 @@ end end -xdescribe "search" do - it "will return false for empty array" do - # Arrange - item = "a" - array = [] +# xdescribe "search" do +# it "will return false for empty array" do +# # Arrange +# item = "a" +# array = [] - # Act - answer = search(array, item) +# # 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