Skip to content

Instantly share code, notes, and snippets.

@tbaba
Last active July 24, 2017 08:34
Show Gist options
  • Save tbaba/32efc166ea6bf37b5978c8221c2918ae to your computer and use it in GitHub Desktop.
Save tbaba/32efc166ea6bf37b5978c8221c2918ae to your computer and use it in GitHub Desktop.
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]
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
def fib(num)
case num
when 0, 1
num
else
fib(num - 1) + fib(num - 2)
end
end
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