# Print the alphabet four times

The program must print the alphabet four times: first in the normal alphabetical order, second in the order of a qwerty keyboard, third in the order of a dvorak keyboard, and lastly in reverse alphabetical order. The output should resemble this:

abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm
pyfgcrlaoeuidhtnsqjkxbmwvz
zyxwvutsrqponmlkjihgfedcba


The output is not case-sensitive, and you can add or omit newlines or spaces wherever you wish.

The catch: the program must be less than 104 characters, or, in other words, smaller than the length of the alphabet times four.

I will accept the answer with the shortest code, unless I see something really clever or interesting that I am more impressed with.

EDIT: I will accept the shortest answer on Wednesday, 4/27/2011.

EDIT2: And the winner is (as usual) Golfscript in 64 chars! Second place, which is behind by only three chars, is also in Golfscript, with 67 chars, followed by Bash in third place with 72 chars.

But there were a few others I wanted to mention, such as this one, which, depending on your definition, only used 52 "characters", and this one where he wrote it in a language he created.

There were a few people who broke an unwritten rule and did not qualify, but I will mention them just for their way of thinking without the box.

• Consider using a bounty for an answer that impressed you. I think accepting an answer in a code-golf should always be the natural criterion (i.e. length). – Joey Apr 23 '11 at 16:00
• I lost using Tcl: tio.run/… – sergiol Jul 25 '17 at 22:19

# Python 2, 97 characters

q="qwertyuiopasdfghjklzxcvbnm"
a="".join(sorted(q))
print a,q,"pyfgcrlaoeuidhtnsqjkxbmwvz",a[::-1]


Sorting QWERTY and converting back from list to string is shorter than the alphabet itself!*

Another option for generating the alphabet in fewer characters than the alphabet itself:

print map(chr,range(65,91))


Although this prints a Python list of characters.

There's not much to be done here in the way of data compression; jamming the letters into 5 bits only saves 39 characters, which isn't a lot if you then have to un-jam them (and you still have to represent the bytes literally); it looks like zlib (just as an example) saves fewer than 20 bytes; encoding the sequence as a list of deltas to the next letter still takes 5 bits for each delta, because the largest in this sequence is -22:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, -9, 6, -18, 13, 2, 5, -4, -12, 6,
1, -15, 18, -15, 2, 1, 2, 1, 14, -2, -21, 19, -20, 12,
-1, 3, 9, -19, 1, -4, 15, -6, -11, 14, -10, 16, -12, -5,
4, 12, -6, 5, -2, -7, 1, 13, -22, 11, 10, -1, 4, 0, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]


* Although only slightly.

• Python 3.5: q="qwertyuiopasdfghjklzxcvbnm";a=sorted(q);print(*a,q,"pyfgcrlaoeuidhtnsqjkxbmwvz",*a[::-1]) – Veedrac Mar 23 '16 at 8:22

# PHP, 100 bytes

As ISO 8859-1:

<?=base64_encode("i·yø!9%z)ª»-ºü1Ë:°z»rº*)jÇ_ä<\½¹æ§'àr¹Z¡ë¢vg²¨äÅ¹°¿<òÇî¶Êê¦æHâÞuÆÚ");


To reproduce reliably,

printf '<?=base64_encode("' > 4times.php
printf abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba | base64 -d >> 4times.php
printf '");' > 4times.php

• That's an interesting way to do it. All of the other ones are variations on the same idea, but this one is a little different. – Peter Olson Apr 23 '11 at 20:05
• I don't think this should count; the question is tagged "Kolmogorov complexity" and specifies "104 characters" which means we should be discriminating on what we consider a character. Otherwise I'll just define my own character encoding with a single character that maps to the ASCII for an entire program that solves the problem, and then decode that. – Adrian Petrescu Apr 23 '11 at 22:20
• @Adrian Petrescu: that's easily solved by requiring that the encoding already exist at the time the challenge is posed. – Lowjacker Apr 23 '11 at 22:52
• @Lowjacker Sure, but that seems to be a pretty arbitrary way to resolve this. – Adrian Petrescu Apr 23 '11 at 23:09
• @Adrian, The advantage of unicode when using characters vs bytes has been acknowledged for a long time. In my opinion it doesn't make sense for the question to be tagged Kolmogorov while asking for characters. It should be counting bytes. It's up to the question asker to be careful about these terms. – gnibbler Apr 24 '11 at 22:04

## Ruby 1.9, 888784 80

puts b=[*?a..?z],:qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz,b.reverse


## Bash, 82

echo {a..z}
echo qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz
echo {z..a}


## Python, 97 96 "characters"

Taking your use of the word "characters" liberally, here's some python that uses 16-bit unicode characters to encode 3 normal characters each. The program is a total of 96 characters, 35 of which are the weirdest unicode characters I've ever seen.

for i in range(104):print chr((ord(u'ࠠᒃ⃦ⵉ��割廕��匓�เ᳅ⵉૹ�懬ࣅű傎ᱨ�⤰〷�弙劶��ⶍKᓇࡤ^A'[i/3])>>i%3*5&31)+97),


I'm not sure the unicode garbage will come though correctly, so here's a python program to generate the above python program:

#!/usr/bin/python
import codecs
f=codecs.open('alpha.py','w','utf-8')
f.write('#!/usr/bin/python\n')
f.write('# coding=utf-8\n')
f.write('for i in range(104):print chr((ord(u\"')
S='''abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcbaa'''
for i in xrange(0,104,3):
n=32*32*(ord(S[i+2])-97)+32*(ord(S[i+1])-97)+ord(S[i+0])-97
f.write(u'%c'%n)
f.write('"[i/3])>>i%3*5&31)+97),\n')


## Bash, 72

echo {a..z} qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz {z..a}


EDIT: removed tr -d.

Credits: Alexander, Roger.

• Rules allow you to omit |tr -d ' ', saving 10 characters and making this version a leader at the moment of writing :-) – Alexander Gladysh Apr 24 '11 at 0:04
• @Alexander, Cool ;-) did not know that. Thanks to @Roger though for pointing me in the right direction. – asoundmove Apr 24 '11 at 5:07
• I was trying to find a more elegant solution with some sort of polynomial with powers of primes and modulo (or in other words find a simple enough permutation function that works through that sequence)... but can't spend the sort of time required for such a solution, so I'll leave it at that. Plus it'd be pretty hard to express concisely. – asoundmove Apr 24 '11 at 5:59

### Golfscript, 64 chars

"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz".$2%\1$-1%


Note: also 64 bytes. Actually, playing with Unicode and base changes seems counterproductive because base is too long a keyword.

## Windows PowerShell, 89

Not particularly good, but at least shorter than the output string:

[char[]](65..90)
'qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz'
[char[]](90..65))


Way shorter now that I saw that there is a relaxation on whitespace. And a little shorter still, if I ignore case which I'm allowed to.

• You've got an extra ) there. Should be 88. – Iszi Dec 13 '13 at 17:48

## Golfscript, 76, 67 chars

123,97>+."qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz"\-1%

• Use 123,97>+ to generate the initial alphabet. – Ventero Apr 23 '11 at 23:36
• Thanks Ventero, I indeed saw that in your other post the other day. – YOU Apr 24 '11 at 6:14

## CI - 92 chars

'a(1p'z((,0(4d)(.$)<)$)(0c0c.1+2p$.)>)$)qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz


CI ("Compiler/Interpreter") is a simple stack-based language I created for the "Self-Interpreting Interpreter" challenge (link to answer). I posted that answer about two hours before this challenge was posted. It's no GolfScript (which I still haven't gotten around to learning), as my goal was to make the interpreter short.

This program must be passed to the ci interpreter through standard input. The interpreter treats characters after a terminating ) as input to the program. My language doesn't support string literals, so stdin is taking its place.

