63
\$\begingroup\$

In as few bytes as possible, write a program or function that outputs the following:

Abcdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
abcdefghijKlmnopqrstuvwxyz
abcdefghijkLmnopqrstuvwxyz
abcdefghijklMnopqrstuvwxyz
abcdefghijklmNopqrstuvwxyz
abcdefghijklmnOpqrstuvwxyz
abcdefghijklmnoPqrstuvwxyz
abcdefghijklmnopQrstuvwxyz
abcdefghijklmnopqRstuvwxyz
abcdefghijklmnopqrStuvwxyz
abcdefghijklmnopqrsTuvwxyz
abcdefghijklmnopqrstUvwxyz
abcdefghijklmnopqrstuVwxyz
abcdefghijklmnopqrstuvWxyz
abcdefghijklmnopqrstuvwXyz
abcdefghijklmnopqrstuvwxYz
abcdefghijklmnopqrstuvwxyZ
abcdefghijklmnopqrstuvwxYz
abcdefghijklmnopqrstuvwXyz
abcdefghijklmnopqrstuvWxyz
abcdefghijklmnopqrstuVwxyz
abcdefghijklmnopqrstUvwxyz
abcdefghijklmnopqrsTuvwxyz
abcdefghijklmnopqrStuvwxyz
abcdefghijklmnopqRstuvwxyz
abcdefghijklmnopQrstuvwxyz
abcdefghijklmnoPqrstuvwxyz
abcdefghijklmnOpqrstuvwxyz
abcdefghijklmNopqrstuvwxyz
abcdefghijklMnopqrstuvwxyz
abcdefghijkLmnopqrstuvwxyz
abcdefghijKlmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
Abcdefghijklmnopqrstuvwxyz

A trailing newline is permitted. You can find a reference ungolfed Python implementation here.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is it safe to assume input is never upper case? \$\endgroup\$ – Winny Jul 26 '15 at 5:46
  • 40
    \$\begingroup\$ @Winny There is no input. The output is fixed. In fact, that's the general idea of kolmogorov-complexity questions. \$\endgroup\$ – Chris Jester-Young Jul 26 '15 at 5:49
  • \$\begingroup\$ This has been in the HNQ list consistently since you posted it. Nice work. :) \$\endgroup\$ – Alex A. Jul 30 '15 at 20:25
  • 1
    \$\begingroup\$ You can find a reference ungolfed Python implementation here. -> link's broken \$\endgroup\$ – Franck Dernoncourt Aug 11 '17 at 22:48
  • \$\begingroup\$ Franck Dernoncourt's point still stands. The link is broken. \$\endgroup\$ – Jonathan Frech Jul 25 '18 at 20:18

74 Answers 74

64
\$\begingroup\$

Pyth, 12 bytes

V+Gt_GXGNrN1

Demonstration.

In Pyth, G is the lowercase alphabet. +Gt_G is abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba, the character that needs to be uppercased in each row.

V sets up a for loop over this string, with N as the loop variable.

In the body, XGNrN1 is a string translation function. X translates G, the alphabet, replacing N with rN1, the uppercase version of N. r ... 1 is the uppercase function. This gives the desired output.

\$\endgroup\$
  • 30
    \$\begingroup\$ Am I the only one who finds it funny that the lowercase alphabet is represented by an uppercase letter? \$\endgroup\$ – Alex A. Jul 31 '15 at 16:31
31
\$\begingroup\$

C,73

Sometimes the simplest approach is best: print every character one by one. this beats a lot of languages it really shouldn't.

i;f(){for(i=1377;i--;)putchar(i%27?123-i%27-32*!(i/702?i%28-4:i%26):10);}

explanation

i;f(){
   for(i=1377;i--;)
   putchar(i%27?                 //if I not divisible by 27
     123-i%27-                   //  print lowercase letter from ASCII 122 downards
       32*!(i/702?i%28-4:i%26)   //  subtract 32 to make it uppercase where necessary: above i=702, use i%28-4, below it use i%26
     :10);                       //if I divisible by 27 print a newline (10)
}
\$\endgroup\$
26
\$\begingroup\$

Python 2, 69 bytes

i=25
exec"L=range(97,123);L[~abs(i)]^=32;i-=1;print bytearray(L);"*51

Nice and simple, I think.

\$\endgroup\$
  • \$\begingroup\$ That's really clever. Loop unrolling! \$\endgroup\$ – Alex Van Liew Jul 31 '15 at 20:03
20
\$\begingroup\$

Brainfuck (8bit), 231 bytes

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

Ok, so it's never going to be the shortest, but it's the taking part that counts... right?!

Try it here (ensure to tick 'Dynamic memory')

\$\endgroup\$
  • 5
    \$\begingroup\$ If the goal was to be the longest possible code that is still completely indecipherable to the average human programmer… \$\endgroup\$ – Caleb Jul 27 '15 at 11:15
  • 7
    \$\begingroup\$ @Caleb I think the BF code is some of the easiest code to understand in existence. It's the program/functionality that's hard to understand. Everyone should know that > shifts one cell to the right, for example. \$\endgroup\$ – mbomb007 Jul 27 '15 at 17:48
  • 4
    \$\begingroup\$ Always got to love the BF answers ;) \$\endgroup\$ – RedPanda Jul 28 '15 at 7:33
  • \$\begingroup\$ You didn't write this code directly now did you? \$\endgroup\$ – BAR Jul 30 '15 at 22:32
  • 6
    \$\begingroup\$ I'm ashamed to say I did! \$\endgroup\$ – Jarmex Jul 30 '15 at 22:40
12
\$\begingroup\$

MS-DOS Binary, 61

This code does not have to be compiled, it will run in MS-DOS if you write it into a file called wave.com . The code in hex:

ba3d0189d7b91a00b061aa404975fbb00aaab00daab024aa31f6e8130046
83fe1a75f7be1800e807004e75fae80100c389d3802820b409cd21800020
c3

Or, if you prefer something more readable, here is how to produce it using debug.exe (the empty line after the code is important):

debug.exe wave.com
a
mov dx,13d
mov di,dx
mov cx,1a
mov al,61
stosb
inc ax
dec cx
jnz 10a
mov al,a
stosb
mov al,d
stosb
mov al,24
stosb
xor si,si
call 130
inc si
cmp si,1a
jnz 11a
mov si,18
call 130
dec si
jnz 126
call 130
ret
mov bx,dx
sub byte ptr [si+bx],20
mov ah,9
int 21
add byte ptr [si+bx],20
ret

rcx
3e
w
q
\$\endgroup\$
11
\$\begingroup\$

Ruby: 71 68 65 63 characters

puts f=(e=*?a..?z).map{|c|(e*"").tr c,c.upcase},f[0,25].reverse

Sample run:

bash-4.3$ ruby -e 'puts f=(e=*?a..?z).map{|c|(e*"").tr c,c.upcase},f[0,25].reverse' | head
Abcdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
\$\endgroup\$
  • 1
    \$\begingroup\$ 63: puts f=(e=*?a..?z).map{|c|(e*"").tr c,c.upcase},f[0,25].reverse \$\endgroup\$ – Ventero Jul 26 '15 at 18:20
  • \$\begingroup\$ Doh. I tried a couple of dumb ideas to optimize e's reuse, but of course not the right way. Thank you, @Ventero. \$\endgroup\$ – manatwork Jul 27 '15 at 7:59
10
\$\begingroup\$

Matlab, 60 58 54 bytes

I=32*eye(26);[ones(51,1)*(97:122) '']-[I;I(25:-1:1,:)])

With thanks to Dennis Jaheruddin for saving me 4 bytes.

\$\endgroup\$
  • \$\begingroup\$ Here you can also use the typical trick to replace char(x) by [x ''] to save a byte. \$\endgroup\$ – Dennis Jaheruddin Jul 30 '15 at 8:58
  • \$\begingroup\$ Also end-1 is a rather verbose way to write 25! \$\endgroup\$ – Dennis Jaheruddin Jul 30 '15 at 8:59
  • \$\begingroup\$ @DennisJaheruddin Oops. Thanks for both! Actually the [x ''] trick is not usual at all for me. But now I remember seeing it in one of your anwers :-) \$\endgroup\$ – Luis Mendo Jul 30 '15 at 9:19
8
\$\begingroup\$

SWI-Prolog, 136 bytes

