21
\$\begingroup\$

You will be given a String that only contains letters of the English Alphabet, both lowercase and uppercase (ASCII 65-90 and 97-122). Your task is to output the Fizz-Buzzified version of the String.

How to Fizz-Buzzify a String?

  • Each letter that has an even index in the English alphabet (the alphabet must be 1-indexed: a->1,b->2,...,z->26) will be turned into fizz if it is lowercase and FIZZ if it is uppercase (f -> fizz, F -> FIZZ).

  • Each letter that has an odd index in the English alphabet will be turned into buzz if it is lowercase, and BUZZ if it is uppercase (e -> buzz, E -> BUZZ).

  • Let's have an example, to illustrate the algorithm, using the string CodeGolf (spaces added for clarity):

    "C o d e G o l f" ->  "BUZZ buzz fizz buzz BUZZ buzz fizz fizz"
     ^ ^ ^ ^ ^ ^ ^ ^
     1 1 0 1 1 1 0 0       (1 is odd index, 0 is even index)
    
  • If it is more convenient for your language, you may also leave single spaces between the groups (fizz, buzz, FIZZ, BUZZ). Hence, a result like fizzBUZZbuzzbuzz can also be returned as fizz BUZZ buzz buzz. Other separators are not allowed.


Test Cases:

Input  ->  Output 

"egg"          ->  "buzzbuzzbuzz"
"CodeGolf"     ->  "BUZZbuzzfizzbuzzBUZZbuzzfizzfizz"
"Reset"        ->  "FIZZbuzzbuzzbuzzfizz"
"ATOM"         ->  "BUZZFIZZBUZZBUZZ"
"yOuArEgReAt"  ->  "buzzBUZZbuzzBUZZfizzBUZZbuzzFIZZbuzzBUZZfizz"

  • Any standard method for I/O can be used.

  • Default Loopholes apply.

  • You are only allowed to take input in your language's native String type. The same applies for output.

  • You can assume that the input will not be empty.

  • Shortest code in bytes in every language wins. Good Luck and Fizz-Buzz!

\$\endgroup\$
1

23 Answers 23

7
\$\begingroup\$

Charcoal, 26 24 bytes

FθF⎇﹪℅ι²buzz¦fizz⎇№αι↥κκ

Try it online! Originally inspired by @CarlosAlejo. Edit: Saved 2 bytes by looping over the letters of fizz/buzz instead of assigning to a temporary. Explanation:

Fθ          Loop over the input (i = loop variable)
  F         Choose and loop over the word (k = loop variable)
   ⎇        Ternary
    ﹪℅ι²    If i has an odd ASCII code
    buzz
    fizz
            Print (implicit)
  ⎇         Ternary
   №αι      If i is an uppercase letter
    ↥κ      Uppercase(k)
     κ      k
\$\endgroup\$
5
  • \$\begingroup\$ I finally got to understand your solution. I thought that Ordinal(i) returned the position of the i char in its original string, but it returns its ASCII value (character code). Very clever solution, I still need to improve a lot my Charcoal skills! \$\endgroup\$
    – Charlie
    Jun 27, 2017 at 9:22
  • \$\begingroup\$ How you do this in Charcoal in only 24 bytes... \$\endgroup\$ Jun 27, 2017 at 9:40
  • \$\begingroup\$ In which encoding is this 24 bytes? \$\endgroup\$
    – Ruslan
    Jun 28, 2017 at 14:40
  • \$\begingroup\$ @Ruslan In Charcoal's custom code page. \$\endgroup\$
    – Neil
    Jun 28, 2017 at 16:46
  • \$\begingroup\$ @EriktheOutgolfer 22 bytes \$\endgroup\$
    – Neil
    Apr 10 at 11:19
6
\$\begingroup\$

C#, 92 bytes

using System.Linq;a=>string.Concat(a.Select(x=>x%2<1?x<97?"FIZZ":"fizz":x<97?"BUZZ":"buzz"))
\$\endgroup\$
5
  • \$\begingroup\$ Posted at practically the same time but you saved a byte to my solution by ordering the ternary the shorter way +1 \$\endgroup\$ Jun 27, 2017 at 8:22
  • \$\begingroup\$ @TheLethalCoder ahh, sorry about that. \$\endgroup\$
    – LiefdeWen
    Jun 27, 2017 at 8:24
  • \$\begingroup\$ Nothing to be sorry about we posted 12 seconds apart! And yours is shorter! \$\endgroup\$ Jun 27, 2017 at 8:24
  • \$\begingroup\$ Will C# let you use x%2 directly as a boolean, without needing the <1 part? If so, you may be able to save some bytes that way. \$\endgroup\$
    – Brian J
    Jun 27, 2017 at 15:36
  • \$\begingroup\$ @BrianJ No it doesn't do it implicitly \$\endgroup\$
    – LiefdeWen
    Jun 27, 2017 at 16:24
