From 884be440d1d181f480c2cca78e3d17eb6bd1722c Mon Sep 17 00:00:00 2001 From: Erika Date: Thu, 14 Nov 2019 09:06:00 -0800 Subject: [PATCH 1/7] Question 1 completed, not passing due to arguement error --- lib/recursive-methods.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..9704627 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -3,7 +3,10 @@ # Time complexity: ? # Space complexity: ? def factorial(n) - raise NotImplementedError, "Method not implemented" + if n == 0 || n == 1 + return 1 + end + return n * factorial(n-1) if n > 1 end # Time complexity: ? From 338368ac2cadb11d2f56364bf90d14882cf49e33 Mon Sep 17 00:00:00 2001 From: Erika Date: Sun, 17 Nov 2019 17:10:22 -0800 Subject: [PATCH 2/7] Completed reverse string (problem 2) --- lib/recursive-methods.rb | 13 +++++++++++-- test/recursion_writing_test.rb | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 9704627..4438e59 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -5,14 +5,23 @@ def factorial(n) if n == 0 || n == 1 return 1 + elsif n < 0 + raise ArgumentError end - return n * factorial(n-1) if n > 1 + return n * factorial(n-1) + # return times(n, factorial(n-1)) end + + # Time complexity: ? # Space complexity: ? def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length == 1 + return s + else + return reverse(s[1..-1]) + s[0] + end end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..c633dc3 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" From f25854d223af41e72d74af8e4e2870c7ad48a480 Mon Sep 17 00:00:00 2001 From: Erika Date: Sun, 17 Nov 2019 17:11:57 -0800 Subject: [PATCH 3/7] Added condition for empty string --- lib/recursive-methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 4438e59..2c8838e 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -17,7 +17,7 @@ def factorial(n) # Time complexity: ? # Space complexity: ? def reverse(s) - if s.length == 1 + if s.length == 1 || s == "" return s else return reverse(s[1..-1]) + s[0] From 7fd3658cef1514cd0202c327d4b45bfe20af475b Mon Sep 17 00:00:00 2001 From: Erika Date: Sun, 17 Nov 2019 17:13:49 -0800 Subject: [PATCH 4/7] Same method applied to reverse in place (because it reverses in place) --- lib/recursive-methods.rb | 6 +++++- test/recursion_writing_test.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 2c8838e..ff828c4 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -27,7 +27,11 @@ def reverse(s) # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length == 1 || s == "" + return s + else + return reverse(s[1..-1]) + s[0] + end end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index c633dc3..63e3dbc 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -84,7 +84,7 @@ end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" From 97f7c9c259468e0611ad1344023412efec568b47 Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Wed, 1 Jul 2020 11:33:43 -0700 Subject: [PATCH 5/7] factorial and reverse string (not in place) written --- lib/recursive-methods.rb | 106 ++++++++++++++++----------------- test/recursion_writing_test.rb | 2 + 2 files changed, 53 insertions(+), 55 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index ff828c4..21c886e 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,65 +1,61 @@ -# Authoring recursive algorithms. Add comments including time and space complexity for each method. - -# Time complexity: ? -# Space complexity: ? -def factorial(n) - if n == 0 || n == 1 - return 1 - elsif n < 0 - raise ArgumentError - end - return n * factorial(n-1) - # return times(n, factorial(n-1)) -end +# # Authoring recursive algorithms. Add comments including time and space complexity for each method. +# # Time complexity: O(n) +# # Space complexity: O(n) +# def factorial(n) +# raise ArgumentError unless n >= 0 +# return 1 if n == 1 || n == 0 -# Time complexity: ? -# Space complexity: ? -def reverse(s) - if s.length == 1 || s == "" - return s - else - return reverse(s[1..-1]) + s[0] - end -end +# n * factorial(n - 1) -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - if s.length == 1 || s == "" - return s - else - return reverse(s[1..-1]) + s[0] - end -end +# end -# Time complexity: ? -# Space complexity: ? -def bunny(n) - raise NotImplementedError, "Method not implemented" -end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" -end +# Time complexity: O(n) +# Space complexity: O(n) - I think O(n) because n amount of memory will be used to create the stack during the recursive call. I am creating a variable, but only concat. onto the existing one (<<) rather than creating a new variable each time (+=) so I think the memory allocation for the variable is O(1). -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" -end +def reverse(s) + + return s if s.length <= 1 + reversed = reverse(s[1..-1]) + reversed << s[0] -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - 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 + +# # 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" +# 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" +# end + +# # Time complexity: ? +# # Space complexity: ? +# def is_palindrome(s) +# 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 diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 63e3dbc..e023010 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 From 914d1245239645bdb88a81c9cb04b5b8e67a4566 Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Wed, 8 Jul 2020 16:02:06 -0700 Subject: [PATCH 6/7] Assignment re-done, completed optionals --- lib/recursive-methods.rb | 122 ++++++++++++++++++++++----------- test/recursion_writing_test.rb | 112 +++++++++++++++--------------- 2 files changed, 137 insertions(+), 97 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 21c886e..66d223d 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,20 +1,19 @@ -# # Authoring recursive algorithms. Add comments including time and space complexity for each method. +# # # # # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# # Time complexity: O(n) -# # Space complexity: O(n) -# def factorial(n) +# # # # # Time complexity: O(n) +# # # # # Space complexity: O(n) +def factorial(n) -# raise ArgumentError unless n >= 0 -# return 1 if n == 1 || n == 0 + raise ArgumentError unless n >= 0 + return 1 if n == 1 || n == 0 -# n * factorial(n - 1) - -# end + n * factorial(n - 1) +end -# Time complexity: O(n) -# Space complexity: O(n) - I think O(n) because n amount of memory will be used to create the stack during the recursive call. I am creating a variable, but only concat. onto the existing one (<<) rather than creating a new variable each time (+=) so I think the memory allocation for the variable is O(1). +# # # # Time complexity: O(n) +# # # # Space complexity: O(n) maybe less? def reverse(s) return s if s.length <= 1 @@ -24,38 +23,79 @@ def reverse(s) end -# # Time complexity: ? -# # Space complexity: ? -# def reverse_inplace(s) -# raise NotImplementedError, "Method not implemented" -# end +# # # # # Time complexity: O(n) +# # # # # Space complexity: O(1) +def reverse_inplace(s, pointer = 0) + + return s if pointer == s.length / 2 + s[pointer], s[- 1 - pointer] = s[- 1 - pointer ], s[pointer] + + reverse_inplace(s, pointer + 1) + +end + + +# # # # Time complexity: O(n) +# # # # Space complexity: O(n) +def bunny(n) -# # Time complexity: ? -# # Space complexity: ? -# def bunny(n) -# raise NotImplementedError, "Method not implemented" -# end + return 0 if n == 0 + return 2 if n == 1 + bunny(n - 1) + 2 -# # Time complexity: ? -# # Space complexity: ? -# def nested(s) -# raise NotImplementedError, "Method not implemented" -# end +end + + +# # # Time complexity: O(n) +# # # Space complexity: O(n) -# # Time complexity: ? -# # Space complexity: ? -# def search(array, value) -# raise NotImplementedError, "Method not implemented" -# end +def nested(s, i = 0, j = s.length - 1) -# # Time complexity: ? -# # Space complexity: ? -# def is_palindrome(s) -# raise NotImplementedError, "Method not implemented" -# end + return false if s.length.odd? + return false if s[i] == "(" && s[j] != ")" || (s[i] == ")" && i < j) + return true if i > j || s.empty? + + + nested(s, i + 1, j - 1) + +end -# # Time complexity: ? -# # Space complexity: ? -# def digit_match(n, m) -# raise NotImplementedError, "Method not implemented" -# end \ No newline at end of file + +# # # # Time complexity: O(n) +# # # # Space complexity: O(n) +def search(array, value, i = 0) + + return true if array[i] == value + return false if i == array.length + search(array, value, i + 1) + +end + + +# # # # # Time complexity: O(n) +# # # # # Space complexity: O(n) + +def is_palindrome(s, i = 0, j = -1) + + return false if s[i] != s[j] + return true if i > s.length - 1 + + is_palindrome(s, i + 1, j - 1) + +end + +# # # Time complexity: O(n) +# # # Space complexity: O(n) +def digit_match(n, m, matches = 0) + + dig1 = n % 10 + dig2 = m % 10 + + return matches if n == 0 || m == 0 + matches += 1 if dig1 == dig2 + + n = n / 10 + m = m / 10 + digit_match(n, m, matches) + +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index e023010..b61eb67 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -1,9 +1,9 @@ require 'minitest/autorun' -require 'minitest/reporters' -require "minitest/skip_dsl" +# require 'minitest/reporters' +# require "minitest/skip_dsl" require_relative '../lib/recursive-methods' -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "factorial" do it "will find the factorial of 0" do @@ -131,7 +131,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -166,7 +166,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -212,7 +212,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -262,7 +262,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" @@ -297,64 +297,64 @@ end end -xdescribe "digit_match" do - it "returns 4 for 1072503891 and 62530841" do - # Arrange - num1 = 1072503891 - num2 = 62530841 +# xdescribe "digit_match" do +# it "returns 4 for 1072503891 and 62530841" do +# # Arrange +# num1 = 1072503891 +# num2 = 62530841 - # Act - answer = digit_match(num1, num2) +# # Act +# answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 4 - end +# # Assert +# expect(answer).must_equal 4 +# end - it "returns 0 for nonmatching numbers" do - # Arrange - num1 = 0 - num2 = 62530841 +# it "returns 0 for nonmatching numbers" do +# # Arrange +# num1 = 0 +# num2 = 62530841 - # Act - answer = digit_match(num1, num2) +# # Act +# answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 0 - end +# # Assert +# expect(answer).must_equal 0 +# end - it "returns 3 for 841 and 62530841" do - # Arrange - num1 = 841 - num2 = 62530841 +# it "returns 3 for 841 and 62530841" do +# # Arrange +# num1 = 841 +# num2 = 62530841 - # Act - answer = digit_match(num1, num2) +# # Act +# answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 3 - end +# # 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 63a30416da01fa1b6b059ef0221307aff475655c Mon Sep 17 00:00:00 2001 From: Erika Maust Date: Wed, 8 Jul 2020 16:03:50 -0700 Subject: [PATCH 7/7] Corrected space complexity of reverse_inplace --- lib/recursive-methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 66d223d..503d61e 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -24,7 +24,7 @@ def reverse(s) # # # # # Time complexity: O(n) -# # # # # Space complexity: O(1) +# # # # # Space complexity: O(n) def reverse_inplace(s, pointer = 0) return s if pointer == s.length / 2