36
\$\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.

Lowercase letters and/or extra whitespace are acceptable.

The solution must be a complete program.

This is so answers will be scored in bytes with a lower score being the goal.

\$\endgroup\$
5
  • 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, 2013 at 17:26
  • \$\begingroup\$ And do I have to output only the alphabet? \$\endgroup\$
    – Justin
    Dec 13, 2013 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, 2013 at 17:44
  • 4
    \$\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, 2013 at 17:46
  • 1
    \$\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, 2013 at 19:44

109 Answers 109

18
\$\begingroup\$

APL (5)

⍪4/⎕A

Matrix format () of 4-replication (4/) of alphabet (⎕A).

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Seems unlikely to be beaten. :) \$\endgroup\$
    – Mark Reed
    Dec 17, 2013 at 12:53
  • 1
    \$\begingroup\$ I count 9 bytes. \$\endgroup\$
    – Oliver Ni
    Oct 23, 2016 at 4:06
  • \$\begingroup\$ APL uses a code-page that maps each of the characters it uses to one byte. This code page can be found here, on IBM's website. \$\endgroup\$
    – Steven H.
    Oct 23, 2016 at 4:08
  • \$\begingroup\$ @StevenH. Link is dead \$\endgroup\$
    – Stan Strum
    Mar 25, 2018 at 22:59
  • 1
    \$\begingroup\$ @StanStrum I believe the link is dead because of deprecation, but for the sake of code golfing this meta answer should work. \$\endgroup\$
    – Steven H.
    Mar 27, 2018 at 2:11
12
\$\begingroup\$

Python 2 - 37

for i in range(104):print chr(i/4+65)

As i goes from 0 to 104, it is divided by four and added to the ASCII value for A, and the resulting character is printed.

\$\endgroup\$
6
  • \$\begingroup\$ I take it Python uses integer division by default? Would kinda be nice if PowerShell did right now. As it is, the code necessary to force it into integer division is too long for this trick to help me save anything on my script. \$\endgroup\$
    – Iszi
    Dec 13, 2013 at 17:42
  • \$\begingroup\$ @lszi - Python takes its cue from the type of the operands. 3/4 is 0, while 3.0/4.0 is 0.75; range() generates integers. \$\endgroup\$
    – Mark Reed
    Dec 13, 2013 at 17:43
  • 1
    \$\begingroup\$ This does not work on newer versions of python. First, the print function must be called with brackets, and / no longer does integer division by default (even if both numbers are integers), which is //'s job Try: for i in range(104):print(chr(i//4+65)) \$\endgroup\$
    – user10979
    Dec 14, 2013 at 8:27
  • 4
    \$\begingroup\$ @Consciousness I know that. I deliberately chose to use an old version so that it can be golfed better. If you were to run this at Ideone.com, you'd choose "Python" instead of "Python 3" \$\endgroup\$
    – Justin
    Dec 14, 2013 at 19:48
  • 4
    \$\begingroup\$ @Consciousness - by "newer versions of python", you're referring to "Python 3", which is far from universally adopted at this point. I generally assume that anything claiming to be "Python" without a specified version is Python 2.x until proven otherwise; Python 3 code tends to be explicitly so labeled. \$\endgroup\$
    – Mark Reed
    Apr 10, 2014 at 20:01
9
\$\begingroup\$

R, 30 28 27 25

write(rep(LETTERS,e=4),1)

Former version with 30 bytes:

cat(rep(LETTERS,e=4),sep="\n")
\$\endgroup\$
8
  • \$\begingroup\$ I think a literal newline is a byte shorter :) \$\endgroup\$
    – Giuseppe
    Jul 25, 2017 at 15:31
  • \$\begingroup\$ @Giuseppe Can you specify this idea? \$\endgroup\$ Jul 25, 2017 at 15:34
  • 1
    \$\begingroup\$ Try it online! \$\endgroup\$
    – Giuseppe
    Jul 25, 2017 at 15:36
  • \$\begingroup\$ @Giuseppe Really good idea! Thanks for pointing out. \$\endgroup\$ Jul 25, 2017 at 15:37
  • \$\begingroup\$ You can use 1 instead of "" to specify stdout in write as well, which saves another byte. \$\endgroup\$
    – Giuseppe
    Mar 16, 2018 at 12:53
7
\$\begingroup\$

C, 59

I submit this, an uncompetitively long answer, simply because I don't see a C submission yet. And that makes me sad. :-/

LATER: Props to @moala for doing a "/4" int version of this, saving 13 chars!

float i;main(){while(i<26)printf("%c\n",65+(int)i),i+=.25;}
\$\endgroup\$
2
  • \$\begingroup\$ I've edited my answer, now saving even 2 more chars! \$\endgroup\$
    – moala
    Dec 17, 2013 at 1:37
  • \$\begingroup\$ and another one! \$\endgroup\$
    – moala
    Dec 17, 2013 at 2:42
6
\$\begingroup\$

J: 18 13

4#u:65+i.26 1

I'm still pretty shaky with J, so this could probably be improved

\$\endgroup\$
3
  • 3
    \$\begingroup\$ You can use replicate (#) instead of division like so: 4#u:65+i.26 1. Also, ~ swaps a function's arguments, so if you ever find yourself doing (expression) F value, you can replace that with value F~ expression to save a character. \$\endgroup\$
    – marinus
    Dec 14, 2013 at 1:34
  • \$\begingroup\$ @marinus Thanks for the tip. I'm still learning J and it's hard to find any good info with those kinds of tricks. \$\endgroup\$
    – p.s.w.g
    Dec 14, 2013 at 1:55
  • 2
    \$\begingroup\$ 4#65{26,.\a. for 12 bytes. \$\endgroup\$
    – FrownyFrog
    Oct 29, 2017 at 6:59
5
\$\begingroup\$

Ruby, 23

puts ([*?A..?Z]*4).sort

All credit to @manatwork -- upvote his comment, not this. :)

\$\endgroup\$
3
  • \$\begingroup\$ Huge. @manatwork, I'll make the edit but obviously I don't deserve any credit! \$\endgroup\$ Dec 13, 2013 at 19:01
  • 6
    \$\begingroup\$ Better make it puts [*?A..?Z].map{|i|[i]*4} or puts ([*?A..?Z]*4).sort, so the letters get ordered as in the example. \$\endgroup\$
    – manatwork
    Dec 13, 2013 at 19:02
  • 2
    \$\begingroup\$ @manatwork: puts (?A..?Z).map{|i|[i]*4} is a character shorter. You can call map directly on a Range, so you don't need the splat in this case. \$\endgroup\$
    – Mark Reed
    Dec 13, 2013 at 19:08
5
\$\begingroup\$

PowerShell: 32 23

Golfed code:

[char[]](65..90*4)|Sort

Walkthrough:

[char[]](...) takes an array of objects and converts them to ASCII characters.
65..90 are the ASCII codes for A-Z.
*4 repeats the series 4 times.
|Sort sorts the output.

Note:

If you want this written to a file, just throw >, followed by a file name, at the end.

\$\endgroup\$
5
\$\begingroup\$

Haskell, 46

x a=a++a
main=putStr$['A'..'Z']>>=x.x.(:"\n")
\$\endgroup\$
1
  • 1
    \$\begingroup\$ putStr$['A'..'Z']>>=("golf">>).(:"\n") saves 8 bits \$\endgroup\$
    – Angs
    Oct 23, 2016 at 17:03
5
\$\begingroup\$

Befunge 98 - 18

1+::'g`#@_4/'A+,a,

Works by storing a number and ending when it reaches 104. Prints out the corresponding character of the alphabet for the number divided by 4, followed by a newline. But if I need not add a newline after each letter, then it is 16 chars:

1+::'g`#@_4/'A+,

Can be reduced if I can print more characters (ie all of them four times)(7 6 chars, even works in Befunge 93):

