Created
October 9, 2020 02:55
-
-
Save ankita199/06b6a0c2bb871cd7da04932ed7daaab5 to your computer and use it in GitHub Desktop.
Reverse Given Input and fibonacci series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_reverse(str) | |
str.reverse | |
end | |
str = "hello" | |
puts get_reverse(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
A fibonnaci sequence is defined like the following | |
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... | |
The next number is found by adding up the two numbers before it. The first two numbers of | |
the sequence are 0,1. | |
Write a function that outputs the nth number in the series | |
For example, ``` | |
fibonnaci(0) => 0 | |
fibonnaci(1) => 1 | |
fibonnaci(4) => 3 | |
fibonnaci(7) => 13``` | |
Your function should work for large numbers of | |
=end | |
def fibonacci(n) | |
return n if [0,1].include?(n) | |
(1...n).inject([0,1]) { |number| number << number.last(2).inject(:+) }.last | |
end | |
p fibonacci(0) | |
p fibonacci(1) | |
p fibonacci(3) | |
p fibonacci(7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment