From c47a5d96a97485aae13ec01494d2b2756f9b1b78 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Sat, 9 Nov 2019 15:09:32 -0800 Subject: [PATCH 1/8] 4 down, 5 to go --- lib/recursive-methods.rb | 106 +++++++++++++--- test/recursion_writing_test.rb | 220 ++++++++++++++++----------------- 2 files changed, 200 insertions(+), 126 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..64be1af 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,123 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. +# https://github.com/stupendousC/recursion-writing -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def factorial(n) - raise NotImplementedError, "Method not implemented" + if (n == 1) || (n == 0) + return 1 + elsif n > 1 + return n * factorial(n-1) + else + raise ArgumentError, "argument must be >= 0" + end end # Time complexity: ? # Space complexity: ? def reverse(s) - raise NotImplementedError, "Method not implemented" + # accepts a string and returns the reverse of the string by reversing all letters and all words in the string + # ex: reverse("hello world") will convert the input string to "dlrow olleh" + + + # if s = "" + # return + # else + # return reverse(s[0..-1]) + # end + + + end # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. + raise NotImplementedError, "Method not implemented" end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + # N represents a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication). + if n == 0 + # base case: no bunnies left + return 0 + elsif n >= 1 + # recurse + return 2 + bunny(n-1) + else + # no negative bunnies + raise ArgumentError + end end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + + raise NotImplementedError, "Method not implemented" end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1), b/c only 1 active stack def search(array, value) - raise NotImplementedError, "Method not implemented" + # accepts an unsorted array of integers and an integer value to find and then + # returns true if the value if found in the unsorted array and false otherwise. + if array.empty? + return false + end + if array[0] == value + return true + else + array.shift + return search(array, value) + end + end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1), b/c only 1 active stack def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if (s.length == 0) || (s.length == 1) + return true + elsif s[0] == s[-1] + return is_palindrome(s[1...-1]) + else + return false + end end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + + if n[-1] && m[-1] + if n[-1] == m[-1] + if (n==0) || (m==0) + return 0 + else + return 1+ digit_match(n%10, m%10) if n[-2] && m[-2] + end + else + if (n==0) || (m==0) + return 0 + else + return digit_match(n%10, m%10) if n[-2] && m[-2] + end + end + else + return 0 + end +end + + +# Added Fun +def fib(n) + # returns the nth fibonacci number + # e.g. fib(4) = (0 1 1 2 3) should return 3 + # Try it with a large number (> 100), what do you notice happening? + + end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..574c923 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -7,77 +7,77 @@ 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) - }.must_raise ArgumentError - end + answer = factorial(num) + }.must_raise ArgumentError +end end xdescribe "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 @@ -88,77 +88,77 @@ 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 @@ -168,191 +168,191 @@ 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 d636060b8460281a450923ba7c9e2580219cfe2f Mon Sep 17 00:00:00 2001 From: stupendousC Date: Sat, 9 Nov 2019 15:14:55 -0800 Subject: [PATCH 2/8] 5 down,4 to go --- lib/recursive-methods.rb | 52 ++++++++++++++++------------------ test/recursion_writing_test.rb | 4 +-- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 64be1af..f8b2c57 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -13,20 +13,16 @@ def factorial(n) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse(s) # accepts a string and returns the reverse of the string by reversing all letters and all words in the string # ex: reverse("hello world") will convert the input string to "dlrow olleh" - - - # if s = "" - # return - # else - # return reverse(s[0..-1]) - # end - - + if s.length <= 1 + return s + else + return s[-1] + reverse(s[1...-1]) + s[0] + end end @@ -93,23 +89,23 @@ def is_palindrome(s) # Space complexity: ? def digit_match(n, m) - if n[-1] && m[-1] - if n[-1] == m[-1] - if (n==0) || (m==0) - return 0 - else - return 1+ digit_match(n%10, m%10) if n[-2] && m[-2] - end - else - if (n==0) || (m==0) - return 0 - else - return digit_match(n%10, m%10) if n[-2] && m[-2] - end - end - else - return 0 - end + # if n[-1] && m[-1] + # if n[-1] == m[-1] + # if (n==0) || (m==0) + # return 0 + # else + # return 1+ digit_match(n%10, m%10) if n[-2] && m[-2] + # end + # else + # if (n==0) || (m==0) + # return 0 + # else + # return digit_match(n%10, m%10) if n[-2] && m[-2] + # end + # end + # else + # return 0 + # end end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 574c923..5dca64b 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" @@ -295,7 +295,7 @@ end end -describe "digit_match" do +xdescribe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 From 5f5a0a11424e41ad19a940374c0ec9daf55e073c Mon Sep 17 00:00:00 2001 From: stupendousC Date: Sat, 9 Nov 2019 15:53:50 -0800 Subject: [PATCH 3/8] 3 to go --- lib/cw.rb | 45 +++++++++++++++++++++++++++++++ lib/recursive-methods.rb | 48 ++++++++++++++++++++++++++++++---- test/recursion_writing_test.rb | 2 +- 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 lib/cw.rb diff --git a/lib/cw.rb b/lib/cw.rb new file mode 100644 index 0000000..676e93f --- /dev/null +++ b/lib/cw.rb @@ -0,0 +1,45 @@ +def nested(s) + puts "\n TESTING ON #{s}" + + # read from front until ( is found, return false if ) instead + i = 0 + left_paren_found = false + while s[i] && left_paren_found==false + if s[i] == ")" + puts "HELL NO" + return false + elsif s[i] != "(" + i += 1 + else + puts "\tfound ( at index #{i}" + left_paren_found = true + end + end + + # read from back until ) is found, return false if ( instead + j = s.length-1 + right_paren_found = false + while s[j] && right_paren_found==false + if s[j] == "(" + puts "BUSTED" + return false + elsif s[j] != ")" + j -= 1 + else + puts "\tfound ) at index #{j}" + right_paren_found = true + end + end + + if (right_paren_found && left_paren_found) && (i < j) + puts "recursion..." + return nested(s[i+1...j]) + elsif !right_paren_found && !left_paren_found + return true + else + return false + end +end + +s = "ab(c)d" +p nested(s) \ No newline at end of file diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index f8b2c57..dc9a798 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -49,15 +49,53 @@ def bunny(n) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def nested(s) + # puts "\nTESTING ON #{s}" - raise NotImplementedError, "Method not implemented" + # read from front until ( is found, return false if ) instead + i = 0 + left_paren_found = false + while s[i] && left_paren_found==false + if s[i] == ")" + # puts "NOPE" + return false + elsif s[i] != "(" + i += 1 + else + # puts "\tfound ( at index #{i}" + left_paren_found = true + end + end + + # read from back until ) is found, return false if ( instead + j = s.length-1 + right_paren_found = false + while s[j] && right_paren_found==false + if s[j] == "(" + # puts "BUSTED" + return false + elsif s[j] != ")" + j -= 1 + else + # puts "\tfound ) at index #{j}" + right_paren_found = true + end + end + + if (right_paren_found && left_paren_found) && (i < j) + # puts "recursion..." + return nested(s[i+1...j]) + elsif !right_paren_found && !left_paren_found + return true + else + return false + end end # Time complexity: O(n) -# Space complexity: O(1), b/c only 1 active stack +# Space complexity: O(1) def search(array, value) # accepts an unsorted array of integers and an integer value to find and then # returns true if the value if found in the unsorted array and false otherwise. @@ -74,7 +112,7 @@ def search(array, value) end # Time complexity: O(n) -# Space complexity: O(1), b/c only 1 active stack +# Space complexity: O(1) def is_palindrome(s) if (s.length == 0) || (s.length == 1) return true diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 5dca64b..4527714 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -164,7 +164,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" From 49d53479df2ba5647e5134318d4a0ea4066c6e3e Mon Sep 17 00:00:00 2001 From: stupendousC Date: Sat, 9 Nov 2019 16:08:49 -0800 Subject: [PATCH 4/8] 2 to go --- lib/cw.rb | 59 ++++++++++++---------------------- lib/recursive-methods.rb | 47 ++++++++++++++++----------- test/recursion_writing_test.rb | 2 +- 3 files changed, 49 insertions(+), 59 deletions(-) diff --git a/lib/cw.rb b/lib/cw.rb index 676e93f..f75b522 100644 --- a/lib/cw.rb +++ b/lib/cw.rb @@ -1,45 +1,26 @@ -def nested(s) - puts "\n TESTING ON #{s}" - - # read from front until ( is found, return false if ) instead - i = 0 - left_paren_found = false - while s[i] && left_paren_found==false - if s[i] == ")" - puts "HELL NO" - return false - elsif s[i] != "(" - i += 1 - else - puts "\tfound ( at index #{i}" - left_paren_found = true - end - end +def digit_match(n, m) + # puts "digit_matching #{n} vs #{m}" + # puts "comparing #{n%10} vs #{m%10}" - # read from back until ) is found, return false if ( instead - j = s.length-1 - right_paren_found = false - while s[j] && right_paren_found==false - if s[j] == "(" - puts "BUSTED" - return false - elsif s[j] != ")" - j -= 1 - else - puts "\tfound ) at index #{j}" - right_paren_found = true + if n>9 && m>9 + # recurse-able condition + if n%10 == m%10 + return 1 + digit_match(n/10,m/10) + else + return 0 + digit_match(n/10, m/10) end - end - - if (right_paren_found && left_paren_found) && (i < j) - puts "recursion..." - return nested(s[i+1...j]) - elsif !right_paren_found && !left_paren_found - return true + else - return false + # no need to recurse, n and/or m already on single digits + if n%10 == m%10 + return 1 + else + return 0 + end end + end -s = "ab(c)d" -p nested(s) \ No newline at end of file +n = 54321 +m = 12345 +p digit_match(n,m) \ No newline at end of file diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index dc9a798..d94ddf8 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -31,6 +31,13 @@ def reverse(s) def reverse_inplace(s) # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. raise NotImplementedError, "Method not implemented" + + + + + + + end # Time complexity: O(n) @@ -123,27 +130,29 @@ def is_palindrome(s) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), where n is the longer number +# Space complexity: O(n) def digit_match(n, m) + # puts "digit_matching #{n} vs #{m}" + # puts "comparing #{n%10} vs #{m%10}" + + if n>9 && m>9 + # recurse-able condition + if n%10 == m%10 + return 1 + digit_match(n/10,m/10) + else + return 0 + digit_match(n/10, m/10) + end + + else + # no need to recurse, n and/or m already on single digits + if n%10 == m%10 + return 1 + else + return 0 + end + end - # if n[-1] && m[-1] - # if n[-1] == m[-1] - # if (n==0) || (m==0) - # return 0 - # else - # return 1+ digit_match(n%10, m%10) if n[-2] && m[-2] - # end - # else - # if (n==0) || (m==0) - # return 0 - # else - # return digit_match(n%10, m%10) if n[-2] && m[-2] - # end - # end - # else - # return 0 - # end end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 4527714..d3cac66 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -295,7 +295,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 From 1360368fc84477dc9f38b465966ddbb0f1f30c78 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Sat, 9 Nov 2019 16:30:52 -0800 Subject: [PATCH 5/8] 1 to go --- lib/cw.rb | 33 ++++++++++++--------------------- lib/recursive-methods.rb | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/cw.rb b/lib/cw.rb index f75b522..aba9617 100644 --- a/lib/cw.rb +++ b/lib/cw.rb @@ -1,26 +1,17 @@ -def digit_match(n, m) - # puts "digit_matching #{n} vs #{m}" - # puts "comparing #{n%10} vs #{m%10}" +def fib(n) + # returns the nth fibonacci number + # e.g. fib(4) = (0 1 1 2 3) should return 3 + # Try it with a large number (> 100), what do you notice happening? + # it got a lot slower as n increases, really noticeable around n=40 - if n>9 && m>9 - # recurse-able condition - if n%10 == m%10 - return 1 + digit_match(n/10,m/10) - else - return 0 + digit_match(n/10, m/10) - end - + if n == 0 + return 0 + elsif n == 1 + return 1 else - # no need to recurse, n and/or m already on single digits - if n%10 == m%10 - return 1 - else - return 0 - end + # generate fib seq up to the n-th index place + return fib(n-1) + fib(n-2) end - end -n = 54321 -m = 12345 -p digit_match(n,m) \ No newline at end of file + diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index d94ddf8..2834354 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -157,10 +157,29 @@ def digit_match(n, m) # Added Fun +# Time complexity: O(n*2^n), almost I think. +# It'll go n-1 levels deep, with up to 2^n leaves, so n*2^n... +# Space complexity: O(n*2^n)...? not sure def fib(n) # returns the nth fibonacci number # e.g. fib(4) = (0 1 1 2 3) should return 3 # Try it with a large number (> 100), what do you notice happening? + # it got a lot slower as n increases, really noticeable around n=40 - -end \ No newline at end of file + if n == 0 + return 0 + elsif n == 1 + return 1 + else + # generate fib seq up to the n-th index place + return fib(n-1) + fib(n-2) + end +end + +### to see fib(n) in action, uncomment chunk below +# n = 0 +# while n < 200 +# puts "\n\nn = #{n}" +# p fib(n) +# n+= 1 +# end \ No newline at end of file From 5d30daddd1bc32ed2dab3322c7a7532efba6ed10 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Mon, 11 Nov 2019 14:29:09 -0800 Subject: [PATCH 6/8] done, but not sure if I'm allowed to solve reverse_inplace(s) the way I did... --- lib/cw.rb | 35 ++++++++++++++++++++++------------ lib/recursive-methods.rb | 26 ++++++++++++++++++------- test/recursion_writing_test.rb | 2 +- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/lib/cw.rb b/lib/cw.rb index aba9617..a2d17b4 100644 --- a/lib/cw.rb +++ b/lib/cw.rb @@ -1,17 +1,28 @@ -def fib(n) - # returns the nth fibonacci number - # e.g. fib(4) = (0 1 1 2 3) should return 3 - # Try it with a large number (> 100), what do you notice happening? - # it got a lot slower as n increases, really noticeable around n=40 +def reverse_inplace(s) + # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. - if n == 0 - return 0 - elsif n == 1 - return 1 - else - # generate fib seq up to the n-th index place - return fib(n-1) + fib(n-2) + # helper method, which will be called as a recursive function + def swap(s, left_index, right_index) + temp = s[left_index] + s[left_index] = s[right_index] + s[right_index] = temp + return s end + + left_index = 0 + right_index = s.length - 1 + + while left_index < right_index + s = swap(s, left_index, right_index) + left_index += 1 + right_index -= 1 + end + + return s end + +s = "super awesome" +p reverse_inplace(s) + diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 2834354..e5b7135 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -26,18 +26,30 @@ def reverse(s) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def reverse_inplace(s) # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. - raise NotImplementedError, "Method not implemented" - - - - + ### I'M NOT SURE IF I'M ALLOWED TO SOLVE THE PROBLEM THIS WAY... + left_index = 0 + right_index = s.length - 1 + while left_index < right_index + s = swap(s, left_index, right_index) + left_index += 1 + right_index -= 1 + end + return s +end + +# helper method, which will be called as a recursive function +def swap(s, left_index, right_index) + temp = s[left_index] + s[left_index] = s[right_index] + s[right_index] = temp + return s end # Time complexity: O(n) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index d3cac66..ee7e97c 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 e3f53afd58f2c70f6cad7f09533e635e62dc4554 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Mon, 11 Nov 2019 23:10:00 -0800 Subject: [PATCH 7/8] fixed reverse_inplace() --- lib/recursive-methods.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index e5b7135..d4f6072 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -26,32 +26,33 @@ def reverse(s) end -# Time complexity: O(n) -# Space complexity: O(1) +# Time complexity: O(???) +# Space complexity: O(???) def reverse_inplace(s) # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. - - ### I'M NOT SURE IF I'M ALLOWED TO SOLVE THE PROBLEM THIS WAY... left_index = 0 right_index = s.length - 1 - while left_index < right_index - s = swap(s, left_index, right_index) - left_index += 1 - right_index -= 1 - end + swap(s, left_index, right_index) return s end -# helper method, which will be called as a recursive function +# recursive fcn to be used inside reverse_inplace() def swap(s, left_index, right_index) - temp = s[left_index] - s[left_index] = s[right_index] - s[right_index] = temp - return s + if left_index >= right_index + return + else + temp = s[left_index] + s[left_index] = s[right_index] + s[right_index] = temp + left_index += 1 + right_index -= 1 + swap(s, left_index, right_index) + end end + # Time complexity: O(n) # Space complexity: O(n) def bunny(n) From 0977223b849279cbb7c7f7fff9115aad149af475 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 14 Nov 2019 20:21:15 -0800 Subject: [PATCH 8/8] made fixes, corrected big O notations, also added some more diff versions --- lib/recursive-methods.rb | 92 +++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index d4f6072..e4148ce 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -13,8 +13,8 @@ def factorial(n) end end -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(n^2) b/c line 24 made a new string O(n) * recursing O(n) times +# Space complexity: O(n^2) b/c line 24 made a new string O(n) * recursing O(n) times def reverse(s) # accepts a string and returns the reverse of the string by reversing all letters and all words in the string # ex: reverse("hello world") will convert the input string to "dlrow olleh" @@ -26,8 +26,8 @@ def reverse(s) end -# Time complexity: O(???) -# Space complexity: O(???) +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) # accepts a string as a parameter and then reverses the string IN PLACE using a recursive algorithm. left_index = 0 @@ -69,8 +69,8 @@ def bunny(n) end end -# Time complexity: O(n) -# Space complexity: O(1) +# Time complexity: O(n^2) b/c line 109 is O(n) for making new array, x O(n) recursive steps +# Space complexity: O(n^2)? def nested(s) # puts "\nTESTING ON #{s}" @@ -114,25 +114,52 @@ def nested(s) end end +### Chris says... +# This is only tangentially recursive, you have a lot of nested loop sand if-else blocks. + +def nested_Chris_inefficient(s) + return true if s.empty? + return false unless s[0] == "(" && s[-1] == ")" + return nested_Chris_inefficient(s[1..-2]) ### This makes it O(n)*O(n) +end + +def nested_Chris(s, i=0, j=s.length-1) + # O(n) time, O(n) space + # puts "looking at #{s[i..j]}, i=#{i} and j=#{j}" + return false if s.length % 2 == 1 + + while i < j + return false unless s[i] == "(" && s[j] == ")" + return nested_Chris(s, i+1, j-1) + end + + return true +end + +# s = "(())" +# p nested_Chris_inefficient(s) +# p nested_Chris(s) + # Time complexity: O(n) -# Space complexity: O(1) -def search(array, value) +# Space complexity: O(n) +def search(array, value, i=0) # accepts an unsorted array of integers and an integer value to find and then # returns true if the value if found in the unsorted array and false otherwise. if array.empty? return false end - if array[0] == value + if array[i] == value return true + elsif array[i+1] + return search(array, value, i+1) else - array.shift - return search(array, value) + return false end end -# Time complexity: O(n) -# Space complexity: O(1) +# Time complexity: O(n^2) b/c O(n) for making new string * O(n) recursive steps +# Space complexity: O(n^2) def is_palindrome(s) if (s.length == 0) || (s.length == 1) return true @@ -143,6 +170,20 @@ def is_palindrome(s) end end +def is_palindrome_better(s, i=0, j=s.length-1) + # O(n) time & space + while i < j + if s[i] == s[j] + return is_palindrome_better(s, i+1, j-1) + else + return false + end + end + + return true +end + + # Time complexity: O(n), where n is the longer number # Space complexity: O(n) def digit_match(n, m) @@ -164,15 +205,34 @@ def digit_match(n, m) else return 0 end + end +end + +### can also treat n and m as strings and iter/compare from index -1 +def digit_match_str(n, m, i = -1) + s1 = n.to_s + s2 = m.to_s + + if (!s1[i]) || (!s2[i]) + return 0 + else + if s1[i] == s2[i] + return 1 + digit_match_str(s1,s2,i-1) + else + return 0 + digit_match_str(s1,s2,i-1) + end end end +# n= 123451 +# m = 65431 +# p digit_match_str(n,m) + # Added Fun -# Time complexity: O(n*2^n), almost I think. -# It'll go n-1 levels deep, with up to 2^n leaves, so n*2^n... -# Space complexity: O(n*2^n)...? not sure +# Time complexity: O(2^n), 2^n leaves +# Space complexity: O(n) per Chris def fib(n) # returns the nth fibonacci number # e.g. fib(4) = (0 1 1 2 3) should return 3