Python 2.7.X - 103 characters ironically...

a='abcdefghijklmnopqrstuvwxyz'
print a,'qwertyuiopasdfghjklzxcvbnm pyfgcrlaoeuidhtnsqjkxbmwvz',a[::-1]


As it turns out, generating the alphabet in python takes more characters than hard-coding it. As does importing the alphabet. Gotta love a readability-centered language sometimes.

• You don't need the space between the QWERTY and DVORAK strings, it would be 1 character less. – Lowjacker Apr 23 '11 at 18:57
• true, but python automatically appends a space between each item in the comma-separated printing list, so the space is my personal sacrifice to the alter of readable output. – arrdem Apr 23 '11 at 20:55
• You could use the + instead of , and save that space. – Martin Ueding Apr 23 '11 at 22:21

# C — 133 chars

Shortened version from Casey:

main(i){char a[105];strcpy(a+26,"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz");for(i=0;i<26;)a[i]=a[103-i]=i+++65;puts(a);}


Whitespaced:

main(i) {
char a[105];
strcpy(a+26, "qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz");
for(i=0;i<26;)
a[i]=a[103-i]=i+++65;
puts(a);
}


### Transposed output

Here is a version that does transposed output, for fun. 134 chars.

main(i){char*a="qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz";for(i=0;i<26;i++,a++)printf("%c%c%c%c\n",i+65,*a,*(a+25),90-i);}


Whitespaced:

main(i){
char*a="qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz";
for(i=0;i<26;i++,a++)
printf("%c%c%c%c\n",i+65,*a,*(a+25),90-i);
}


Prints:

AqmZ
BwpY
CeyX
DrfW
EtgV
FycU
GurT
HilS
IoaR
JpoQ
KaeP
LsuO
MdiN
NfdM
OghL
PhtK
QjnJ
RksI
SlqH
TzjG
UxkF
VcxE
WvbD
XbmC
YnwB
ZmvA

• Still does not fit, of course. – Alexander Gladysh Apr 23 '11 at 23:24
• by declaring i as global and putting strcpy(...) to the first block of the for loop you get 129. still too much though. – bebe Jul 11 '14 at 21:20

J

x=:(97+/i.26){a.
x
261329910883437428257896643x A.x
247073478763191548160234722x A.x
(_1+/-i.26){x


My first experience with J. The idea is simply to use the canonical (lexicographic) number of the required permutation; unfortunately this takes more characters than hard-coding it. But if one could find some arithmetic expression (using powers and whatnot) yielding the magic numbers, it could be shortened.

• 261329910883437428257896643 has the prime factorization 7*601*991*213987797*292923318887 so there are no powers in this one. – Jerry Jeremiah Mar 23 '16 at 21:35

## Python, 90 unicode chars

#coding:u8
print u"扡摣晥桧橩汫湭灯牱瑳癵硷穹睱牥祴極灯獡晤桧歪穬捸扶浮祰杦牣慬敯極桤湴煳歪扸睭究祺睸當獴煲潰浮歬楪杨敦捤慢".encode("u16")[2:]


• Actually, it only has 52 "characters". – Peter Olson Apr 24 '11 at 15:21

Javascript 139 121

new:

q='qwertyuiopasdfghjklzxcvbnm'
a=q.split('').sort()


old:

a=[]
a[26]='qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz'
for(i=0;i<26;i++)a[i]=a[52-i]=String.fromCharCode(i+65)

• You could also have 121 with: alert((a=(q='qwertyuiopasdfghjklzxcvbnm').split('').sort()).join('')+q+'pyfgcrlaoeuidhtnsqjkxbmwvz'+a.reverse().join('')) – WallyWest Dec 17 '13 at 5:06

# Lua — 111 characters

Beats C and C++! :-)

print'abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba'


### "Clever" version in Lua

129 characters

q='qwertyuiopasdfghjklzxcvbnm'a={q:byte(1,-1)}table.sort(a)a=q.char(unpack(a))print(a,q,'pyfgcrlaoeuidhtnsqjkxbmwvz',a:reverse())


Whitespaced:

q='qwertyuiopasdfghjklzxcvbnm'
a={q:byte(1,-1)}
table.sort(a)
a=q.char(unpack(a))
print(a,q,'pyfgcrlaoeuidhtnsqjkxbmwvz',a:reverse())


# C - 163 135 chars

This doesn't meet the requirements as it clocks in at over 104 characters, but it was fun to make. Maybe someone will have some suggestions.

main(i){char a[105];strcpy(&a[26],"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz");for(i=0;i<26;)a[i]=a[103-i]=i+++65;puts(a);}


This compiles on GCC 4.5.1 (others untested). Yay for omitting #includes, return types, and variable declarations! Hardcoding would've resulted in a shorter program, but that isn't any fun.

Whitespaced version:

main(i) {
char a[105];
strcpy(&a[26], "qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz");
for(i=0;i<26;)
a[i]=a[103-i]=i+++65;
puts(a);
}


## C - 124 122 lame

Here is the lame hardcoded version, and it still doesn't make the cut.

main(){puts("abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba");}

• Use puts() instead printf in second case — 2 chars less. – Alexander Gladysh Apr 23 '11 at 23:11
• And replace while with puts(a); in first case, dropping *p=a; — 138 chars instead of 163. – Alexander Gladysh Apr 23 '11 at 23:13
• Awesome suggestions! I had totally forgotten about puts. Changed. – Casey Apr 24 '11 at 0:28

## PHP 88 bytes

<?=$a=join(range(a,z)),~Žˆš‹†Š–žŒ›™˜—•”“…‡œ‰‘’†™˜œ“žšŠ–›—‹‘ŒŽ•”‡’ˆ‰…,strrev($a);


# J - 69 char

'pyfgcrlaoeuidhtnsqjkxbmwvz'(,~|.)&(,\:~)'mnbvcxzlkjhgfdsapoiuytrewq'


Explanation:

• (,\:~) y - Sort y in descending order (\:~), and append (,) that to y.

• x (,~|.) y - Reverse (|.) y, and append that to the front of x.

• x F&G y - Execute (G y) F (G y), that is, perform F on the results of applying G to the arguments.

So, all together, we append the alphabet, backwards, to both the Dvorak and reverse-QWERTY strings, then reverse the QWERTY/alphabet one and put that in front of the Dvorak/alphabet one.

# Bash, 64

I'm very late to the party, but I have a 64 character solution. Program needs to be saved to disk to work

echo {a..z} $(sed 1d$0|base64) {z..a};exit
««·+¢¢–¬uø!ŽIsÅËÛžjr~+•ªº'a¶{*ŽL[›ó


How it works:

{a..z} and {z..a} are pretty self explanatory, but basically that just prints every character in that respective range.

$(sed 1d$0|base64) outputs the program, ignores the first line, then passes it through a base64 encoder (as some other examples have done).

The reason this is shorter than other similar examples...

...is because the {a..z} ranges are shorter than base64 encodings, so I only need to base64 encode half the string. And because I'm reading the binary data from file rather than storing it in a string, I don't need to escape any character in code.

How to recreate this script:

If the code doesn't copy/paste correctly from Stack Exchange to your text editor then run the following in your command line (shell agnostic):

echo 'echo {a..z} $(sed 1d$0|base64) {z..a};exit' > alphabetgolf
echo 'qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz' | base64 -d >> alphabetgolf


Then to run:

bash alphabetgolf


## C++

It's kind of sad when the shortest way to do something in a language is to hard-code it.

#include <iostream>

int main()
{
std::cout<<"abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba";
}

• You don't need to output the linefeeds. – Lowjacker Apr 23 '11 at 20:21
• @Low: Doh! I forgot to remove those when I took out the line breaks I realized I didn't need. Thanks for pointing that out. – John Apr 23 '11 at 20:23
• This is a very obvious solution, and it surpasses the maximum length requirement. – Peter Olson Apr 24 '11 at 0:35
• std::cout is shorter that using namespace std, and you can skip void. – Alexander Gladysh Apr 24 '11 at 0:42
• @Alex: Thanks for pointing that out. – John Apr 24 '11 at 2:18

# J (77)

(,|.@(26&{.))(,~/:~@(26&{.))'qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz'


Shaved off 3 characters by using a variable:

(,|.@a)(,~/:~@(a=.26&{.))'qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz'


Shaved off 2 more characters by using a value-level approach:

(|.a),~t,~a=./:~26{.t=.'qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz'


main=putStr\$['a'..'z']++"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz"++['z','y'..'a']


# Perl (51)

There's no rule that says I can't.

use LWP::Simple;print get("http://bit.ly/fGkflf");


## Q (68)

.Q.a,"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz",(|:).Q.a


Outputs:

"abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnmpyfg
crlaoeuidhtnsqjkxbmwvzzyxwvutsrqponmlkjihgfedcba"


Tried to find an improvement on hard-coding the middle two but didn't come up with one.

# K - 69 char

b,a,|b:a@<26#a:"qwertyuiopasdfghjklzxcvbnmpyfgcrlaeouidhtnsqjkxbmwvz"


Explanation:

• 26#a: - Assign the two keyboard layouts to a, and then take the first 26 characters.

• b:a@< - Sort a and assign that to b: < provides the permutation for an ascending sort, and @ lets us index a in order to sort it.

• b,a,| - Append in a row b, then a, then the reverse (|) of b.

# Pyth, 57

G"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz"VG


Alphabet, string, reversed alphabet. G is the alphabet. Print is implicit.

# Dyalog APL, 60 bytes

⎕A'QWERTYUIOPASDFGHJKLZXCVBNMPYFGCRLAOEUIDHTNSQJKXBMWVZ',⌽⎕A


Prints:

  ABCDEFGHIJKLMNOPQRSTUVWXYZ  QWERTYUIOPASDFGHJKLZXCVBNMPYFGCRLAOEUIDHTNSQJKXBMWVZ ZYXWVUTSRQPONMLKJIHGFEDCBA


Which is permitted as per OP:

The output is not case-sensitive, and you can add or omit newlines or spaces wherever you wish.

# Jelly, 36 bytes (non-competing)

Øa;Øq;“pyfgcrlaoeuidhtnsqjkxbmwvz”;U


Try it online!

Explanation:

Øa;Øq;“pyfgcrlaoeuidhtnsqjkxbmwvz”;U    |
Øa;                                     | alphabet
Øq;                                  | list with all rows of the qwerty keyboard
“pyfgcrlaoeuidhtnsqjkxbmwvz”;     | string
U    | reverse


# 05AB1E, 60 59 bytes

AÂ"qwertyuiopasdfghjklzxcvbnm""pyfgcrlaoeuidhtnsqjkxbmwvz"»


Try it online!

AÂ - pushes the bifurcated alphabet.

"qwertyuiopasdfghjklzxcvbnm""pyfgcrlaoeuidhtnsqjkxbmwvz" - Pushes qwerty and drovak.

» - Prints stack.

# 05AB1E, 57 bytes (If you don't care about random \n)

AÂ"qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz"»


Try it online!

• In the first example, you can save a byte by replacing "" with a newline. – Neil A. May 22 '17 at 0:26

# Deorst, 39 bytes

EfEuE|'pyfgcrlaoeuidhtnsqjkxbmwvz'EfVE;


Try it online!

How on Earth is Deorst beating everything bar Jelly???

## How it works

Ef             - Push the lowercase alphabet
EuE|         - Push 'qwerty…vbnm'
'…'      - Push that string
Ef    - Push the alphabet
VE; - Reverse and concatenate