Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
4  
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? –  Iszi Dec 13 '13 at 17:26
    
And do I have to output only the alphabet? –  Quincunx Dec 13 '13 at 17:33
    
@MarkReed Do I need to print it with newlines in between? Why not just print it, but newlines optional? –  Quincunx Dec 13 '13 at 17:44
2  
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. –  Quincunx Dec 13 '13 at 17:46
    
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? –  Iszi Dec 13 '13 at 19:44

34 Answers 34

up vote 13 down vote accepted

APL (5)

⍪4/⎕A

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

share|improve this answer
1  
Seems unlikely to be beaten. :) –  Mark Reed Dec 17 '13 at 12:53

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.

share|improve this answer
    
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. –  Iszi Dec 13 '13 at 17:42
    
@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. –  Mark Reed Dec 13 '13 at 17:43
1  
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)) –  Consciousness Dec 14 '13 at 8:27
3  
@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" –  Quincunx Dec 14 '13 at 19:48
    
@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. –  Mark Reed Apr 10 at 20:01

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;}
share|improve this answer
    
+1 for golfing in C! –  syb0rg Dec 13 '13 at 22:40
    
I've edited my answer, now saving even 2 more chars! –  moala Dec 17 '13 at 1:37
    
and another one! –  moala Dec 17 '13 at 2:42

J: 18 13

4#u:65+i.26 1

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

share|improve this answer
3  
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. –  marinus Dec 14 '13 at 1:34
    
@marinus Thanks for the tip. I'm still learning J and it's hard to find any good info with those kinds of tricks. –  p.s.w.g Dec 14 '13 at 1:55

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,
share|improve this answer
1  
The best part about it is that this is very readable Befunge code. Anyone who knows Befunge syntax and execution style should find it easy to see what this does. –  Quincunx Dec 14 '13 at 2:11

Ruby, 23

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

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

share|improve this answer
    
Huge. @manatwork, I'll make the edit but obviously I don't deserve any credit! –  Darren Stone Dec 13 '13 at 19:01
4  
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. –  manatwork Dec 13 '13 at 19:02
1  
@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. –  Mark Reed Dec 13 '13 at 19:08

Perl 5, 21

map{print"$_
"x4}A..Z
share|improve this answer
2  
It has never occurred to me to put a literal newline inside a double-quoted string in Perl. +1. –  Mark Reed Dec 14 '13 at 4:44
    
I should note, that I didn't either, but @manatwork mentioned it on another answer of mine and it's stuck! –  Dom Hastings Dec 14 '13 at 9:00

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? :)

share|improve this answer
6  
50 rep. is needed to comment on anything, and you have 101 at the time of posting this comment. –  syb0rg Dec 13 '13 at 22:44
    
Great! Thanks! Answer edited! –  moala Dec 14 '13 at 10:39
1  
You can replace >>2 by /4. –  marinus Dec 17 '13 at 2:37

Perl 6, 23

.say for("A".."Z")»xx»4
share|improve this answer
    
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. –  Mark Reed Dec 15 '13 at 4:43

R, 30

cat(rep(LETTERS,e=4),sep="\n")
share|improve this answer

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.

share|improve this answer

GolfScript: 17 15 characters

26,{65+...}%+n*
share|improve this answer

Haskell, 46

x a=a++a
main=putStr$['A'..'Z']>>=x.x.(:"\n")
share|improve this answer

BrainF* ,79 60

+++++++++++++[->++>+>+++++<<<]>>---<[->>>++++[-<.<.>>]<+<<]
share|improve this answer
3  
+++++++++++++[>+>+++++>++<<<-]>--->>[<.<.>.<.>.<.>.<.>+>-] –  alephalpha Dec 14 '13 at 7:32

Forth, 37

'h 0 [do] [i] 4 / 'A + emit cr [loop]
share|improve this answer

Dc: 35 characters

[rdP10Pr1-d1<p]sp65[5lpx+d91>l]dslx
share|improve this answer

Scala, 42

('A'to'Z')map(x=>List.fill(4)(println(x)))
share|improve this answer
    
+1 for answering "42" –  moala Dec 14 '13 at 10:44

Mathematica 50 41

With 9 fewer characters thanks to alephalpha:

Print /@ {#, #, #, #} & /@ "A"~CharacterRange~"Z"
share|improve this answer
    
Print /@ {#, #, #, #} & /@ "A"~CharacterRange~"Z"; –  alephalpha Dec 14 '13 at 7:27

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}}
share|improve this answer
    
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}. –  manatwork Dec 14 '13 at 20:01
1  
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. –  Mark Reed Dec 15 '13 at 2:36
    
@MarkReed Your code isn't working. Check this –  Wasi Dec 15 '13 at 9:03
    
D'oh. So close! :) But I still don't like the END pattern's requirement for an input stream... –  Mark Reed Dec 15 '13 at 22:00

Bash: 24 characters

printf %s\\n {A..Z}{,,,}
share|improve this answer

Julia, 39, 36

println([char(i/4+64.5)for i=0:103])
share|improve this answer

JavaScript (59 56)

for(i=65;i<91;i+=1/4)console.log(String.fromCharCode(i))
share|improve this answer
    
Hey @user113215, you can save 1 extra char by doing i|0 instead of i,10! –  Dom Hastings Dec 14 '13 at 16:09
    
@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. –  user113215 Dec 15 '13 at 5:19
    
Ahhh, i see! Apologies, for some reason I thought it was doing some kind of parseInt! –  Dom Hastings Dec 15 '13 at 9:09

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)))
share|improve this answer
    
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. –  Mark Reed Dec 16 '13 at 0:42
    
you'r right, i was too lazy to test it out :D fixed –  Shlomi Dec 16 '13 at 0:52

PHP, 53

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

PowerShell, 21

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

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

share|improve this answer

Java: 56

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

edit: changed from 'print' to 'println'

share|improve this answer
    
The output should include newlines after each letter. –  Pierre Arlaud Dec 17 '13 at 14:14
    
thanks for pointing that out, it should print a new line each time now –  reblerebel Dec 17 '13 at 19:51

F#: 61 62 49

for i in 'A'..'Z'do for j in 0..4 do printfn"%c"i
share|improve this answer
    
hm, do you not need ;; at the end? –  Mark Reed Dec 15 '13 at 2:32
    
@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. –  p.s.w.g Dec 17 '13 at 23:06

Q, 16

........

-1@'(,/)4#'.Q.A;
share|improve this answer

Matlab, 38

x=repmat(char(65:90)',1,4)';disp(x(:))
share|improve this answer

Q (13)

.........

-1@''4#'.Q.A;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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