a:-(R=0;R=1),between(1,26,I),(I=1,R=0;I\=1,nl),between(1,26,J),(R=0,L=I;R=1,L is 27-I),(J=L,K is J+64,put(K);J\=L,K is J+96,put(K)),\+!.

Abusing backtracking to loop...

\$\endgroup\$
8
\$\begingroup\$

Haskell 100 89 88 bytes

putStr$map toEnum.(\(h,c:t)->h++c-32:t++[10]).(`splitAt`[97..122]).(25-).abs=<<[-25..25]

The lambda helper function \(h,c:t) takes a pair of lists of ascii values and concatenates both, but with the first value of the second list capitalized. The main function splits the lowercase alphabet (given in ascii, 97..122) at every position 0,..,24,25,24,..,0 and calls the lambda in every step. Before printing each value is turned into the corresponding character.

\$\endgroup\$
8
\$\begingroup\$

Scala 110 109 characters

val a=('a'to'z').map(c⇒('a'to'z').map(v⇒if(v==c)c.toUpper else v).mkString)
a++a.init.reverse foreach println
\$\endgroup\$
  • 5
    \$\begingroup\$ OMG in Scala ⇒ symbol is used? I mean not => but ⇒??? \$\endgroup\$ – shabunc Jul 27 '15 at 23:35
  • 2
    \$\begingroup\$ Both are valid :) \$\endgroup\$ – gilad hoch Jul 28 '15 at 4:29
  • \$\begingroup\$ could shave off 1 byte if I change foreach println to mkString("\n"), and output a string as return value instead of printing it to the screen \$\endgroup\$ – gilad hoch Jul 29 '15 at 11:59
7
\$\begingroup\$

SQL (postgreSQL), 107 101

Generate are series from -25 to 25 and use the absolute value to replace characters with their uppercase version. Thanks to manatwork for the tip about the @ operator.

select replace('abcdefghijklmnopqrstuvwxyz',chr(122- @i),chr(90- @i))from generate_series(-25,25)a(i)
\$\endgroup\$
  • \$\begingroup\$ You know that PostgreSQL has a @ operator? \$\endgroup\$ – manatwork Jul 26 '15 at 11:46
  • \$\begingroup\$ @manatwork nope I didn't know that, but now I do thanks \$\endgroup\$ – MickyT Jul 26 '15 at 12:46
7
\$\begingroup\$

Haskell, 81 bytes

Counting bytes the way @nimi did; f is an IO action that prints the desired output.

x!y|x==min(50-y)y=65|0<1=97
f=mapM putStrLn[[toEnum$x+x!y|x<-[0..25]]|y<-[0..50]]
\$\endgroup\$
  • \$\begingroup\$ Very elegant. Didn't know that guards can be used inline. \$\endgroup\$ – user2845840 Jul 29 '15 at 15:38
6
\$\begingroup\$

Pyth - 18 17 bytes

First pass, probably can be made much shorter. Uses X to substitute and r1 to capitalize.

V+KU26t_KXGNr@GN1

Try it online here.

\$\endgroup\$
6
\$\begingroup\$

MATLAB - 58 bytes

char(bsxfun(@minus,97:122,32*[eye(25,26);rot90(eye(26))]))

Similar to Luis Mendo's solution, but using the broadcasting abilities of bsxfun.

Taking advantage that in ASCII, the difference between a capital and lower case character is exactly 32 values away from each other, we first generate lower case letters from ASCII codes 97 to 122 which are the ASCII codes from lowercase a to lowercase z respectfully, then create a 51 row matrix that contains the 26 ASCII codes from 97 to 122. Therefore, each row of this matrix contains a numerical sequence of values from 97 to 122. Next, we create another matrix where each ith row of this matrix contains a 32 in the ith column. The first 26 rows of this matrix has this pattern, which is essentially the identity matrix multiplied by 32. The function eye creates an identity matrix for you. The last 25 rows of this matrix is the scaled identity matrix rotated 90 degrees.

By taking this custom weighted identity matrix and subtracting this with the first matrix, then converting the resulting ASCII codes into characters, the desired "Mexican Hat" sequence is produced.

Example Run

>> char(bsxfun(@minus,97:122,32*[eye(25,26);rot90(eye(26))]))

ans =

Abcdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
abcdefghijKlmnopqrstuvwxyz
abcdefghijkLmnopqrstuvwxyz
abcdefghijklMnopqrstuvwxyz
abcdefghijklmNopqrstuvwxyz
abcdefghijklmnOpqrstuvwxyz
abcdefghijklmnoPqrstuvwxyz
abcdefghijklmnopQrstuvwxyz
abcdefghijklmnopqRstuvwxyz
abcdefghijklmnopqrStuvwxyz
abcdefghijklmnopqrsTuvwxyz
abcdefghijklmnopqrstUvwxyz
abcdefghijklmnopqrstuVwxyz
abcdefghijklmnopqrstuvWxyz
abcdefghijklmnopqrstuvwXyz
abcdefghijklmnopqrstuvwxYz
abcdefghijklmnopqrstuvwxyZ
abcdefghijklmnopqrstuvwxYz
abcdefghijklmnopqrstuvwXyz
abcdefghijklmnopqrstuvWxyz
abcdefghijklmnopqrstuVwxyz
abcdefghijklmnopqrstUvwxyz
abcdefghijklmnopqrsTuvwxyz
abcdefghijklmnopqrStuvwxyz
abcdefghijklmnopqRstuvwxyz
abcdefghijklmnopQrstuvwxyz
abcdefghijklmnoPqrstuvwxyz
abcdefghijklmnOpqrstuvwxyz
abcdefghijklmNopqrstuvwxyz
abcdefghijklMnopqrstuvwxyz
abcdefghijkLmnopqrstuvwxyz
abcdefghijKlmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
Abcdefghijklmnopqrstuvwxyz

You can also run this example using IDEone's online Octave environment. Octave is essentially MATLAB but free: http://ideone.com/PknMe0

\$\endgroup\$
  • 1
    \$\begingroup\$ rot90 -- well thought! \$\endgroup\$ – Luis Mendo Jul 27 '15 at 13:31
  • \$\begingroup\$ char(ones(26,1)*[97:122] -eye(26)*32) \$\endgroup\$ – user3528438 Jul 29 '15 at 17:17
  • \$\begingroup\$ @user3528438 how do you handle the second half? The code only computes the first half of the wave. You need to compute the rest. \$\endgroup\$ – rayryeng Jul 29 '15 at 17:18
  • \$\begingroup\$ @user3528438 - Also note that what you wrote is basically the first half of Luis Mendo's answer. I decided to write something a bit different to achieve the same thing :) \$\endgroup\$ – rayryeng Jul 29 '15 at 17:34
  • \$\begingroup\$ @rayryeng yeah, it suprises me that the second half is harder to handle, and also how to avoid the center duplicate. \$\endgroup\$ – user3528438 Jul 29 '15 at 17:47
5
\$\begingroup\$

J, 31 23 bytes

u:|:(97+i.26)-32*=|i:25

8 bytes saved thanks to @Mauris.

Try it online here.

\$\endgroup\$
  • \$\begingroup\$ I could get 23: u:|:(97+i.26)-32*=|i:25 (monad = is really useful here!) \$\endgroup\$ – Lynn Jul 27 '15 at 12:45
  • \$\begingroup\$ @Mauris Thanks, I haven't thought of using monad = here. It's very nice! \$\endgroup\$ – randomra Jul 27 '15 at 13:44
5
\$\begingroup\$

Perl, 51 bytes

50 bytes code + 1 byte command line parameter

@a=a..z,@a[-1-abs]=uc@a[-1-abs],print@a for-25..25

Can be used as follows:

perl -le '@a=a..z,@a[-1-abs]=uc@a[-1-abs],print@a for-25..25'