1+:4/,

With newline:

1+:4/,a,
\$\endgroup\$
0
5
\$\begingroup\$

C, 46 44 43

46:

i;main(){while(i<104)printf("%c\n",65+i++/4);}

44:

i=260;main(j){for(;(j=i++>>2)<91;puts(&j));}

44 too:

i=260;main(j){while(j=i++>>2,j<91)puts(&j);}

Thanks to @marinus, 43:

i=260;main(j){while(j=i++/4,j<91)puts(&j);}

Should I add a bounty for getting to 42? :)

\$\endgroup\$
3
  • 6
    \$\begingroup\$ 50 rep. is needed to comment on anything, and you have 101 at the time of posting this comment. \$\endgroup\$
    – syb0rg
    Dec 13, 2013 at 22:44
  • 1
    \$\begingroup\$ You can replace >>2 by /4. \$\endgroup\$
    – marinus
    Dec 17, 2013 at 2:37
  • \$\begingroup\$ 42 bytes Can I get the bounty :-) \$\endgroup\$
    – jdt
    Sep 15, 2021 at 16:39
5
\$\begingroup\$

Java: 56

for(int i=0;i<104;)System.out.println((char)(i++/4+65));

edit: changed from 'print' to 'println'

\$\endgroup\$
6
  • \$\begingroup\$ The output should include newlines after each letter. \$\endgroup\$ Dec 17, 2013 at 14:14
  • \$\begingroup\$ thanks for pointing that out, it should print a new line each time now \$\endgroup\$
    – reblerebel
    Dec 17, 2013 at 19:51
  • 2
    \$\begingroup\$ 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. i think your solution violates this condition \$\endgroup\$
    – user902383
    Jul 20, 2016 at 11:58
  • 2
    \$\begingroup\$ @user902383 If you paste it into JShell (the Java REPL in Java 9) it works, and you don't even need the final semicolon. \$\endgroup\$ Jul 20, 2016 at 12:09
  • \$\begingroup\$ @DavidConrad Its awesome then, I think I might start using REPL/JShell. \$\endgroup\$
    – user902383
    Jul 20, 2016 at 12:13
5
\$\begingroup\$

brainfuck, 48 bytes

+++[[-<+>>++<]+>]<<<<<<+[->,++++[->.<<<.>>]>+<<]

Try it online!

Prints in lowercase, separated by carriage returns. Uses wrapping 8 bit cells as well as cells left of the origin, though you can prepend a > to counter the latter.

\$\endgroup\$
1
  • \$\begingroup\$ I was about to post a new one, but you managed to completely out-do my best, i got 71 bytes with ++++++[->++>++++>++++++++++<<<]++++>+>++>+++++<[->.<<.>>.<<.>>.<<.>>.+<<.>] \$\endgroup\$ Jan 31, 2019 at 15:17
4
\$\begingroup\$

GolfScript: 17 15 characters

26,{65+...}%+n*
\$\endgroup\$
4
\$\begingroup\$

Bash: 24 characters

printf %s\\n {A..Z}{,,,}
\$\endgroup\$
4
\$\begingroup\$

BrainF* ,79 60

+++++++++++++[->++>+>+++++<<<]>>---<[->>>++++[-<.<.>>]<+<<]
\$\endgroup\$
1
  • 4
    \$\begingroup\$ +++++++++++++[>+>+++++>++<<<-]>--->>[<.<.>.<.>.<.>.<.>+>-] \$\endgroup\$
    – alephalpha
    Dec 14, 2013 at 7:32
4
\$\begingroup\$

Forth, 37

'h 0 [do] [i] 4 / 'A + emit cr [loop]
\$\endgroup\$
1
  • \$\begingroup\$ 35 bytes by avoiding +. \$\endgroup\$
    – Bubbler
    Oct 12, 2019 at 8:41
4
\$\begingroup\$

Actually, 6 bytes

4ú*SÖi

Try it here!

