26
\$\begingroup\$

Note that this is not the same as Print the alphabet four times.

This task is to write a program to generate four copies of each letter of the English alphabet, one letter per line, on standard output:

A
A
A
A
B
B
B
B

etc.

The output should include newlines after each letter.

Uppercase letters with no extra output are preferred; however, lowercase letters and/or extra whitespace are acceptable if capitalizing/stripping would lengthen your solution.

EDITED TO ADD: The solution must be complete enough to execute. I should be able to invoke an implementation of the language, paste the code from the answer, and get results, without typing any additional code.

The above question of completeness came up in the context of a C solution. Surely there must be a standing rule or convention about this on a code golfing site? If so, I'll gladly yield to the community guidelines. But this is my take:

  1. With regard to C specifically, you need to include (and count) the main(){...} around the code, since it won't compile otherwise. Warnings because there's no #include <stdio.h> are fine as long as the code still compiles. In general, a compiled language requires a compilable unit.

  2. A bare expression that yields the desired results is acceptable if there's a way to execute the expression directly; for instance, if the language has a REPL. So you can submit Haskell without a main= as long as it actually works as written at e.g. the ghci prompt. But since that means putting let on your declarations, it may be a net win to stick with the runhaskell format.

  3. Similarly, awk scripts should be in BEGIN (or END, with the assumption that stdin is attached to /dev/null) blocks since we're not processing any input.

etc.

\$\endgroup\$
  • 4
    \$\begingroup\$ I'm slightly confused. Is the challenge here just to output the alphabet with each letter repeated four times, or does the output actually need to be stored in a file as well? \$\endgroup\$ – Iszi Dec 13 '13 at 17:26
  • \$\begingroup\$ And do I have to output only the alphabet? \$\endgroup\$ – Justin Dec 13 '13 at 17:33
  • \$\begingroup\$ @MarkReed Do I need to print it with newlines in between? Why not just print it, but newlines optional? \$\endgroup\$ – Justin Dec 13 '13 at 17:44
  • 3
    \$\begingroup\$ Also, I recommend rephrasing your challenge so that it is more like a challenge and less like telling the story of how you invented your answer. \$\endgroup\$ – Justin Dec 13 '13 at 17:46
  • \$\begingroup\$ The last bit muddies the whitespace rules just a tad. Could you please clarify? Particularly, am I reading it right to interpret that extra whitespace is okay but omission of newlines is not? \$\endgroup\$ – Iszi Dec 13 '13 at 19:44

81 Answers 81

2
\$\begingroup\$

Q (13)

.........

-1@''4#'.Q.A;
\$\endgroup\$
2
\$\begingroup\$

Javascript 55

for(i=65;i<91;i+=1/4)document.write("&#"+(i|0)+";<br>")

1 character shorter, but outputs to the screen instead of console.

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

F#: 61 62 49

for i in 'A'..'Z'do for j in 0..4 do printfn"%c"i
\$\endgroup\$
  • \$\begingroup\$ hm, do you not need ;; at the end? \$\endgroup\$ – Mark Reed Dec 15 '13 at 2:32
  • \$\begingroup\$ @MarkReed If executed from fsi.exe, yes, you would usually need that. However, this is a complete program by itself, so you can simply compile and run it as is. \$\endgroup\$ – p.s.w.g Dec 17 '13 at 23:06
2
\$\begingroup\$

Pyke, 6 bytes (noncompetitive)

G4m*_X

Try it here!

Or 4 bytes without newline separation

G4m*
\$\endgroup\$
2
\$\begingroup\$

CJam, 15 13 characters

'[,65>4f*e_N*

Probably very golf-able; I'm new to CJam and even code golfing.

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

Swift 3, 53

(0...103).map{print(String(UnicodeScalar($0/4+65)!))}

IF one day Apple decided to include Foundation by default, we could have

(0...103).map{print(String(format:"%c",$0/4+65))} //49 bytes

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

Common Lisp, 58 bytes

