From e1bde037a5470d43c36590f116871b490fb0f899 Mon Sep 17 00:00:00 2001 From: Malik Ahmed Date: Tue, 30 Oct 2018 00:12:39 -0400 Subject: [PATCH 1/4] Add fibonacci sequence one-liners in Ruby (recursive and iterative examples). --- CONTRIBUTORS.md | 4 ++++ ruby/.ruby-version | 1 + ruby/fibonacci.rb | 5 +++++ ruby/fibonacci_iterative.rb | 7 +++++++ 4 files changed, 17 insertions(+) create mode 100644 ruby/.ruby-version create mode 100644 ruby/fibonacci.rb create mode 100644 ruby/fibonacci_iterative.rb diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 96fc5c2b..283d5749 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -233,3 +233,7 @@ Name: Ankur Sonawane
Place: Varanasi, India Coding Experience:Python, C, CPP Email: sankur.shrikant.eee16@itbhu.ac.in + +Name: [Malik Ahmed](https://github.com/animanmaster)
+Place: New Jersey +Coding Experience: Ruby, Java, Python, C, C++, C#, Javascript diff --git a/ruby/.ruby-version b/ruby/.ruby-version new file mode 100644 index 00000000..73462a5a --- /dev/null +++ b/ruby/.ruby-version @@ -0,0 +1 @@ +2.5.1 diff --git a/ruby/fibonacci.rb b/ruby/fibonacci.rb new file mode 100644 index 00000000..8a44b98a --- /dev/null +++ b/ruby/fibonacci.rb @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +fibonacci = ->(n) { n < 2 ? n : fibonacci.(n - 1) + fibonacci.(n - 2) } + +# Test this by running `ruby fibonacci.rb n` where n is the index in the fibonacci sequence to calculate. +puts fibonacci.call(ARGV[0].to_i) diff --git a/ruby/fibonacci_iterative.rb b/ruby/fibonacci_iterative.rb new file mode 100644 index 00000000..64b192ea --- /dev/null +++ b/ruby/fibonacci_iterative.rb @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +# Calculate the fibonacci sequence iteratively by passing along fib(n-2) and fib(n-1) through each iteration of the loop. +# The resulting fibonacci number is the sum of the last iteration of the loop. +fibonacci = ->(n) { n < 2 ? n : 1.upto(n - 2).inject([0, 1]) { |last_two, i| fib = last_two.sum and [last_two[1], fib] }.sum } + +# Test this by running `ruby fibonacci_iterative.rb n` where n is the index in the fibonacci sequence to calculate. +puts fibonacci.call(ARGV[0].to_i) From 265dddc47f5c8944fc3f33672e8461c51eafe1d2 Mon Sep 17 00:00:00 2001 From: William Zhou Date: Tue, 30 Oct 2018 13:44:16 -0400 Subject: [PATCH 2/4] Delete .ruby-version --- ruby/.ruby-version | 1 - 1 file changed, 1 deletion(-) delete mode 100644 ruby/.ruby-version diff --git a/ruby/.ruby-version b/ruby/.ruby-version deleted file mode 100644 index 73462a5a..00000000 --- a/ruby/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.5.1 From f23e28cee3efc80a616aedacf010659529d02d04 Mon Sep 17 00:00:00 2001 From: William Zhou Date: Tue, 30 Oct 2018 13:44:39 -0400 Subject: [PATCH 3/4] Update fibonacci.rb Add # Ruby version 2.5.1 --- ruby/fibonacci.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/ruby/fibonacci.rb b/ruby/fibonacci.rb index 8a44b98a..a8e5ba37 100644 --- a/ruby/fibonacci.rb +++ b/ruby/fibonacci.rb @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# Ruby version 2.5.1 fibonacci = ->(n) { n < 2 ? n : fibonacci.(n - 1) + fibonacci.(n - 2) } # Test this by running `ruby fibonacci.rb n` where n is the index in the fibonacci sequence to calculate. From 7847db8a8fca82abc8877c34946bf4f9e7451485 Mon Sep 17 00:00:00 2001 From: William Zhou Date: Tue, 30 Oct 2018 13:44:53 -0400 Subject: [PATCH 4/4] Update fibonacci_iterative.rb Add # Ruby version 2.5.1 --- ruby/fibonacci_iterative.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/ruby/fibonacci_iterative.rb b/ruby/fibonacci_iterative.rb index 64b192ea..37127f7d 100644 --- a/ruby/fibonacci_iterative.rb +++ b/ruby/fibonacci_iterative.rb @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# Ruby version 2.5.1 # Calculate the fibonacci sequence iteratively by passing along fib(n-2) and fib(n-1) through each iteration of the loop. # The resulting fibonacci number is the sum of the last iteration of the loop. fibonacci = ->(n) { n < 2 ? n : 1.upto(n - 2).inject([0, 1]) { |last_two, i| fib = last_two.sum and [last_two[1], fib] }.sum }