Last active
July 24, 2017 08:34
-
-
Save tbaba/32efc166ea6bf37b5978c8221c2918ae to your computer and use it in GitHub Desktop.
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 reverse!(n) | |
(n.length / 2).times { |i| n[-(i + 1)], n[i] = n[i], n[-(i + 1)] } | |
n | |
end | |
def reverse(n) | |
n.length.downto(1).each_with_object([]) { |i, lst| lst << n[i - 1] } | |
end | |
p reverse! [1, 2, 3, 4] | |
p reverse! [1, 2, 3, 4, 5] | |
p reverse [1, 2, 3, 4] | |
p reverse [1, 2, 3, 4, 5] |
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 fizzbuzz(n) | |
if n % 15 == 0 | |
"FizzBuzz" | |
elsif n % 5 == 0 | |
"Buzz" | |
elsif n % 3 == 0 | |
"Fizz" | |
else | |
n | |
end | |
end | |
100.times do |i| | |
puts fizzbuzz i | |
end |
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 fib(num) | |
case num | |
when 0, 1 | |
num | |
else | |
fib(num - 1) + fib(num - 2) | |
end | |
end |
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 sosuu | |
(2..100).to_a.each_with_object([]) { |i, sosuus| sosuus << i if sosuus.none? { |j| (i % j).zero? } } | |
end | |
p sosuu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment