Skip to content

Instantly share code, notes, and snippets.

@virajkulkarni14
Last active December 24, 2015 18:59
Show Gist options
  • Save virajkulkarni14/6847512 to your computer and use it in GitHub Desktop.
Save virajkulkarni14/6847512 to your computer and use it in GitHub Desktop.
FizzBuzzThe Problem and its various solutions: Amazing creativity! (Source: http://www.talentbuddy.co/challenge/5227bcc74af0110af382d87c51846c184af0110af3822c30)
Given an integer number N
Your task is to
write a function that prints to the standard output (stdout) the numbers from 1 to N (one per line) with the following restrictions:
for multiples of three print “Fizz” instead of the number
for the multiples of five print “Buzz” instead of the number
for numbers which are multiples of both three and five print “FizzBuzz”
Note that your function will receive the following arguments:
n
which is the integer number described above
Data constraints
the maximum value of N will not exceed 1000
Efficiency constraints
your function is expected to print the result in less than 2 seconds
Example:
Input
n: 15
Output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
def fizzbuzz(n)
# My Solution
if !n.between?(1, 1000)
puts "Please enter a value between 1 and 1000"
else
(1..n).each do |i|
case
when (i % 15) == 0
puts "FizzBuzz"
when (i % 3) == 0
puts "Fizz"
when (i % 5) == 0
puts "Buzz"
else
puts i
end
end
end
end
def fizzbuzz(n)
for i in 1..n do
o = ""
o += "Fizz" if i % 3 == 0
o += "Buzz" if i % 5 == 0
o = i.to_s if o == ""
puts o
end
end
def fizzbuzz(n)
1.upto(n) do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
end
end
def fizzbuzz(n)
(1..n).each{|i| puts (st=(i%3>0 ? '':"Fizz")+(i%5>0 ? '':"Buzz")).empty? ? i:st}
end
def fizzbuzz(n)
ret = (1..n).to_a.map { |x|
x % 15 == 0 ? "FizzBuzz" :
x % 3 == 0 ? "Fizz" :
x % 5 == 0 ? "Buzz" : x
}.join("\n")
print ret
end
def fizzbuzz(n)
1.upto(n) do |z|
y = ''
y += 'Fizz' if (z % 3 == 0)
y += 'Buzz' if (z % 5 == 0)
puts(y.empty? ? z : y)
end
end
def fizzbuzz(n)
1.upto(n) do |i|
fizz = i % 3 == 0
buzz = i % 5 == 0
if fizz || buzz
puts (fizz ? "Fizz" : "") + (buzz ? "Buzz" : "")
else
puts i
end
end
end
def fizzbuzz(n)
(1..n).each do |i|
fizz(i)
end
end
def fizz(n)
e = []
e << "Fizz" if n % 3 == 0
e << "Buzz" if n % 5 == 0
puts e.any? ? e.join : n
end
def fizzbuzz(n)
(1..(n)).each do |i|
out = ""
out += "Fizz" if i%3 == 0
out += "Buzz" if i%5 == 0
puts (out.empty? ? i : out)
end
end
def fizzbuzz(min = 1, max = 100, n1 = 3, n2 = 5)
(min..max).map{|n| case(0) when n % (n1*n2) then 'fizzbuzz'; when n % n1 then 'fizz'; when n % n2 then 'buzz'; else n end}.join ' '
end
p (1..100).map{|i|o="";o<<"Fizz" if i%3==0;o<<"Buzz" if i%5==0;o<<i.to_s if o=="";o}
1.upto(100){|n|puts'FizzBuzz'[o=n**4%-15,o+13]||n}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment