54
\$\begingroup\$

Write a program or function that outputs this exact text, case-insensitive:

A, B, C, D, E, F, G,
H, I, J, K, L, M, N, O, P,
Q, R, S,
T, U, V,
W, X, Y, Z.

(Based on the alphabet song that many American kids learn to help memorize the alphabet, though edited for more compressibility.)

The output must look exactly the same as the above (again, case-insensitive), but may contain trailing spaces on each line and/or trailing newlines. Notice the period at the end.

This is code-golf, so the shortest code in bytes wins.

\$\endgroup\$
5
  • 4
    \$\begingroup\$ For regex based languages consider a 0 width lookahead... /(?=[HQTW])/ \$\endgroup\$ Sep 30, 2016 at 18:24
  • 34
    \$\begingroup\$ I thought it was H, I, J, K, LMNO, P? \$\endgroup\$ Sep 30, 2016 at 21:00
  • 8
    \$\begingroup\$ Shouldn't the last line end with "Y and Z."? \$\endgroup\$
    – KM.
    Oct 2, 2016 at 12:36
  • 2
    \$\begingroup\$ @KM. This was discussed in the sandbox, and we decided to stick with this version to make the challenge less complicated. \$\endgroup\$ Oct 2, 2016 at 18:26
  • 9
    \$\begingroup\$ Golf seems boring. Cricket is better. \$\endgroup\$
    – m4n0
    Oct 3, 2016 at 19:49

60 Answers 60

1
2
2
\$\begingroup\$

Cheddar, 57 bytes

->(65@"90).sub(/[GPSV]/g,"$0
").sub(/[^Z\n]/g,"$0, ")+"."

Try it online! Isn't that beautiful? It's a nice rectangle.

Two regex substitutions. (65@"90) is the uppercase alphabet, .sub(/[GPSV]/g,"$0\n") replaces GPSV with itself and "\n", .sub(/[^Z\n]/g,"$0, ") replaces all non newline and Z characters with itself and ", ", and "." adds a final ..

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice technique, and nice job outgolfing Downgoat ;) \$\endgroup\$ Oct 2, 2016 at 18:27
2
\$\begingroup\$

TI-Basic, 86 bytes

Cheers to anyone who can compress it further :3

Disp "A, B, C, D, E, F, G,","H, I, J, K, L, M, N, O, P,","Q, R, S,","T, U, V,
"W, X, Y, Z.
\$\endgroup\$
1
  • \$\begingroup\$ I think this is optimal, unfortunately. \$\endgroup\$ Oct 2, 2016 at 18:16
2
\$\begingroup\$

MathGolf, 20 bytes

'A[793∙]öæ└╣·\n\\;'.

Try it online!

Explanation

'A                    Push "A"
  [793∙]              Push [7, 9, 3, 3, 3]
        ö             Start foreach-loop over index array using next 7 instructions
         æ            Start for-loop for current item of index array, using next 4 instructions
          └           Duplicate and increment, e.g. "A" -> "A", "B"
           ╣·         Push ", "
             \        Swap top two, keeping the letter on top after inner loop
              n\      Push newline and swap top two, keeping the letter on top
                \;'.  Swap top two, discard top, and push "." (remove trailing comma)
\$\endgroup\$
2
\$\begingroup\$

Java (JDK), 66 bytes

v->"ABCDEFG\nHIJKLMNOP\nQRS\nTUV\nWXY".replaceAll(".","$0, ")+"Z."

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt -R, 17 bytes

;LiB¬q,ú2¹óÏk`qw

Test it

;LiB¬q,ú2¹óÏk`qw
;L                   :"."
  i                  :Prepend
;  B                 :  Uppercase alphabet
    ¬                :  Split
     q               :  Join with
      ,ú2            :    "," right padded with spaces to length 2
         ¹           :End prepend
          ó          :Partition before characters that return a falsey value (empty string)
           Ï         :When passed through the following function
            k        :  Case insensitively remove
             `qw     :  Characters in the compressed string "qthw"
                     :Implicit output joined with newlines
\$\endgroup\$
2
\$\begingroup\$

C (76 bytes)

c;main(){for(c=64;++c<90;)printf("%c,%c",c," \n"[4784256>>c&1]);puts("Z.");}

This relies on

  1. implicit-int declarations (for c, main, puts and printf without #include<stdio.h>);
  2. ASCII;
  3. >> being modulo the size of an integer in bits, either 32 or 64, so that X>>'A' gives "X shifted by 1" and X>>'Z' gives "X shifted by 26".

4784256 is 2⁷+2¹⁶+2¹⁹+2²² (or equivalently 1<<7 | 1<<16 | 1<<19 | 1<<22), because 'G', 'P', 'S', and 'V' are respectively the 7th, 16th, 19th, and 22nd letters of the alphabet.

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

Retina, 43 bytes


Z
{2`
$`
}T01`L`_L
.
$&, 
[HQTW]
¶$&
, $
.

Leading newline is significant. Try it online!

This is my first time using Retina, so any golfing tips are appreciated...

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

Forth, 85 bytes

This is a function that prints the string. Checking for when to print a new line is costly. This is still shorter than simply printing the required string, because it has new lines in it. The code does not need to be on this many lines, but there does need to be a line break within the DO LOOP somewhere. I don't know why.

: f 90 65 DO
I 72 = I 81 = I 84 = I 87 = + + +
if CR then
I emit ." , "
LOOP ." Z." ;

Try it online

The code is case-insensitive.

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

Ruby, 57 bytes

26.times{|i|$><<(i+65).chr+",."[i/25]+" 
"[2392128>>i&1]}

The space at the end of the first line is required: it's a literal [space][newline.]

C,74 bytes

c;main(){for(;++c<27;)printf("%c%c%c",64+c,44+c/26*2,4784256>>c&1?10:32);}

Basic bitshift and mask to determine when to print a space and when to print a newline. I noted that with the A and Z removed all groups are a multiple of 3 letters long, but I was unable to make use of this.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ That 4784256>>c&1 is a great idea. But you should probably post the two languages as two separate answers. \$\endgroup\$ Sep 30, 2016 at 22:13
1
\$\begingroup\$

PHP, 67 65 64 bytes

Not exactly short, but it works.
This assumes that all errors are going to stderr.

foreach(range(A,Z)as$C)echo$C,",."[$C>Y],"
 "[!strstr(GPSV,$C)];

You can try it on http://ideone.com/VSK8Yu.

Thanks to @Jörg Hülsermann for saving 2 bytes! And thanks to @Titus for saving me 1 byte!

\$\endgroup\$
21
  • \$\begingroup\$ $C>Y instead of $C==Z saves one Byte and 'strstr(GPSV,$C)' instead of strpos(zGPSV,$C) should also save one Byte \$\endgroup\$ Sep 30, 2016 at 19:53
  • \$\begingroup\$ @JörgHülsermann That is pure genious! I should have though about that! \$\endgroup\$ Sep 30, 2016 at 20:30
  • \$\begingroup\$ @ETHproductions Duh, stupid me. Thanks, I totally forgot about it. \$\endgroup\$ Sep 30, 2016 at 20:33
  • \$\begingroup\$ Uhm ... don´t you need all doublequotes for -r? \$\endgroup\$
    – Titus
    Oct 1, 2016 at 10:56
  • 1
    \$\begingroup\$ Unix LF Windows CR LF Apple CR can only works on an unix system "\n "[!strstr(GPSV,$c)] should work on every system. By the way the solution you have found Ismael is more genious \$\endgroup\$ Oct 1, 2016 at 11:56
1
\$\begingroup\$

Racket 173 bytes

(λ()(let((l(list 7 9 3 3 4))(s 65))(for((i 5))(define e(+ s(list-ref l i)))(for((j(range s e)))
(printf"~a~a "(integer->char j)(if(= j 90)"."",")))(printf"~n")(set! s e))))

Ungolfed:

(define f
  (λ()
    (let ((l (list 7 9 3 3 4))
          (s 65))
      (for ((i 5))
        (define e (+ s (list-ref l i)))
        (for ((j (range s e)))
          (printf "~a~a " (integer->char j) (if(= j 90) "." ",")))
        (printf "~n")
        (set! s e)
        ))))

Testing:

(f)

Output:

A, B, C, D, E, F, G, 
H, I, J, K, L, M, N, O, P, 
Q, R, S, 
T, U, V, 
W, X, Y, Z. 
\$\endgroup\$
1
\$\begingroup\$

Haskell, 60 bytes

putStr$(['A'..'Y']>>= \x->x:", "++['\n'|elem x"GPSV"])++"Z."

Try it on Ideone.

putStr$(['A'..'Y']>>= \x->x:", "++['\n'|elem x"GPSV"])++"Z."
        ['A'..'Y']                                          --String "ABC...XY"
                      \x->x:", "++['\n'|elem x"GPSV"]       --function to append ", " and a "\n" when needed
       (          >>=                                )      --map function over the string and concat
                                                      ++"Z."--append "Z."
putStr$                                                     --print
\$\endgroup\$
1
\$\begingroup\$

Fourier, 56 bytes

|~S44a32aS^|f|(iaf~i)10a|L65~i72L81L84L87L90(iaf~i)ia46a

Again, uses functions to take out repeated parts of the code.

Try it FourIDE!

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

Clojure, 154 bytes

#(print(loop[a(map char(range 65 91))[c & d][7 9 3 3 4]s""](if c(let[[l r](split-at c a)e(if d\,\.)](recur r d(str s(clojure.string/join", "l)e"\n")))s)))

That call to clojure.string/join is killer, although this wasn't competitive to begin with. Basically, it cuts the generated alphabet string a at the predefined line lengths, then adds each line to the acc.

It would have been significantly shorter for me to just "cheat" and print the string (91 bytes):

(print "A, B, C, D, E, F, G,\nH, I, J, K, L, M, N, O, P,\nQ, R, S,\nT, U, V,\nW, X, Y, Z.")

But where's the fun in that?

Ungolfed:

(defn abcs []
  (print
    (loop [alpha (map char (range 65 91))
           [c & cuts] [7 9 3 3 4]
           acc ""]
      (if c
        (let [[a-line a-rest] (split-at c alpha)
              e (if cuts \, \.)]
          (recur a-rest cuts (str acc (clojure.string/join ", " a-line) e "\n")))
        acc))))
\$\endgroup\$
1
\$\begingroup\$

GameMaker Language, 85 bytes

b=""for(a=65;a<90;a++){d=chr(a)c=", "if string_pos(d,"GPSV")c=",#"b+=d+c}return b+"Z.

Boring alternate (94 bytes)

return string_replace_all("AaBaCaDaEaFaG,#HaIaJaKaLaMaNaOaP,#QaRaS,#TaUaV,#WaXaYaZ.","a",", ")

Pretty straightforward. Note that # is newline literal (think \n in most languages). Use \# for #.

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

><>, 52 bytes

79334"A"\
or:?!~r&>:o1+:}&a" ,.["{=:}?$~o{?;{1-:}?$~

Try it online!

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

PHP, 61 58 55 50 bytes

Note: uses Windows 1252 encoding.

for($a=A;$a<Z;)echo"$a,",~ßõ[!trim($a++,GPSV)]?>Z.

Run like this:

php -r 'for($a=A;$a<Z;)echo"$a,",~ßõ[!trim($a++,GPSV)]?>Z.' 2>/dev/null;echo

If your terminal is set to UTF-8, this shows that it works:

php -r 'for($a=A;$a<Z;)echo"$a,"," \n"[!trim($a++,GPSV)]?>Z.' 2>/dev/null;echo

Explanation

Iterates over the alphabet until Y is reached. Prints the letter with a comma, then prints a space, unless the letter is one of GPSV (found by !trim($a,"GPSV")). After the loop, print Z..

Tweaks

  • Saved 3 bytes by using strpos instead of char count
  • Saved 3 bytes by using trim instead of strpos
  • Saved 5 bytes by using the closing tag to print a literal Z. instead of echo
\$\endgroup\$
1
\$\begingroup\$

q, 41 bytes

@[", "sv,:'[.Q.A];20 47 56 65;:;"\n"],"."

How it works:

            .Q.A                          / Built-in alphabet
        ,:'[    ]                         / Flip data (to 1-row matrix)
  ", "sv                                  / Join with ", "
@[               ;20 47 56 65;:;"\n"]     / Replace spaces with newlines at indices
                                     ,"." / Append period
\$\endgroup\$
1
\$\begingroup\$

PHP, 90 chars

A<?for($i=66;$i<91;$i++){echo", ";if(in_array($i,[72,81,84,87]))echo"\n";echo chr($i);}?>.

I know it's longer than 77 chars, but this is as short as it gets.

\$\endgroup\$
2
  • \$\begingroup\$ timmyRS, When I noticed the "." outside your closing tag, I thought for a split second that you had an error, but I realized you were saving the trouble of concatenating that into your existing string. But, that also made me realize that anything greater than 77 bytes is overkill because you could to the whole thing as plain text and it would work just as well as your ".". \$\endgroup\$
    – TecBrat
    Oct 3, 2016 at 18:37
  • \$\begingroup\$ @TecBrat Well I fixed it now ;P \$\endgroup\$
    – Sainan
    Oct 5, 2016 at 11:06
1
\$\begingroup\$

PowerShell, 56 bytes

('ABCDEFG
HIJKLMNOP
QRS
TUV
WXY'-replace'.','$0, ')+'Z.'

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 53 bytes? \$\endgroup\$
    – GMills
    Mar 6, 2019 at 14:24
  • \$\begingroup\$ Thanks. It's cool. A char range allowed in Powershell 6+ only. I think it would be better if you create a new post with Powershell 6. \$\endgroup\$
    – mazzy
    Mar 6, 2019 at 15:42
  • \$\begingroup\$ Ok, I will. Thanks. \$\endgroup\$
    – GMills
    Mar 6, 2019 at 18:54
1
\$\begingroup\$

><>, 45 bytes

4339"A"7v$oa~<
"&+oo1+$>:1(?^1-$:o:"Z"=2*&" ,

Try it online!

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

Stax, 16 bytes

é╡₧!D☻àw╟`«▒Σ╪░↓

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

'.          push "."
VA          push "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
',\         repeat zip alphabet with comma, yielding ["A,", "B,", ...]
J           join with spaces
)           overwrite end of joined string with period (pushed first)
"r!WEF"!    crammed array literal for [20, 47, 56, 65]
A&          assign 10 to these array indices.  10 is the codepoint for newline.

Run this one

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

APL (Dyalog Unicode), 30 29 bytesSBCS

Full program.

↑'AHQTW'(∊⍨⊂⊢)'.',⍨∊', '∘,¨⎕a

Try it online!

⎕a the uppercase alphabet

', '∘,¨ prepend ", " to each letter

ϵnlist (flatten)

'.',⍨ append a period

'AHQTW'() apply the following tacit function with the string as left argument:

 the right argument (", A, B…")

 partitioned so a new partition begins at each True in

∊⍨ the Boolean mask indicating argument elements that are members of the left argument

 mix the list of strings into a character matrix

solved together with Adam

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

PowerShell 6, 53 52 47 bytes

"$('A'..'Y'|%{"$_,"-replace'[HQTW]','
$&'}) Z."

Try it online!

I believe PowerShell 6 is newer than this challenge. Originally posted as a suggestion for @mazzy's solution.

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

ReRegex, 47 bytes

-/,\n/\w\B/$0, /ABCDEFG-HIJKLMNOP-QRS-TUV-WXYZ.

Ungolfed

-/,\n/                             # Replace all dashes with a comma and newline
\w\B/$0, /                         # Replace all letters preceeding a letter with itself and a comma space.
ABCDEFG-HIJKLMNOP-QRS-TUV-WXYZ.    # The default code.