5
\$\begingroup\$

Python 3, 73 69 bytes

4 bytes thanks to ovs.

lambda s:"".join("fbFBiuIUzzZZzzZZ"[(c<"a")*2+ord(c)%2::4]for c in s)

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

C (gcc), 75 bytes

f(char*s){for(;*s;++s)printf(*s>90?*s%2?"buzz":"fizz":*s%2?"BUZZ":"FIZZ");}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Java (OpenJDK 8), 105 100 94 91 90 bytes

s->{for(int i:s.getBytes())System.out.print(i%2<1?i>90?"fizz":"FIZZ":i>90?"buzz":"BUZZ");}

Try it online!

Much golfable, very bytes, so Java!

Very golfed by @KevinCruijssen by 9 bytes!

\$\endgroup\$
4
  • \$\begingroup\$ I thought that maybe streams would make this shorter, so I wrote one. s->s.join("",s.chars().mapToObj(i->i>90?i%2<1?"fizz":"buzz":i%2<1?"FIZZ":"BUZZ").toArray(String[]::new)); Alas, it's 105 chars long :( If only they could add the join directly on the stream or integrate a toList, actually, anything sensible would be good. \$\endgroup\$ Jun 27, 2017 at 10:12
  • 1
    \$\begingroup\$ Printing directly instead of returning a String is 6 bytes shorter, and using getBytes() instead of toCharArray() is an additional 3 bytes shorter: s->{for(int i:s.getBytes())System.out.print(i>90?i%2<1?"fizz":"buzz":i%2<1?"FIZZ":"BUZZ");} \$\endgroup\$ Jun 27, 2017 at 10:52
  • \$\begingroup\$ You forgot the additional 3 bytes by changing toCharArray() to getBytes(). :) \$\endgroup\$ Jun 27, 2017 at 11:11
  • 1
    \$\begingroup\$ I clicked before your edit and didn't see it afterwards ;) Also, I just thought about it before seeing your comment xD \$\endgroup\$ Jun 27, 2017 at 11:21
4
\$\begingroup\$

Plain English, 820 632 610 bytes

Mr. Xcoder suggested eliminating an unneeded error trap, which saved 22 bytes.

A fizzy string is a string.
To convert a s string to a z fizzy string:
Clear the z.
Slap a r rider on the s.
Loop.
If the r's source's first is greater than the r's source's last, exit.
Put the r's source's first's target in a b byte.
If the b is not any letter, bump the r; repeat.
Put "FIZZ" in a t string.
If the b is d, put "BUZZ" in the t.
If the b is _, lowercase the t.
Append the t to the z.
Bump the r.
Repeat.
To decide if a b byte is d:
Put the b in a n number.
If the n is odd, say yes.
Say no.
To decide if a b byte is _:
Put the b in a c byte.
Lowercase the c.
If the c is the b, say yes.
Say no.

Ungolfed code:

A fizzy string is a string.

To convert a string to a fizzy string:
  Clear the fizzy string.
  Slap a rider on the string.
  Loop.
    If the rider's source's first is greater than the rider's source's last, exit.
    Put the rider's source's first's target in a byte.
    If the byte is not any letter, bump the rider; repeat.
    Put "FIZZ" in another string.
    If the byte is odd, put "BUZZ" in the other string.
    If the byte is lower case, lowercase the other string.
    Append the other string to the fizzy string.
    Bump the rider.
  Repeat.

To decide if a byte is odd:
  Put the byte in a number.
  If the number is odd, say yes.
  Say no.

To decide if a byte is lower case:
  Privatize the byte.
  Lowercase the byte.
  If the byte is the original byte, say yes.
  Say no.

The Plain English IDE is available at github.com/Folds/english. The IDE runs on Windows. It compiles to 32-bit x86 code.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "You can assume that the input will not be empty." so I think you can drop If the s is "", exit. \$\endgroup\$
    – Mr. Xcoder
    Jun 28, 2017 at 7:20
  • \$\begingroup\$ @Mr.Xcoder -- Thank you. It turns out that the trap was not needed even if s was empty. \$\endgroup\$
    – Jasper
    Jun 28, 2017 at 15:34
3
\$\begingroup\$

