From 833c3642aa221bdbe52133aa4af66b9d5f65ed19 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Sun, 17 Nov 2019 18:24:08 -0800 Subject: [PATCH 1/3] fibonacci with recursive --- lib/fibonacci.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..54d5b3c 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -3,6 +3,51 @@ # Time Complexity - ? # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method -def fibonacci(n) - -end + +# # # # brute force +# def fibonacci(target) +# if target < 0 +# raise ArgumentError +# elsif target == 0 +# return 0 +# elsif target == 1 +# return 1 +# end + +# ultimate = 1 +# pentultimate = 0 +# current = 1 +# n = 2 + +# until n == target +# pentultimate = ultimate + +# ultimate = current +# current = pentultimate + ultimate +# n += 1 +# end + +# return current + +# end + +def fibonacci(target, ultimate = 1, pentultimate = 0, n = 2) + if target < 0 + raise ArgumentError + elsif target == 0 + return 0 + elsif target == 1 + return 1 + end + + if n == target + current = pentultimate + ultimate + return current + else + n += 1 + holder = pentultimate + ultimate + pentultimate = ultimate + ultimate = holder + return fibonacci(target, ultimate = ultimate, pentultimate = pentultimate, n = n) + end +end \ No newline at end of file From 850cfa405d3168bb7720f33c529adfd19e6b6ea9 Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Sun, 17 Nov 2019 19:15:24 -0800 Subject: [PATCH 2/3] super digit with recursion --- lib/fibonacci.rb | 48 ++++++++++----------- lib/super_digit.rb | 29 +++++++++---- test/fibonacci_test.rb | 90 ++++++++++++++++++++-------------------- test/super_digit_test.rb | 40 +++++++++--------- 4 files changed, 111 insertions(+), 96 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 54d5b3c..8e65232 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,9 +1,30 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) +# Space Complexity - O(n) (should be O(n)) # Hint, you may want a recursive helper method +def fibonacci(target, ultimate = 1, pentultimate = 0, n = 2) + if target < 0 + raise ArgumentError + elsif target == 0 + return 0 + elsif target == 1 + return 1 + end + + if n == target + current = pentultimate + ultimate + return current + else + n += 1 + holder = pentultimate + ultimate + pentultimate = ultimate + ultimate = holder + return fibonacci(target, ultimate = ultimate, pentultimate = pentultimate, n = n) + end +end + # # # # brute force # def fibonacci(target) # if target < 0 @@ -29,25 +50,4 @@ # return current -# end - -def fibonacci(target, ultimate = 1, pentultimate = 0, n = 2) - if target < 0 - raise ArgumentError - elsif target == 0 - return 0 - elsif target == 1 - return 1 - end - - if n == target - current = pentultimate + ultimate - return current - else - n += 1 - holder = pentultimate + ultimate - pentultimate = ultimate - ultimate = holder - return fibonacci(target, ultimate = ultimate, pentultimate = pentultimate, n = n) - end -end \ No newline at end of file +# end \ No newline at end of file diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..fa63a80 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,30 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? -def super_digit(n) - +# Time Complexity - O(n) +# Space Complexity - O(log n) + +def super_digit(input) + # # # # brute recursion + if input < 10 + return input + elsif input >= 10 + sum = 0 + i = 0 + limit = input.to_s.length + dig_input = input.to_s + until i == limit + sum += dig_input[i].to_i + i += 1 + end + return super_digit(sum) + end end - + # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) - + end - \ No newline at end of file + + diff --git a/test/fibonacci_test.rb b/test/fibonacci_test.rb index 639f3b1..a4e07d7 100644 --- a/test/fibonacci_test.rb +++ b/test/fibonacci_test.rb @@ -3,54 +3,54 @@ describe "Fibonacci" do it "will raise an error for n < 0" do expect { - # Act - fibonacci(-1) - # Assert - }.must_raise ArgumentError - end - - it "will return 0 for fib(0)" do - # Act - answer = fibonacci(0) - - # Assert - expect(answer).must_equal 0 - end - - it "will return 1 for fib(1)" do # Act - answer = fibonacci(1) - + fibonacci(-1) # Assert - expect(answer).must_equal 1 - end - - it "will return 1 for fib(2)" do - # Act - answer = fibonacci(2) - - # Assert - expect(answer).must_equal 1 - end - it "will return 3 for fib(4)" do - # Act - answer = fibonacci(4) + }.must_raise ArgumentError +end - # Assert - expect(answer).must_equal 3 - end - it "will return 5 for fib(5)" do - # Act - answer = fibonacci(4) +it "will return 0 for fib(0)" do + # Act + answer = fibonacci(0) + + # Assert + expect(answer).must_equal 0 +end - # Assert - expect(answer).must_equal 3 - end - it "will return 55 for fib(10)" do - # Act - answer = fibonacci(10) +it "will return 1 for fib(1)" do + # Act + answer = fibonacci(1) + + # Assert + expect(answer).must_equal 1 +end - # Assert - expect(answer).must_equal 55 - end +it "will return 1 for fib(2)" do + # Act + answer = fibonacci(2) + + # Assert + expect(answer).must_equal 1 +end +it "will return 3 for fib(4)" do + # Act + answer = fibonacci(4) + + # Assert + expect(answer).must_equal 3 +end +it "will return 5 for fib(5)" do + # Act + answer = fibonacci(4) + + # Assert + expect(answer).must_equal 3 +end +it "will return 55 for fib(10)" do + # Act + answer = fibonacci(10) + + # Assert + expect(answer).must_equal 55 +end end diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..c92996b 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -1,61 +1,61 @@ require_relative "test_helper" -xdescribe "super_digit" do +describe "super_digit" do it "will return 2 for super_digit(9875)" do # Act answer = super_digit(9875) - + # Assert expect(answer).must_equal 2 end - + it "will return 5 for super_digit(5)" do # Act answer = super_digit(5) - + # Assert expect(answer).must_equal 5 - end - + end + it "will return 6 for super_digit(123)" do # Act answer = super_digit(123) - + # Assert expect(answer).must_equal 6 end - + it "will return 6 for super_digit(12327)" do # Act answer = super_digit(12327) - + # Assert expect(answer).must_equal 6 end - - describe "refined superdigit" do + + xdescribe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1) - + # Assert expect(answer).must_equal 1 end - + it "will return 8 for n=9875 and k = 4" do # Act answer = refined_super_digit(9875, 4) - + # Assert expect(answer).must_equal 8 end - + it "will return 3 for n=148 and k = 3" do - # Act - answer = refined_super_digit(148, 3) - - # Assert - expect(answer).must_equal 3 + # Act + answer = refined_super_digit(148, 3) + + # Assert + expect(answer).must_equal 3 end end end From a1810f13914c8cc388d90a8a7b10f4626777947c Mon Sep 17 00:00:00 2001 From: C Gutierrez Date: Sun, 17 Nov 2019 19:35:12 -0800 Subject: [PATCH 3/3] refined_super_digit --- README.md | 1 + lib/super_digit.rb | 14 ++++++++++---- test/super_digit_test.rb | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f5ae1ef..2649e7c 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,4 @@ You can use your superdigit solution in the solution for refined_superdigit. Ca ## Problem Source - [HackerRank Super Digit](https://www.hackerrank.com/challenges/super-digit/problem) + \ No newline at end of file diff --git a/lib/super_digit.rb b/lib/super_digit.rb index fa63a80..fcff7d0 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -20,11 +20,17 @@ def super_digit(input) end end +# Time Complexity - O(n) +# Space Complexity - O(m log n) -# Time Complexity - ? -# Space Complexity - ? -def refined_super_digit(n, k) - +# This method uses the super_digit method as a helper as I'm too lazy to write it again. +def refined_super_digit(number, repeater) + assembled_number = "" + dig_number = number.to_s + repeater.times do + assembled_number = assembled_number + dig_number + end + return super_digit(super_digit(number) * repeater) end diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index c92996b..ac68984 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -33,7 +33,7 @@ expect(answer).must_equal 6 end - xdescribe "refined superdigit" do + describe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1)