for x in range(1, 101):
out = ""
if x % 3 == 0:
out += "Fizz"
if x % 5 == 0:
out += "Buzz"
print(out or x)FizzBuzz in ten languages
41–50 of 103 posts
Re: FizzBuzz in ten languages
#42Re: FizzBuzz in ten languages
#43puts (1..100).map { |i| (fb = [["Fizz"][i % 3], ["Buzz"][i % 5]].compact.join).empty? ? i : fb }
Re: FizzBuzz in ten languages
#44Why or how did fizzbuzz earn its place in lore? I looked at Wikipedia and it didn't seem to go into detail. Also we have the teapot, hello world, Foo Bar What are some others?
Re: FizzBuzz in ten languages
#45 with Ada.Text_IO;
procedure FizzBuzz_Predicate is
subtype Div_3 is Integer
with Dynamic_Predicate => Div_3 mod 3 = 0;
subtype Div_5 is Integer
with Dynamic_Predicate => Div_5 mod 5 = 0;
begin
for i in Integer range 1 .. 99 loop
if i in Div_3 and i in Div_5 then
Ada.Text_IO.Put_Line ("FizzBuzz");
elsif i in Div_3 then
Ada.Text_IO.Put_Line ("Fizz");
elsif i in Div_5 then
Ada.Text_IO.Put_Line ("Buzz");
else
Ada.Text_IO.Put_Line (Integer'Image (i));
end if;
end loop;
end FizzBuzz_Predicate;Re: FizzBuzz in ten languages
#46I like Ruby one liner from https://commandercoriander.net/blog/2013/02/03/fizzbuzz-in-o... puts (1..100).map { |i| (fb = [["Fizz"][i % 3], ["Buzz"][i % 5]].compact.join).empty? ? i : fb }
Re: FizzBuzz in ten languages
#47Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )). for i in {1..100}; do if (( $i % 3 && $i % 5 )); then echo FizzBuzz elif (( $i % 3 )); then echo Fizz elif (( $i % 5 )); then echo Buzz else echo $i fi done Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do case "$((…
#/bin/bash
for n in {1..100} ; do
f=""
((n % 3)) || f="Fizz"
((n % 5)) || f="${f}Buzz"
echo ${f:-$n}
doneRe: FizzBuzz in ten languages
#48Re: FizzBuzz in ten languages
#49Re: FizzBuzz in ten languages
#50 x