JavaScript (ES6), 79 77 bytes

s=>s.replace(/./g,c=>['BUZZ','buzz','FIZZ','fizz'][parseInt(c,36)%2*2|c>'Z'])

Test cases

let f =

s=>s.replace(/./g,c=>['BUZZ','buzz','FIZZ','fizz'][parseInt(c,36)%2*2|c>'Z'])

console.log(f("egg"        )) // "buzzbuzzbuzz"
console.log(f("CodeGolf"   )) // "BUZZbuzzfizzbuzzBUZZbuzzfizzfizz"
console.log(f("Reset"      )) // "FIZZbuzzbuzzbuzzfizz"
console.log(f("ATOM"       )) // "BUZZFIZZBUZZBUZZ"
console.log(f("yOuArEgReAt")) // "buzzBUZZbuzzBUZZfizzBUZZbuzzFIZZbuzzBUZZfizz"

\$\endgroup\$
3
\$\begingroup\$

C#, 97 bytes

using System.Linq;s=>string.Concat(s.Select(c=>"fizzbuzzFIZZBUZZ".Substring(c%2*4+(c>96?0:8),2)))
\$\endgroup\$
1
  • \$\begingroup\$ I don't know C#, but can't you use c%2>1?c>96?"fizz":"buzz":... \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 8:33
3
\$\begingroup\$

Jelly, 21 bytes

“=%“Ƈ×»ẋ13Œu;$ɓØWiЀị

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pyth, 23 21 bytes

smr<d\a@c"fizzbuzz"4C

Test suite.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ O_o 3 minutes :) \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 8:14
2
\$\begingroup\$

Charcoal, 40 36 bytes

Fθ¿№αι¿﹪⌕αι²FIZZ¦BUZZ¿﹪⌕βι²fizz¦buzz

Try it online!

Explanation:

Fθ                                      for every char i in the input:
   ¿№αι                                    if i is found in the uppercase alphabet
       ¿﹪⌕αι²                             if i is an even uppercase char
              FIZZ¦BUZZ                    print "FIZZ" or else "BUZZ"
                       ¿﹪⌕βι²             if i is an even lowercase char
                              fizz¦buzz    print "fizz" or else "buzz"

An alternative with the same byte count:

AfizzφAbuzzχFθ¿№αι¿﹪⌕αι²↥φ↥χ¿﹪⌕βι²φχ

Try it online! (Verbose version)

  • 4 bytes saved thanks to Neil!
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Use Count or Ordinal respectively instead of Find to save you some bytes. \$\endgroup\$
    – Neil
    Jun 27, 2017 at 8:45
  • \$\begingroup\$ The entire if counts as a single statement so you don't need the {}s. I also saved a byte by moving the Uppercase inside a Ternary: Try it online! \$\endgroup\$
    – Neil
    Jun 27, 2017 at 8:52
  • \$\begingroup\$ Better still, Fθ«A⎇﹪℅ι²buzz¦fizzχ⎇№αι↥χχ for 26 bytes. (Deverbosifier doesn't like this version.) \$\endgroup\$
    – Neil
    Jun 27, 2017 at 8:56
  • \$\begingroup\$ @Neil Thank you very much, but I can't even understand your last version, so I cannot take credit for it, I would feel as it is not my own answer. I'd be honored to be outgolfed by you if you write your own post. :-) \$\endgroup\$
    – Charlie
    Jun 27, 2017 at 9:05
2
\$\begingroup\$

><>, 68 bytes