(dotimes(i 26)(format t"~4@{~c
~:*~}"(code-char(+ 65 i))))

Explanation

(dotimes(i 26) ; loop for i from 0 to 25
(format t"~4@{~c
~:*~} ;output four times character given by (code-char(+ 65 i)) followed by newline

Try it online!

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

Vim, 27 bytes

:h<_
jjYZZPVUqqx4O<C-r>"<esc>jq25@q

Thanks @Lynn for the trick of grabbing the alphabet from helpfiles.

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

Brain-Flak, 122 bytes

(((((()()()()){}){}){}){}())((((()()()){}){}()){}){({}<(()()()()){({}<(({})<((()()()()()){})>)>[()])}{}({}())>[()])}{}{}

this is 120 bytes of source code, and +2 bytes for the -Ar flags.

Try it online!

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

PHP, 36 bytes

while($i<104)echo"
",chr($i++/4+65);

bonus version, 38 bytes

while($i<104)echo"@K"^chr($i++/4+1).A;

Run with -nr or try it online.

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

Mathematica 50 41 30

With help from alephalpha and Mark S.

Print/@{#,#,#,#}&/@Alphabet[];
\$\endgroup\$
  • 1
    \$\begingroup\$ Print /@ {#, #, #, #} & /@ "A"~CharacterRange~"Z"; \$\endgroup\$ – alephalpha Dec 14 '13 at 7:27
  • \$\begingroup\$ The semicolon in @alephalpha's comment is necessary. Also, using a newer version of Mathematica you can use Print/@{#,#,#,#}&/@Alphabet[]; instead of Print/@{#,#,#,#}&/@"A"~CharacterRange~"Z"; \$\endgroup\$ – Mark S. Jul 25 '17 at 23:08
2
\$\begingroup\$

Pyth, 8 bytes

-2 bytes thanks to Mr. Xcoder

VGj*4rN1

Try it online!

Explanation

VG          # For N in G (the alphabet)...
     rN1    # ...convert N to uppercase...
   *4       # ...generate the string NNNN...
  j         # ...join on newlines
            # Implicit print for each iteration
\$\endgroup\$
  • 1
    \$\begingroup\$ 8 bytes: VGj*4rN1 \$\endgroup\$ – Mr. Xcoder Aug 20 '17 at 20:29
  • \$\begingroup\$ VrG1NNNN is also 8 bytes and is way simpler... \$\endgroup\$ – hakr14 Mar 24 '18 at 16:11
2
\$\begingroup\$

Perl 6, 22 bytes

.say xx 4 for "A".."Z"

Even though this is an old challenge, I found a different approach to the Perl 6 answer, so I decided to post it.

Try it online!

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

///, 73 bytes

/./\/A
A
A
A
\/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.

Try It Online!

This just replace . by /AAAA/A/ to get AAAA/A/B/AAAA/A/C/AAAA... (newlines excluded for legibility). The /~/ is the first part of a dummy replacement which does nothing; it's there to grab initial / from the first replacement. We don't need to worry about the final /A/ (/Z/ by the time the interpreter gets there), because /// simply exits if it encounters EOF while reading a replacement

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

Whitespace, 126 117 bytes

[S S S T    S S S S S S N
_Push_64][S N
S _Duplicate][N
S S S N
_Create_Label_LOOP][S N
N
_Discard_top][S S S T   N
_Push_1][T  S S S _Add][S N
S _Duplicate][S N
S _Duplicate][S S S T   S T T   S T T   N
_Push_91][T S S T   _Subtract][N
T   S S S N
_Jump_to_Label_EXIT_if_0][S S S T   S S N
_Push_4][N
S S T   N
_Create_Label_INNER_LOOP][S S S T   N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   T   S N
_Jump_to_Label_LOOP_if_negative][S N
T   _Swap_top_two][S N
S _Duplicate][T N
S S _Print_as_character][S S S T    S T S N
_Push_10][T N
S S _Print_as_character][S N
T   _Swap_top_two][N
S N
T   N
_Jump_to_Label_INNER_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (containing raw spaces, tabs and new-lines only).

General explanation in pseudo-code:

Integer i = 64
Start Loop:
  i = i+1
  If i == 91: Exit program with error
  Integer j = 4
  Start Inner Loop:
    j = j-1
    If j is negative: Go to the next iteration of the outer Loop
    Print i as character
    Print new-line
    Go to the next iteration of the Inner Loop

Example run:

Command        Explanation                            Stack            STDOUT    STDERR

SSSTSSSSSSN    Push 64                                [64]
SNS            Duplicate top (64)                     [64,64]
NSSSN          Create Label_LOOP                      [64,64]
 SNN            Discard top                           [64]
 SSSTN          Push 1                                [64,1]
 TSSS           Add (64+1)                            [65]
 SNS            Duplicate top (65)                    [65,65]
 SNS            Duplicate top (65)                    [65,65,65]
 SSSTSTTSTTN    Push 91                               [65,65,65,91]    
 TSST           Subtract (65-91)                      [65,65,-26]
 NTSSSN         If 0: Jump to Label_EXIT              [65,65]
 SSSTSSN        Push 4                                [65,65,4]
 NSSTN          Create Label_INNER_LOOP               [65,65,4]
  SSSTN          Push 1                               [65,65,4,1]
  TSST           Subtract (4-1)                       [65,65,3]
  SNS            Duplicate top (3)                    [65,65,3,3]
  NTTSN          If negative: Jump to Label_LOOP      [65,65,3]
  SNT            Swap top two                         [65,3,65]
  SNS            Duplicate top (65)                   [65,3,65,65]
  TNSS           Print as character                   [65,3,65]        A
  SSSTSTSN       Push 10                              [65,3,65,10]
  TNSS           Print as character                   [65,3,65]        \n
  SNT            Swap top two                         [65,65,3]
  NSNTN          Jump to Label_INNER_LOOP             [65,65,3]

  SSSTN          Push 1                               [65,65,3,1]
  TSST           Subtract (2-1)                       [65,65,2]
  SNS            Duplicate top (2)                    [65,65,2,2]
  NTTSN          If negative: Jump to Label_LOOP      [65,65,2]
  SNT            Swap top two                         [65,2,65]
  SNS            Duplicate top (65)                   [65,2,65,65]
  TNSS           Print as character                   [65,2,65]        A
  SSSTSTSN       Push 10                              [65,2,65,10]
  TNSS           Print as character                   [65,2,65]        \n
  SNT            Swap top two                         [65,65,2]
  NSNTN          Jump to Label_INNER_LOOP             [65,65,2]

  SSSTN          Push 1                               [65,65,2,1]
  TSST           Subtract (2-1)                       [65,65,1]
  SNS            Duplicate top (1)                    [65,65,1,1]
  NTTSN          If negative: Jump to Label_LOOP      [65,65,1]
  SNT            Swap top two                         [65,1,65]
  SNS            Duplicate top (65)                   [65,1,65,65]
  TNSS           Print as character                   [65,1,65]        A
  SSSTSTSN       Push 10                              [65,1,65,10]
  TNSS           Print as character                   [65,1,65]        \n
  SNT            Swap top two                         [65,65,1]
  NSNTN          Jump to Label_INNER_LOOP             [65,65,1]

  SSSTN          Push 1                               [65,65,1,1]
  TSST           Subtract (1-1)                       [65,65,0]
  SNS            Duplicate top (1)                    [65,65,0,0]
  NTTSN          If negative: Jump to Label_LOOP      [65,65,0]
  SNT            Swap top two                         [65,0,65]
  SNS            Duplicate top (65)                   [65,0,65,65]
  TNSS           Print as character                   [65,0,65]        A
  SSSTSTSN       Push 10                              [65,0,65,10]
  TNSS           Print as character                   [65,0,65]        \n
  SNT            Swap top two                         [65,65,0]
  NSNTN          Jump to Label_INNER_LOOP             [65,65,0]

  SSSTN          Push 1                               [65,65,0,1]
  TSST           Subtract (0-1)                       [65,65,-1]
  SNS            Duplicate top (-1)                   [65,65,-1,-1]
  NTTSN          If negative: Jump to Label_LOOP      [65,65,-1]

 (Note: Contains additional trailing `[65,` here, but we'll ignore that for now.)
 SNS            Discard top                           [65]
 SSSTN          Push 1                                [65,1]
 TSSS           Add (65+1)                            [66]
 SNS            Duplicate top (66)                    [66,66]
 SNS            Duplicate top (66)                    [66,66,66]
 SSSTSTTSTTN    Push 91                               [66,66,66,91]    
 TSST           Subtract (66-91)                      [66,66,-25]
 NTSSSN         If 0: Jump to Label_EXIT              [66,66]
 SSSTSSN        Push 4                                [66,66,4]
 NSSTN          Create Label_INNER_LOOP               [66,66,4]
  SSSTN          Push 1                               [66,66,4,1]
  TSST           Subtract (4-1)                       [66,66,3]
  SNS            Duplicate top (3)                    [66,66,3,3]
  NTTSN          If negative: Jump to Label_LOOP      [66,66,3]
  SNT            Swap top two                         [66,3,66]
  SNS            Duplicate top (66)                   [66,3,66,66]
  TNSS           Print as character                   [66,3,66]        B
  SSSTSTSN       Push 10                              [66,3,66,10]
  TNSS           Print as character                   [66,3,66]        \n
  SNT            Swap top two                         [66,66,3]
  NSNTN          Jump to Label_INNER_LOOP             [66,66,3]

  ... etc. etc. for all letters in the alphabet

 SNN            Discard top                           [90]
 SSSTN          Push 1                                [90,1]
 TSSS           Add (90+1)                            [91]
 SNS            Duplicate top (91)                    [91,91]
 SNS            Duplicate top (91)                    [91,91]
 SSSTSTTSTTN    Push 91                               [91,91,91]    
 TSST           Subtract (91-91)                      [91,91,0]
 NTSSSN         If 0: Jump to Label_EXIT              [91,91]                    error
                (Label_EXIT doesn't exit, so exits program with an error to STDERR)
\$\endgroup\$
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 46 bytes

for(var a=259;a++<363;WriteLine((char)(a/4)));

Try it online!

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

Pyt, 6 bytes

ʊ4*ąşÁ

Explanation:

ʊ     Pushes "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4*    Quadruples the string
ą     Convert to array of characters
ş     Sort the array in ascending order
Á     Push contents of the array onto the stack
      implicit output

Try it online!


If outputting it as an array is acceptable:

Pyt, 5 bytes

ʊ4*ąş
\$\endgroup\$
2
\$\begingroup\$

SmileBASIC, 29 bytes

FOR I=260TO 363?CHR$(I/4)NEXT
\$\endgroup\$
  • \$\begingroup\$ This is a polyglot, and will function in Yabasic - Try it online! \$\endgroup\$ – Taylor Scott Jul 9 '18 at 18:23
2
\$\begingroup\$

VBA, 30 bytes

An anonymous VBE immediate window function that takes no input and outputs to the console.

For i=260To 363:?Chr(i\4):Next
\$\endgroup\$
2
\$\begingroup\$

Japt -R, 7 6 bytes

;B²²¬n

Test it

;B²²¬n
;B         :Uppercase alphabet
  ²        :Duplicate
   ²       :Duplicate
    ¬      :Split
     n     :Sort
           :Implicitly join with newlines
\$\endgroup\$
1
\$\begingroup\$

Matlab, 38

x=repmat(char(65:90)',1,4)';disp(x(:))
\$\endgroup\$
1
\$\begingroup\$

JavaScript (59 56)

for(i=65;i<91;i+=1/4)console.log(String.fromCharCode(i))
\$\endgroup\$
  • \$\begingroup\$ Hey @user113215, you can save 1 extra char by doing i|0 instead of i,10! \$\endgroup\$ – Dom Hastings Dec 14 '13 at 16:09
  • \$\begingroup\$ @DomHastings No, i,10 prints a newline as well as the letter. i|0 is the same as just i. Considering that console.log adds an implicit newline, however, I suppose this could be shortened to just i. \$\endgroup\$ – quietmint Dec 15 '13 at 5:19
  • \$\begingroup\$ Ahhh, i see! Apologies, for some reason I thought it was doing some kind of parseInt! \$\endgroup\$ – Dom Hastings Dec 15 '13 at 9:09
1
\$\begingroup\$

Clojure 41 45

(doseq[i(range 65 91 0.25)](println(char i)))

wrong answer:

(doseq[i(range 65 91 0.25)](prn(char i)))
\$\endgroup\$
  • \$\begingroup\$ except that outputs the letters in reader character syntax (\A, \B, etc). And I don't count leading backslashes as "whitespace". :) Replacing prn with println yields the correct output at a cost of 4 additional characters. \$\endgroup\$ – Mark Reed Dec 16 '13 at 0:42
  • \$\begingroup\$ you'r right, i was too lazy to test it out :D fixed \$\endgroup\$ – Shlomi Dec 16 '13 at 0:52
1
\$\begingroup\$

PHP, 53

for($i=65;$i<91;$i++)echo str_repeat(chr($i)."\n",4);
\$\endgroup\$
  • 3
    \$\begingroup\$ I count 53 chars; where's the 44 from? \$\endgroup\$ – Mark Reed Dec 14 '13 at 5:01
  • \$\begingroup\$ I know a 44 character long solution, but definitely not with that str_repeat(): pastebin.com/decsAZXi \$\endgroup\$ – manatwork Dec 14 '13 at 12:58
  • \$\begingroup\$ @manatwork - that looks like 40, even. Why not post it as an answer? \$\endgroup\$ – Mark Reed Dec 15 '13 at 1:27
  • \$\begingroup\$ Sorry, I didn't count the $i \$\endgroup\$ – ub3rst4r Dec 15 '13 at 3:02
  • \$\begingroup\$ It doesn't includes ZZZZ \$\endgroup\$ – VarunAgw Dec 15 '13 at 12:16
1
\$\begingroup\$

LINQ, 59 bytes

from i in Enumerable.Range(0, 104)select(char)(65+i/4)+"\n"

LINQ expression, try it with LinqPad. The select part is a bit too C#ist but I guess that fine.

from i                        // for each variable
in Enumerable.Range(0, 104)   // in the range 0, 26*4
select(char)(65+i/4)+"\n"     // get the letter and a newline
\$\endgroup\$
1
\$\begingroup\$

C#, 86 bytes

class P{static void M(){for(var i=65d;i<91;i+=.25)System.Console.WriteLine((char)i);}}

a full program...

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

C#, 88 bytes

var s="";int i,j;for(i=0;i++<26;){for(j=0;j++<4;)s+=(char)(i+64)+"\n";}Console.Write(s);

Try it with this REPL shell: https://csharppad.com/

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

05AB1E, 5 bytes

Aε4F=

Try it online!

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

Lua, 53 Bytes

Simple solution, we're using the range [260,363] which is 65*4 and 90*4+3 (letter A and Z) to iterate.

Then we just have to do an euclidian division by 4 and print out each letter 4 times this way.

Edit: Approved @MCAdventure10's edit that pointed out a rounding error that made only one Z being printed out, it wasn't deviating from my original idea and didn't change the byte count, thus,I approved it.

for i=260,363 do print(("").char(math.floor(i/4)))end
\$\endgroup\$
1
\$\begingroup\$

Funky, 32 bytes

fori=0i<104i++print("%c"%65+i/4)

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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