Explanation

4ú*SÖi

4 *         Do 4 times
 ú          Create string of alphabet in lowercase
   S        Sort it
    Ö       Switch Case
     i      Push each character of string

4 Bytes with lowercase and no newline:

4ú*S
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Jul 21, 2016 at 9:56
4
\$\begingroup\$

16-bit x86 machine code MS-DOS COM, 25 bytes

In hex:

B409BA160189D7B96800F6C1037502FE05CD21E2F5C3400A24

This is a complete MS-DOS .COM program. Copy the byte sequence to the file with .com extension and run it from DOSBox

Disassembly:

00: B4 09        mov    ah,0x09         ;INT 21h "Write string to STDOUT" function
02: BA 16 01     mov    dx,0x116        ;Address of the string s ('$'-terminated)
05: 89 D7        mov    di,dx           ;Because there's no way to dereference address in DX
07: B9 68 00     mov    cx,104          ;CX=26*4
_0000000A:
0A: F6 C1 03     test   cl,0x03         ;When lower two bits are zero...
0D: 75 02        jne    _00000011       ;...do not skip the next instruction
0F: FE 05        inc    b,[di]          ;*s++
_00000011:
11: CD 21        int    21              ;Print the string
13: E2 F5        loop   _0000000A       ;Until --CX==0
15: C3           retn
16: 40           db     0x40            ;s[0], starts with 'A'-1
17: 0A           db     0x0A            ;'\n'
18: 24           db     '$'             ;Terminator required by the print function
\$\endgroup\$
0
4
\$\begingroup\$

Perl 5, 20 bytes

print"$_
"x4for A..Z

Try it online!


Perl 5 + -M5.10.0, 20 bytes

eval"say;"x4for A..Z

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ It has never occurred to me to put a literal newline inside a double-quoted string in Perl. +1. \$\endgroup\$
    – Mark Reed
    Dec 14, 2013 at 4:44
  • \$\begingroup\$ I should note, that I didn't either, but @manatwork mentioned it on another answer of mine and it's stuck! \$\endgroup\$ Dec 14, 2013 at 9:00
4
\$\begingroup\$

x86-16 ASM, IBM PC DOS, 22 19 bytes

Binary:

00000000: 400a 24b8 1109 8bd6 b168 d0c8 102c cd21  @.$......h...,.!
00000010: e2f8 c3                                  ...

Build with xxd -r and test in DOSBox or your favorite DOS VM.

Unassembled:

40 0A 24    DB   '@', 0AH, '$'      ; the letter + NL + '$' string terminator
B8 0911     MOV  AX, 0911H          ; AH = DOS print string; AL = 0001 0001 bit pattern
8B D6       MOV  DX, SI             ; DX to output string
B1 68       MOV  CL, 26*4           ; Loop 4 times for each letter in alphabet
        ALOOP:
D0 C8       ROR  AL, 1              ; roll repeating bit pattern to the right
10 2C       ADC  BYTE PTR[SI], CH   ; add rightmost bit to ASCII character
CD 21       INT  21H                ; print letter to console
E2 F8       LOOP ALOOP              ; continue looping
C3          RET                     ; return to DOS

Notes:

For most flavors of DOS, we know the values of many CPU registers at the start of execution and we know SI is 0100H, and CH is 0. So by placing the output string in the beginning of the program, [SI] will already be pointing to it. These bytes ('@',0AH,'$') decode to two benign instructions (INC AX, OR AH, [SI]), which have no effect on the program.

AL contains the bit pattern of 0001 0001 which is bit rolled right each time and the rightmost bit added to the ASCII value of the output char in memory.

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

Vyxal j, 5 bytes

kAdds

Try it Online!

Look ma, no Unicode!

kA    # Uppercase output
  dd  # Quadruple
    s # Sorted
      # (j flag) join by newlines