<vv?("^"$%2:;?(0:i
v\?\"zzif"
v \\"zzub"
v \ "ZZIF"
v  \"ZZUB"
\oooo

Try it online, or watch it at the fish playground!

(But look at Aaron's answer which is 13 bytes shorter!)

If you're not familiar with ><>, there's a fish that swims through the code in 2D, and the edges wrap. The symbols >, <, ^ and v set the direction of the fish, / and \ are mirrors that reflect it, and ? means "do the next instruction if the top thing on the stack is non-zero, otherwise jump over the next instruction".

In the first line, the fish takes a character of input (i); if it's -1 for EOF, it halts (:0(?;); it gets the charcode mod 2 (:2%$); and it pushes a 1 or 0 on the stack depending on whether the charcode is less than or greater than the charcode of "^" ("^"(). The next three lines redirect the fish to the right fizz/buzz string, then the last line prints it (one o for each character) and sends the fish back to the start.

\$\endgroup\$
5
  • \$\begingroup\$ @Aaron, that's great! I did think of that idea but couldn't get it to work. Would you like to post that as your own solution? \$\endgroup\$
    – Not a tree
    Jun 27, 2017 at 14:17
  • \$\begingroup\$ @Aaron, you've also found a bug in either TIO or the online interpreter — in that interpreter, ! or ? followed by a space just skips the space, but TIO waits until the next non-space thing to skip… \$\endgroup\$
    – Not a tree
    Jun 27, 2017 at 14:22
  • \$\begingroup\$ @Aaron, your submission's much better than mine, so you can post it yourself and I'll delete this one. \$\endgroup\$
    – Not a tree
    Jun 27, 2017 at 14:24
  • \$\begingroup\$ ! should skips spaces AFAIK, but the online interpreter pads its codespace with spaces, which is probably what you're seeing \$\endgroup\$
    – Aaron
    Jun 27, 2017 at 14:28
  • \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – Aaron
    Jun 27, 2017 at 14:28
2
\$\begingroup\$

><>, 55 bytes

Based on Not a tree's answer.

<v%2$)"^":;?(0:i
 \?v"ZZIF"
~v >"ZZUB"!
^>{:}" "*+ol1=?

Instead of representing the 4 possible output in the code, I only represent their capitalized versions and add 32 to the character code to get the small case equivalents.

Try it online !

Modified code for the online interpreter, which pads its codespace with empty cells :

<v%2$)"^":;?(0:i
 \?v"ZZIF"
~v >"ZZUB"     !
^>{:}" "*+o l1=?
\$\endgroup\$
2
\$\begingroup\$

Perl5, 50+1 bytes

perl -nE'say+((FIZZ,BUZZ)x48,(fizz,buzz)x16)[unpack"C*",$_]'

Creates a 128-item list that maps ASCII chars to the proper code word.

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 22 bytes

v‘FIZZÒÖ‘#yÇÉèAyåil}J?

Try it online!

Explanation

v                        # for each char y of input string
 ‘FIZZÒÖ‘#               # push the list ['FIZZ','BUZZ']
          yÇÉ            # check if y has an odd character code
             è           # use this to index into the list
              Ayåi       # if y is a member of the lowercase alphabet
                  l}     # convert to lowercase
                    J?   # unwrap from list and print

Alternative 22 byte solution

ÇÉAISå·+‘FIZZÒÖ‘#Dl«èJ
\$\endgroup\$
1
\$\begingroup\$

PHP, 67 bytes

for(;$c=$argn[$i++];)echo[FIZZ,BUZZ,fizz,buzz][ord($c)%2+2*($c>Z)];

Try it online!

\$\endgroup\$
1
\$\begingroup\$

F#, 154 153 145 bytes

saved 1 9 bytes thanks to @Mr. Xcoder

let g s=
 Seq.map(fun c->match int c with
|x when x>64&&x<91->if x%2=0 then"FIZZ"else"BUZZ"
|x->if x%2=0 then"fizz"else"buzz")s
|>String.concat""

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Save 1 byte by removing the space between concat and "" on the last line \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 11:39
  • \$\begingroup\$ 150 bytes, although I don't know F# \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 11:43
  • \$\begingroup\$ 145 bytes, remove the unnecessary whitespace \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 11:45
  • \$\begingroup\$ Thanks @Mr.Xcoder I'm still getting used to F# and when indentation is significant! \$\endgroup\$
    – user20151
    Jun 27, 2017 at 11:52
  • 1
    \$\begingroup\$ And BTW, Welcome to PPCG! I don't know F# at all, just removed stuff to see what happens :) \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 11:53
1
\$\begingroup\$

Mathematica, 134 bytes

""<>{(s=Max@ToCharacterCode@#;If[96<s<123,If[EvenQ@s,c="fizz",c="buzz"]];If[64<s<91,If[EvenQ@s,c="FIZZ",c="BUZZ"]];c)&/@Characters@#}&
\$\endgroup\$
1
\$\begingroup\$

Java 8, 89 bytes

s->s.chars().mapToObj(i->i%2<1?i>90?"fizz":"FIZZ":i>90?"buzz":"BUZZ").collect(joining());

It assumes import static java.util.stream.Collectors.*;

\$\endgroup\$
1
\$\begingroup\$

q/kdb+, 48 bytes

Solution:

raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$

Examples:

q)raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$"egg"
"buzzbuzzbuzz"
q)raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$"CodeGolf"
"BUZZbuzzfizzbuzzBUZZbuzzfizzfizz"
q)raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$"Reset"
"FIZZbuzzbuzzbuzzfizz"
q)raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$"ATOM"
"BUZZFIZZBUZZBUZZ"
q)raze{@[($)`fizz`buzz x mod 2;(&)x<91;upper]}"i"$"yOuArEgReAt"
"buzzBUZZbuzzBUZZfizzBUZZbuzzFIZZbuzzBUZZfizz"

