From b8147a0a027c468509d681eba1ae216fae030d3e Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 11 Nov 2019 23:00:59 -0800 Subject: [PATCH 1/2] all tests passing except digit_match - in progress --- lib/recursive-methods.rb | 82 +++++++++--- test/recursion_writing_test.rb | 222 +++++++++++++++++---------------- 2 files changed, 175 insertions(+), 129 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..5ddcc3a 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,93 @@ # 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 == 1 || n == 0 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n/2) >> O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length >= 2 + temp_char = s[0] + s[0] = s[-1] + s[-1] = temp_char + return s[0] + reverse(s[1...-1]) + s[-1] + else + return s + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n/2) >> O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length >= 2 + temp_char = s[0] + s[0] = s[-1] + s[-1] = temp_char + return s[0] + reverse(s[1...-1]) + s[-1] + else + return s + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def bunny(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError if n < 0 + return 0 if n == 0 + return 2 + bunny(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def nested(s) - raise NotImplementedError, "Method not implemented" + return true if s.length == 0 + return false if s.length % 2 != 0 + + if s[0] == '(' && s[-1] == ')' + nested(s[1..-2]) + else + return false + end end # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + return false if array[0] == nil + + return true if array[0] == value + + search(array[1..-1], value) end # Time complexity: ? # Space complexity: ? def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.length <=1 + + if s[0] == s[-1] + is_palindrome(s[1..-2]) + else + return false + end end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + return matches if m[] == nil + n = n.to_s.split("") + m = m.to_s.split("") + + if n[-1] == = + + + end + + p digit_match(1072503891,62530841) # 4 \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..2561c59 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -3,34 +3,36 @@ 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 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,321 +40,321 @@ 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 +describe "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 end -xdescribe "digit_match" do +describe "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 + + # 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 e6a456c5e0d34ecfc7438bea2bf1d46c399160c9 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Tue, 12 Nov 2019 16:08:57 -0800 Subject: [PATCH 2/2] Finished last problem --- .DS_Store | Bin 0 -> 6148 bytes lib/recursive-methods.rb | 38 ++++++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 O(n) +# Space complexity: O(n/2) => O(n) def is_palindrome(s) return true if s.length <=1 @@ -78,16 +78,26 @@ def is_palindrome(s) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) n is the length of the shortest array +# Space complexity: O(n) def digit_match(n, m) - return matches if m[] == nil - n = n.to_s.split("") - m = m.to_s.split("") + if n.class == Integer + n = n.to_s.split("") # [1, 0, 7, 2, 5, 0, 3, 8, 9, 1] + end - if n[-1] == = - - + if m.class == Integer + m = m.to_s.split("") # [6, 2, 5, 3 ,0, 8, 4, 1] end - p digit_match(1072503891,62530841) # 4 \ No newline at end of file + return n.length if n[-1] == nil || n[-1] == "match" + + return m.length if m[-1] == nil || m[-1] == "match" + + if n[-1] == m[-1] + n.insert(0, "match") + m.insert(0, "match") + end + + digit_match(n[0..-2], m[0..-2]) + +end