\$\endgroup\$
2
  • 2
    \$\begingroup\$ the explanation here is wrong: clearly, the Adds here is what is adding everything 4 times ;) \$\endgroup\$
    – lyxal
    Jul 8, 2021 at 12:55
  • \$\begingroup\$ @lyxal Honestly, this is the first time I've unintentionally done a letters-only answer... \$\endgroup\$
    – emanresu A
    Jul 8, 2021 at 20:03
4
\$\begingroup\$

Jelly, 5 bytes

ØAx4Y

Try it online!

Explanation:

ØAx4Y Main link
ØA    “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
   4  4
  x   Repeat each element of x y times
    Y Join x with newlines
\$\endgroup\$
3
\$\begingroup\$

Q (13)

.........

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

AWK, 48

Lets try it with AWK...

END{s=65;for(i=104;i--;s+=0==i%4)printf"%c\n",s}

As suggested by manatwork we can get rid of 2 chars

AWK, 46 (Edit)

END{for(i=104;i--;s+=0==i%4)printf"%c\n",s+65}

AWK,40 (editing MarkReed's code)

END{for(;i<104;){printf"%c\n",i++/4+65}}
\$\endgroup\$
4
  • \$\begingroup\$ By removing the initialization of variable s you can spare 2 characters: END{for(i=104;i--;s+=0==i%4)printf"%c\n",s+65}. \$\endgroup\$
    – manatwork
    Dec 14, 2013 at 20:01
  • 1
    \$\begingroup\$ Putting the code in an END block means it requires an input stream (even if it's /dev/null) to work. Does that modify the char count? Anyway, BEGIN{for(;++i<104;){printf"%c\n",i/4+65}} is 5 chars shorter. \$\endgroup\$
    – Mark Reed
    Dec 15, 2013 at 2:36
  • \$\begingroup\$ @MarkReed Your code isn't working. Check this \$\endgroup\$
    – Wasi
    Dec 15, 2013 at 9:03
  • \$\begingroup\$ D'oh. So close! :) But I still don't like the END pattern's requirement for an input stream... \$\endgroup\$
    – Mark Reed
    Dec 15, 2013 at 22:00
3
\$\begingroup\$

PowerShell, 21

65..90|%{,[char]$_*4}

A slightly different approach to Iszi's. And shorter :-)

\$\endgroup\$
1
  • \$\begingroup\$ Damn, I got close to this at 22 using what looks like a blend of y'all's: 65..90|%{[char[]]$_*4} \$\endgroup\$ Oct 24, 2022 at 17:02
3
\$\begingroup\$

C# LINQ 115 Bytes110 Bytes

Enumerable.Range(65, 26).SelectMany(i => Enumerable.Repeat(i,4))
.ToList().ForEach(i=> Console.WriteLine((char)i));
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$
    – Riker
    Jul 20, 2016 at 14:09
3
\$\begingroup\$

05AB1E, 6 bytes

A4×{S»

Explanation:

A       # Push 'abcdefghijklmnopqrstuvwxyz'
 4×     # Repeat four times
   {    # Sort
    S   # Split into list
     »  # Join by newlines
        # Implicit print

Without newlines, 4 bytes

A4×{

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ you beat me by >5 years :// \$\endgroup\$
    – Makonede
    Feb 25, 2022 at 19:01
3
\$\begingroup\$

05AB1E, 5 bytes

Aε4F=

Try it online!

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

Mathematica 50 41 30

With help from alephalpha and Mark S.

Print/@{#,#,#,#}&/@Alphabet[];
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Print /@ {#, #, #, #} & /@ "A"~CharacterRange~"Z"; \$\endgroup\$
    – alephalpha
    Dec 14, 2013 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, 2017 at 23:08
  • \$\begingroup\$ Echo/@{#,#,#,#}&/@Alphabet[]; shaves another character. Theoretically, #+0{,,,} might let you get to 28 (since it's one shorter than {#,#,#,#}), but I couldn't get the precedence to work. \$\endgroup\$
    – Romanp
    May 10, 2022 at 15:57
3
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$

Your Answer

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

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