Explanation:

Fairly straightforward, take the input string, cast into ASCII values, create a list of 1 or 0 depending whether it is odd or even (hint A=65=odd), then use this list to index into a list of fizz and buzz. Cast this to a string, and then for cases where the input is <91 (lower than a Z) we apply the upper function to get a FIZZ or a BUZZ.

q is interpreted right to left:

raze{@[string `fizz`buzz x mod 2;where x < 91;upper]}"i"$ / ungolfed version
                                                     "i"$ / cast input to ascii values
    {                                               }     / anonymous lambda
     @[                         ;            ;     ]      / apply 
                                              upper       / upper-case
                                 where x < 91             / indices where input is less than 91 (ie uppercase)
                         x mod 2                          / returns 0 if even and 1 if odd
              `fizz`buzz                                  / 2 item list, which we are indexing into
       string                                             / cast symbols to strings `buzz -> "buzz"
raze                                                      / raze (merge) list into a single string
\$\endgroup\$
2
  • \$\begingroup\$ I don't know this language, but can you remove the space between mod and 2? \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 20:13
  • \$\begingroup\$ Unfortunately not, it could be written it as mod[x;2] if we didn't want any whitespace - but that winds up being 1 byte more! \$\endgroup\$
    – mkst
    Jun 27, 2017 at 20:15
1
\$\begingroup\$

Ruby, 61+1 = 62 bytes

Uses the -p flag.

gsub(/./){k=$&.ord%2*4;$&<?_?"FIZZBUZZ"[k,4]:"fizzbuzz"[k,4]}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift 4, 144 135 bytes

func f(s:String){print(s.map{let d=Int(UnicodeScalar("\($0)")!.value);return d%2<1 ?d>90 ?"fizz":"FIZZ":d>90 ?"buzz":"BUZZ"}.joined())}

Un-golfed:

func f(s:String){
    print(
        s.map{
            let d=Int(UnicodeScalar("\($0)")!.value)
            return d%2 < 1 ? d > 90 ? "fizz" : "FIZZ" : d > 90 ? "buzz" : "BUZZ"
        }.joined()
    )
}

What I am doing is looping over each character in the string. I convert each to its ASCII value, then check to see if it is even or odd, and then check to see if it is upper or lowercase and output the matching value from the loop. I then join all the elements of the resulting array into a single string and print it.

This solution uses Swift 4, so there is no way to easily test it online yet.

Thanks to @Mr.Xcoder for saving me 9 bytes!

\$\endgroup\$
5
  • \$\begingroup\$ error: value of type 'String' has no member 'map', because a String is not automatically converted to a list on runtime. \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 17:56
  • \$\begingroup\$ Also, that service is not free. You have to add a credit card in order to test your submission if I don't copy-paste it. Consider changing the test service. \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 17:58
  • \$\begingroup\$ In order to make it work, you must turn it into $0.characters.map{...} \$\endgroup\$
    – Mr. Xcoder
    Jun 27, 2017 at 18:00
  • \$\begingroup\$ @Mr.Xcoder Cloud 9 has a free tier, in which you only get one private workspace and unlimited public workspaces. You should be able to sign in through GitHub. I have never shared anything with it so I wasn't sure if it would work. Also, I should have mentioned I am using Swift 4, where String conforms to Collection. This mean it has a map function. \$\endgroup\$ Jun 27, 2017 at 18:46
  • 1
    \$\begingroup\$ As I can see, the old function declaration is still valid in Swift 4, like func f(s:String){...}, hence you can save 9 bytes by using the following code, that uses print instead of return: pastebin.com/06kiiGaJ. If it doesn't work, let me know. \$\endgroup\$
    – Mr. Xcoder
    Jun 28, 2017 at 7:27
1
\$\begingroup\$

R, 150 123 108 bytes

i=ifelse
cat(i(sapply(el(strsplit(scan(,''),'')),utf8ToInt)>91,i(x%%2,'buzz','fizz'),i(x%%2,'BUZZ','FIZZ')))

Has to be shorter using ASCII? It was shorter. See edit history for old answer, which used letters.

Checks each letter for (1) whether it's capital or not (>91) and whether it's a fizz or a buzz.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.