Or online here (note I had to add ,"\n" to this as I couldn't add the -l arg).


Much longer method Before the shortened version above, I tried a different method which ended up being pretty chunky. I've left it below anyway for reference.

86 bytes code + 1 byte command line arg

$_=join"",0,a..z,1;print s/1//r while s/(([A-Z])|0)(\D)|(.)((?2))(1)/\L\2\U\3\4\6\L\5/

First Perl I've ever golfed properly so I imagine there's a lot that can be done with it - please do suggest improvements!

Can be used as followed:

perl -le '$_=join"",0,a..z,1;print s/1//r while s/(([A-Z])|0)(\D)|(.)((?2))(1)/\L\2\U\3\4\6\L\5/'

Or online here (note I had to add ."\n" to this as I couldn't add the -l arg).

Explanation

General approach is to use regex substitution to do all the hard work. We start off with:

0abcdefghijklmnopqrstuvwxyz1

This matches (([A-Z])|0)(\D) and gets replaced with \U\3 (\U changes to uppercase) to give:

Abcdefghijklmnopqrstuvwxyz1

From this point onwards, we continue to match the same regex and replace with \L\2\U\3:

aBcdefghijklmnopqrstuvwxyz1
abCdefghijklmnopqrstuvwxyz1
...
abcdefghijklmnopqrstuvwxyZ1

Now the second alternation of the regex matches, (.)((?2))(1) (which is the same as (.)([A-Z])(1)). We replace with \U\4\6\L\5 to give:

abcdefghijklmnopqrstuvwxY1z

This continues to match and replace until we reach:

A1bcdefghijklmnopqrstuvwxyz

and there are no more regex matches.

At each point in the loop we strip off the '1' and print.

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

PHP, 87 71 69 bytes

Not the shortest one, but it works as intended.
Thanks to @manatwork for a few tips to reduce it's size by a lot.
And thanks to @Blackhole, the size was reduced by 2 bytes.

for(;$L=range(a,z),$L[25-abs($i++-25)]^=' ',$i<52;)echo join($L).'
';

Not exactly pretty, but works.

\$\endgroup\$
  • 1
    \$\begingroup\$ “glue Defaults to an empty string.” – PHP documentation about join()'s first parameter. \$\endgroup\$ – manatwork Jul 26 '15 at 14:07
  • 1
    \$\begingroup\$ That string subscript is not really optimal: $i<25?$i:25-($i-25)25-abs($i-25) \$\endgroup\$ – manatwork Jul 26 '15 at 14:16
  • 1
    \$\begingroup\$ Given that you are already ignoring warnings (for the undefined constants a and z), you could ignore another one for the uninitialized $i. While touching $i, move its incrementation into the string subscript. for(;$i<51;){$L=range(a,z);$L[25-abs($i++-25)]^=" ";echo join($L),"↵";} (Just wrap the line where I used “↵” in the code.) \$\endgroup\$ – manatwork Jul 26 '15 at 14:28
  • \$\begingroup\$ @manatwork Thanks a lot! I totally forgot that the \n was there. The initialization of $i was left as an accident. And thank you a lot for the 25-abs($i-25). I wouldn't get there by myself. \$\endgroup\$ – Ismael Miguel Jul 26 '15 at 16:40
  • 2
    \$\begingroup\$ Your for loop can be optimised: for(;$L=range(a,z),$L[25-abs($i++-25)]^=' ',$i<52;)echo join($L).'↵'; (-2 bytes). \$\endgroup\$ – Blackhole Jul 27 '15 at 22:20
5
\$\begingroup\$

PowerShell 3.0, 82 bytes

$(0..25)+$(24..0)|%{$i=$_;[string](@(97..122)|%{[char]@($_,($_-32))[$_-eq$i+97]})}
\$\endgroup\$
5
\$\begingroup\$

TIS Node Type T21 Architecture - 216 215 bytes

Watch it in action here! There's a DOWN in that video that I later golfed to ANY, but it's functionally identical.

This language has no concept of strings or characters, so I should point out that I'm using ASCII values, i.e. output begins 97, 66, 67...88, 89, 90, 10, 65, 98...

Here's the code in the format of TIS-100's save data, for the purposes of scoring:

@5
ADD 25
L:MOV 27 ANY
SUB 1
JGZ L
MOV 25 ANY
JRO -1
@6
JRO 2
S:MOV 10 ANY
ADD 65
MOV ACC ANY
SUB 90
JEZ S
ADD 26
@9
MOV 32 ANY
ADD UP
L:MOV 0 ANY
SUB 1
JGZ L
@10
MOV UP ACC
ADD ANY
SUB 42
D:JEZ D
ADD 42
MOV ACC ANY

Explanation

\$\endgroup\$
  • \$\begingroup\$ is this the first question in TIS-100 or what? \$\endgroup\$ – noɥʇʎԀʎzɐɹƆ Jul 19 '16 at 23:47
  • 1
    \$\begingroup\$ I have implemented a TIS emulator for TIO, so you can now try it online! \$\endgroup\$ – Phlarx May 2 '18 at 19:54
4
\$\begingroup\$

JavaScript ES6, 121 bytes

_=>Array(51).fill('abcdefghijklmnopqrstuvwxyz').map((e,i)=>e.replace(/./g,(f,j)=>j==i|i+j==50?f.toUpperCase():f)).join`
`

This is really long because it makes more sense to hardcode the alphabet than to use the absurdly long String.fromCharCode to generate the characters. Test it out below with the Stack snippet, which uses better-supported ES5 and below.

f=function(){
  return Array(51).fill('abcdefghijklmnopqrstuvwxyz').map(function(e,i){
    return e.replace(/./g,function(f,j){
      return j==i|i+j==50?f.toUpperCase():f
    })
  }).join('\n')
}

// Polyfill for ES6-only fill()
Array.prototype.fill = Array.prototype.fill || function(val){
  for(i=0;i<this.length;i++){
    this[i] = val
  }
  return this
}

document.getElementById('p').innerText=f()
<pre id="p"></pre>

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

CJam, 23 bytes

51{25-z~'{,97>'[2$+tN}/

Try it online in the CJam interpreter.

How it works

51{                  }/ e# For I from 0 to 50:
   25-                  e#   Compute J := I - 25.
                        e#   This maps [0 ... 50] to [-25 ... 25].
      z                 e#   Compute K := abs(J).
                        e#   This maps [-25 ... 25] to [25 ... 0 ... 25].
       ~                e#   Compute L := ~K = -(K + 1).
                        e#   This maps [25 ... 0 ... 25] to [-26 ... -1 ... -26].
        '{,             e#   Push ['\0' ... 'z'].
           97>          e#   Discard the first 97. Pushes ['a' ... 'z'].
              '[2$+     e#   Add L to '['. Pushes 'A' for -26, 'Z' for -1.
                   t    e#   Set ['a' ... 'z'][L] to '[' + L.
                    N   e#   Push a linefeed.
\$\endgroup\$
4
\$\begingroup\$

R, 78 70

M=replicate(26,c(letters,"\n"));diag(M)=LETTERS;cat(M,M[,25:1],sep="")

Improved by @MickyT

\$\endgroup\$
  • 2
    \$\begingroup\$ Almost identical to one I came up with but put to one side. I used M=replicate(26,c(letters,"\n")) rather than a matrix. It will save you a few bytes \$\endgroup\$ – MickyT Jul 26 '15 at 23:15
  • \$\begingroup\$ Save 1 byte by using write: tio.run/##K/r/… \$\endgroup\$ – JayCe Jun 7 '18 at 18:16
4
\$\begingroup\$

Linux Assembly, 289

Unfortunately not competitive with high level languages and probably far from optimal, but pretty straightforward. Run it using nasm -f elf64 -o a.o wave.S; ld -s -o a a.o; ./a (the resulting binary is just 568 bytes big):

section .data
s:db 'abcdefghijklmnopqrstuvwxyz',10
section .text
global _start
_start:
mov esi,0
a:call c
inc esi
cmp esi,26
jne a
mov esi,24
b:call c
dec esi
jnz b
call c
mov eax,1
call d
c:mov ecx,s
sub byte [ecx+esi],32
mov eax,4
mov edx,27
d:mov ebx,1
int 80h
add byte [ecx+esi],32
ret
\$\endgroup\$
  • \$\begingroup\$ Seems a waste of space to compile this to ELF (lots of bloating zeros there). It can be much reduced if done as a DOS's COM program. I guess it could then run in dosbox in Linux :) \$\endgroup\$ – Ruslan Jul 30 '15 at 5:18
  • \$\begingroup\$ I know and I did just that. Look at my other post codegolf.stackexchange.com/a/53984/42642 :) \$\endgroup\$ – user2845840 Jul 30 '15 at 12:23
  • \$\begingroup\$ Yeah, seen it, upvoted it. Didn't notice it was you too though. \$\endgroup\$ – Ruslan Jul 30 '15 at 15:34
4
\$\begingroup\$

C#, 140 139 135 132

void f(){int d=1,i=0;var s="abcdefghijklmnopqrstuvwxyz\n";for(;i>=0;i+=d=i==25?-1:d)Console.Write(s.Replace(s[i],(char)(s[i]-32)));}

Expanded

void f()
{
    int d = 1, i =0;
    var s = "abcdefghijklmnopqrstuvwxyz\n";
    for (; i >= 0; i += d = i == 25 ? -1 : d)
        Console.Write(s.Replace(s[i], (char)(s[i] - 32)));
}

Saved 1 byte thanks to @Gunther34567 using a ternary instead of if

Saved 4 bytes then nesting that ternary inside the loop and moving the alphabet to the outside of the loop

Saved 3 bytes combining integer declarations thanks to @eatonphil

\$\endgroup\$
  • 1
    \$\begingroup\$ you could save 1 byte by changing if(i==25)d=-1; to d=i==25?-1:d; \$\endgroup\$ – grabthefish Jul 28 '15 at 8:39
  • 1
    \$\begingroup\$ You can save 3 bytes by changing var d=1 to int d=1,i. \$\endgroup\$ – eatonphil Aug 1 '15 at 15:55
3
\$\begingroup\$

Bash: 76 66 characters

printf -va %s {a..z}
for c in {a..z} {y..a};{ echo ${a/$c/${c^}};}

Sample run:

bash-4.3$ printf -va %s {a..z};for c in {a..z} {y..a};{ echo ${a/$c/${c^}};} | head
Abcdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
\$\endgroup\$
  • \$\begingroup\$ An anonymous user suggested that the first line is not necessary at all which would reduce the byte count to 45. \$\endgroup\$ – Martin Ender Jul 30 '15 at 17:00
  • \$\begingroup\$ Interesting. Then from where would the alphabet appear? \$\endgroup\$ – manatwork Jul 30 '15 at 17:19
  • \$\begingroup\$ I couldn't tell you. The edit simply removed the printf call. I rejected the edit, so you can test it yourself. \$\endgroup\$ – Martin Ender Jul 30 '15 at 17:24
  • \$\begingroup\$ Yes, I saw. (The site notified me about the edit and its fate.) As without the 1st line which places the alphabet in variable a, the 2nd line can only print empty strings from variable a, I see no other resolution than rejecting it. :( \$\endgroup\$ – manatwork Jul 30 '15 at 17:27
3
\$\begingroup\$

C (208 characters)

(my first try at Code Golf)

#include<stdio.h>
#define P printf
#define A(i) {for(l='a',p=0;l<='z';++l,++p){if(p==i)P("%c",l-32);else P("%c",l);}P("\n");}
int main(){int i,p;char l;for(i=0;i<26;++i)A(i);for(i=24;i>=0;--i)A(i);return 0;}
\$\endgroup\$
  • \$\begingroup\$ Can you remove the space after A(i)? I program Java, but it seems like you could. \$\endgroup\$ – MCMastery Jul 29 '15 at 15:47
  • \$\begingroup\$ @MCMastery I don't think so since it's in a #define. It's defining A(i) to hold the value {for...;}, and the statement needs the space to differentiate symbol from value. (I think that's right but I could be wrong.) \$\endgroup\$ – Alex A. Jul 31 '15 at 16:35
  • \$\begingroup\$ @AlexA. Yes, that's right. The preprocessor needs the space to delimit the macro name from its definition. \$\endgroup\$ – musarithmia Jul 31 '15 at 21:33
3
\$\begingroup\$

Sed: 135 119 116 111 characters

(109 character code + 1 character command line option + 1 character input.)

s/.*/abcdefghijklmnopqrstuvwxyz/
h;H;G;H;G;H;g;G
s/.{,28}/\u&/gp
s/$/\t/
:;s/(\w+\n?)\t(.*)/\t\2\1/;t
s/.*Z//

Sample run:

bash-4.3$ sed -rf mexican.sed <<< '' | head
Abcdefghijklmnopqrstuvwxyz
aBcdefghijklmnopqrstuvwxyz
abCdefghijklmnopqrstuvwxyz
abcDefghijklmnopqrstuvwxyz
abcdEfghijklmnopqrstuvwxyz
abcdeFghijklmnopqrstuvwxyz
abcdefGhijklmnopqrstuvwxyz
abcdefgHijklmnopqrstuvwxyz
abcdefghIjklmnopqrstuvwxyz
abcdefghiJklmnopqrstuvwxyz
\$\endgroup\$
3
\$\begingroup\$

Javascript (ES6), 113 bytes

c=-1;while(c++<50){console.log('abcdefghijklmnopqrstuvwxyz'.replace(/./g,(x,i)=>i==c|i+c==50?x.toUpperCase():x))}

110 bytes

for(c=-1;c++<50;)console.log('abcdefghijklmnopqrstuvwxyz'.replace(/./g,(x,i)=>i==c|i+c==50?x.toUpperCase():x))

102 bytes

Old school is unbeatable unless we'll have range operator/function/generator/whatever in js

for(c=-1;c++<50;){for(s='',i=-1;i++<25;)s+=String.fromCharCode(i+(i==c|i+c==50?65:97));console.log(s)}

100 bytes

Unluckily Math.abs is too long

for(c=51;c--;){for(s='',i=26;i--;)s+=String.fromCharCode(c+i==25|c-i==25?90-i:122-i);console.log(s)}

96 94 bytes

Though I've beeing downvoted without explanation I continue my struggle

for(c=-26;c++<25;){for(s='',i=26;i--;)s+=String.fromCharCode(c*c-i*i?122-i:90-i);console.log(s)}

We can shave off a couple of bytes by rearranging loop instructions:

for(c=-26;c++<25;console.log(s))for(s='',i=26;i--;s+=String.fromCharCode(c*c-i*i?122-i:90-i));
\$\endgroup\$
  • \$\begingroup\$ Please explain downvotes. The output is wrong? \$\endgroup\$ – shabunc Jul 28 '15 at 3:43
  • 2
    \$\begingroup\$ Maybe because you technically have multiple answers in a single post? Hell if I know, good shaving, though! \$\endgroup\$ – Sandy Gifford Jul 29 '15 at 20:22
  • \$\begingroup\$ Also, I think you can shave off that last semi-colon \$\endgroup\$ – Sandy Gifford Jul 29 '15 at 20:23
  • \$\begingroup\$ Nope, I was incorrect \$\endgroup\$ – Sandy Gifford Jul 29 '15 at 20:26
3
\$\begingroup\$

x86 assembly for DOS, 41 Bytes compiled

Binary:

00000000  b9 e6 ff b3 61 b8 61 02  50 38 d8 75 02 24 df 88
00000010  c2 cd 21 58 40 3c 7b 75  ef b2 0a cd 21 41 79 02
00000020  43 43 4b 80 f9 19 75 dd  c3

Source code, save as "wave.asm", compile with "nasm -f bin -o wave.com wave.asm" and run with "dosbox wave.com"

org 100h 
section .text
start:
mov cx,-26
mov bl,'a'
next_line:
mov ax, 0261h
next_char:
push ax
cmp al,bl
jnz lower_case
and al,255-32
lower_case:
mov dl,al
int 21h
pop ax
inc ax
cmp al,'z'+1
jnz next_char
mov dl,0ah
int 21h
inc cx
jns move_left
inc bx
inc bx
move_left:
dec bx
cmp cl,25
jnz next_line
ret
\$\endgroup\$
3
\$\begingroup\$

Perl - 95 64 bytes

Takes advantage of the fact \u makes the next character printed an uppercase in Perl.

for$c(0..50){$n=1;print map{++$n==27-abs$c-25?"\u$_":$_}a..z,$/}

Thanks to manatwork for saving 31 bytes and fixing it (my previous code did not work.)

\$\endgroup\$
  • \$\begingroup\$ That \u seems to work in a separate sample, but not in your code. :( All characters stayed lowercase. Could you show us how your code should be executed? (I put it in a file then called perl passing it the file name, no switches.) By the way, I use perl 5.20.2. \$\endgroup\$ – manatwork Jul 27 '15 at 10:14
  • \$\begingroup\$ By the way, it seems to work when \u is followed by the letter to transform in the same string literal: for$c(0..50){$n=1;print map{++$n==27-abs$c-25?"\u$_":$_}a..z,$/} \$\endgroup\$ – manatwork Jul 27 '15 at 10:38
  • \$\begingroup\$ @manatwork Strange, it worked when I did it. (I use 5.18.) Your code works, and it cuts down the size significantly, so I'll use it. Thanks! \$\endgroup\$ – ASCIIThenANSI Jul 29 '15 at 17:32

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.