27
\$\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

84 Answers 84

16
\$\begingroup\$

APL (5)

⍪4/⎕A

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Seems unlikely to be beaten. :) \$\endgroup\$ – Mark Reed Dec 17 '13 at 12:53
  • \$\begingroup\$ I count 9 bytes. \$\endgroup\$ – Oliver Ni Oct 23 '16 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 '16 at 4:08
  • \$\begingroup\$ @StevenH. Link is dead \$\endgroup\$ – Stan Strum Mar 25 '18 at 22:59
  • \$\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 '18 at 2:11
10
\$\begingroup\$

Python - 37

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

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\$
  • \$\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 '13 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 '13 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 '13 at 8:27
  • 3
    \$\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 '13 at 19:48
  • 3
    \$\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 '14 at 20:01
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\$
  • \$\begingroup\$ I've edited my answer, now saving even 2 more chars! \$\endgroup\$ – moala Dec 17 '13 at 1:37
  • \$\begingroup\$ and another one! \$\endgroup\$ – moala Dec 17 '13 at 2:42
7
\$\begingroup\$

R, 30 28 27

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

Former version with 30 bytes:

cat(rep(LETTERS,e=4),sep="\n")
\$\endgroup\$
  • \$\begingroup\$ I think a literal newline is a byte shorter :) \$\endgroup\$ – Giuseppe Jul 25 '17 at 15:31
  • \$\begingroup\$ @Giuseppe Can you specify this idea? \$\endgroup\$ – Sven Hohenstein Jul 25 '17 at 15:34
  • 1
    \$\begingroup\$ Try it online! \$\endgroup\$ – Giuseppe Jul 25 '17 at 15:36
  • \$\begingroup\$ @Giuseppe Really good idea! Thanks for pointing out. \$\endgroup\$ – Sven Hohenstein Jul 25 '17 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 '18 at 12:53
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
    \$\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 '13 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 '13 at 1:55
  • 1
    \$\begingroup\$ 4#65{26,.\a. for 12 bytes. \$\endgroup\$ – FrownyFrog Oct 29 '17 at 6:59
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
    \$\begingroup\$ putStr$['A'..'Z']>>=("golf">>).(:"\n") saves 8 bits \$\endgroup\$ – Angs Oct 23 '16 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\$
4
\$\begingroup\$

Ruby, 23

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

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

\$\endgroup\$
  • \$\begingroup\$ Huge. @manatwork, I'll make the edit but obviously I don't deserve any credit! \$\endgroup\$ – Darren Stone Dec 13 '13 at 19:01
  • 4
    \$\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 '13 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 '13 at 19:08
4
\$\begingroup\$

GolfScript: 17 15 characters

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

Perl 5, 21

map{print"$_
"x4}A..Z
\$\endgroup\$
  • 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 '13 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\$ – Dom Hastings Dec 14 '13 at 9:00
4
\$\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\$
  • 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 '13 at 22:44
  • \$\begingroup\$ Great! Thanks! Answer edited! \$\endgroup\$ – moala Dec 14 '13 at 10:39
  • 1
    \$\begingroup\$ You can replace >>2 by /4. \$\endgroup\$ – marinus Dec 17 '13 at 2:37
4
\$\begingroup\$

Java: 56

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

edit: changed from 'print' to 'println'

\$\endgroup\$
  • \$\begingroup\$ The output should include newlines after each letter. \$\endgroup\$ – Pierre Arlaud Dec 17 '13 at 14:14
  • \$\begingroup\$ thanks for pointing that out, it should print a new line each time now \$\endgroup\$ – reblerebel Dec 17 '13 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 '16 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\$ – David Conrad Jul 20 '16 at 12:09
  • \$\begingroup\$ @DavidConrad Its awesome then, I think I might start using REPL/JShell. \$\endgroup\$ – user902383 Jul 20 '16 at 12:13
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
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Erik the Outgolfer Jul 21 '16 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\$
3
\$\begingroup\$

BrainF* ,79 60

+++++++++++++[->++>+>+++++<<<]>>---<[->>>++++[-<.<.>>]<+<<]
\$\endgroup\$
  • 4
    \$\begingroup\$ +++++++++++++[>+>+++++>++<<<-]>--->>[<.<.>.<.>.<.>.<.>+>-] \$\endgroup\$ – alephalpha Dec 14 '13 at 7:32
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\$
  • \$\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 '13 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 '13 at 2:36
  • \$\begingroup\$ @MarkReed Your code isn't working. Check this \$\endgroup\$ – Wasi Dec 15 '13 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 '13 at 22:00
3
\$\begingroup\$

PowerShell, 21

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

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

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

Forth, 37

'h 0 [do] [i] 4 / 'A + emit cr [loop]
\$\endgroup\$
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
    \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$ – Rɪᴋᴇʀ Jul 20 '16 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\$
3
\$\begingroup\$

Jelly, 5 bytes (non-competing?)

Ø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\$

Perl 6, 32

.say for (('A'..'Z') »xx»4)[*;*]
\$\endgroup\$
  • \$\begingroup\$ I think this is the first time that Perl 6 was the first solution I thought of, but the hyperoperator just seemed a natural fit. \$\endgroup\$ – Mark Reed Dec 15 '13 at 4:43
3
\$\begingroup\$

Canvas, 5 bytes

Z41*⟳

Try it here!

Explanation:
Code        | Explanation                                  | Stack
------------+----------------------------------------------+------------------------------
Z          | The uppercase alphabet                       | "ABC..."
  41*     | Stretched by 4 horizontally and 1 vertically | "AAAABBBBCCCC..."
        ⟳  | Rotated clockwise                            | "A¶A¶A¶A¶B¶B¶B¶B¶C¶C¶C¶C¶..."
            | Print ToS (implicit)                         |

With replaced with \n upon printing.

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

Kotlin, 66 59 bytes

Save 7 bytes removing for loop.

fun main(a:Array<String>){repeat(104){println('A'+(it/4))}}

Try it online!

\$\endgroup\$
3
\$\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\$
  • \$\begingroup\$ I was about to post a new one, but you managed to completely out-do my best, i got 71 bytes with ++++++[->++>++++>++++++++++<<<]++++>+>++>+++++<[->.<<.>>.<<.>>.<<.>>.+<<.>] \$\endgroup\$ – KrystosTheOverlord Jan 31 at 15:17
2
\$\begingroup\$

Bash: 24 characters

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

Dc: 35 characters

[rdP10Pr1-d1<p]sp65[5lpx+d91>l]dslx
\$\endgroup\$
2
\$\begingroup\$

Scala, 42

('A'to'Z')map(x=>List.fill(4)(println(x)))
\$\endgroup\$
2
\$\begingroup\$

Julia, 39, 36

println([char(i/4+64.5)for i=0:103])
\$\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.