Try it online!

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

sed, 54 bytes

No further tricks take us down, I'm afraid:

s/^/ABCDEFG\
HIJKLMNOP\
QRS\
TUV\
WXYZ./
s/[A-Y]/&, /g

Try it online!

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

Ly, 29 bytes

'A043397[[r:o`r' ',oo,]p9`o];

Try it online!

This code pushes the number of letters to print on each line onto the stack. It also keeps the current character on the end of the stack, iterating it each time it prints. The code loops until the "number of letters to print on the next line" is zero.

'A                            - push "A" on the stack
  0                           - add 0 divider to stop loop
   43397                      - push number of chars per line
        [                  ]  - loop while number of chars>0
         [           ,]       - loop for one line of chars
          r                   - reverse stack
           :                  - duplicate current char
            o                 - print
             `                - increment to next char
              r               - reverse stack again
               ' ',oo         - push/print " ,"
                       p      - delete iterator var
                        9`o   - push/print LF
                            ; - exit to abandon junk on stack
\$\endgroup\$
0
\$\begingroup\$

///, 62 bytes

/c/, /AcBcCcDcEcFcG,
HcIcJcKcLcMcNcOcP,
QcRcS,
TcUcV,
WcXcYcZ.

Try it online!

Not much to golf here, but I reduced it from 77 bytes to 62 just by replacing , with c.

\$\endgroup\$
3
  • \$\begingroup\$ What would //, /ABCDE... do? \$\endgroup\$ Oct 1, 2016 at 12:34
  • \$\begingroup\$ @ETHproductions It might crash your computer. I put a c for comma. \$\endgroup\$ Oct 1, 2016 at 12:38
  • 1
    \$\begingroup\$ @ETHproductions Substitutions are repeated until the search string is no longer found. So //.../ will always result in an infinite loop. \$\endgroup\$ Oct 2, 2016 at 11:35
0
\$\begingroup\$

ListSharp, 88 bytes

SHOW="A, B, C, D, E, F, G,\nH, I, J, K, L, M, N, O, P,\nQ, R, S,\nT, U, V,\nW, X, Y, Z."
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Couldn't you put the \n in the string? It looks like that would be shorter. \$\endgroup\$
    – Timtech
    Oct 2, 2016 at 16:48
  • \$\begingroup\$ @Timtech oh dang, I thought im smart but im not \$\endgroup\$ Oct 2, 2016 at 16:53
  • \$\begingroup\$ It's ok. Just something like SHOW="A, B, C, D, E, F, G,\nH, I, J, K, L, M, N, O, P,\nQ, R, S,\nT, U, V,\nW, X, Y, Z." is 88 bytes instead of 113. \$\endgroup\$
    – Timtech
    Oct 2, 2016 at 16:58
0
\$\begingroup\$

Julia 1.0, 53 bytes

_->replace(join('A':'Z',", "),r"(?=[HQTW])"=>"
")*'.'

Try it online!

\$\endgroup\$
1
2

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.