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

85 Answers 85

1 2
3
1
\$\begingroup\$

Kotlin, 121 bytes

Array(26){"abcdefghijklmnopqrstuvwxyz"}.mapIndexed{i,s->s.replace(s[i],s[i]-32)}.let{it+it.reversed()}.forEach(::println)

Try it online!

Generates array of alphabet string with alphabet size, and then replacing characters to uppercase characters, after concacts resulting list and its reversed version.

\$\endgroup\$
1
  • \$\begingroup\$ nice ::println \$\endgroup\$
    – mazzy
    Commented Jul 25, 2018 at 13:35
1
\$\begingroup\$

Java, 159 158 bytes

class A{public static void main(String[]a){for(int b=0;b++<51;)for(int c=70;c++<96;)System.out.print((char)(c+(96-Math.abs(b-26)==c?-6:26))+(c>95?"\n":""));}}

Online demo

Try it online

Explanation

class A {
    public static void main(String[] a) {
        // loop the alphabet two times (2 x 26)
        for (int b = 0; b++ < 51;) {
            // loop from lower character ASCII codes a (97) to z (122)
            // c will be from [71 to 96] (because of condition "c++" before the first iteration)
            for (int c = 70; c++ < 96;) {
                System.out.print(
                    // print character by ASCII code
                    (char)(c + (
                        // if character position matches the wave
                        96 - Math.abs(b - 26) == c
                        // substract 6, because 71 - 6 = 65 = 'A'
                        ? -6
                        // otherwise add 26, because 71 + 26 = 97 = 'a'
                        : 26)
                    ) +
                    // if current character is greater then the 'y' (ASCII 95), also print a new line
                    (c > 95 ? "\n" : "")
                );
            }
        }
    }
}

Perhaps there's too much math in this. Someone who can shorten this?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I know it's been almost three years, but you can golf 8 bytes like this: interface A{static void main(String[]a){for(int b=0,c;b++<51;)for(c=70;c++<96;)System.out.printf("%c%s",96-Math.abs(b-26)==c?c-6:c+26,c>95?"\n":"");}}. Summary of changes: class to interface so you can remove public; int b=0; and int c=70; to int b=0,c; and c=70;; System.out.print((char)(...)+(...)); to System.out.printf("%c%s",...,...);; c+(...?-6:26) to ...?c-6:c+26 to get rid of the parenthesis. Try it online: 150 bytes. nice answer though, +1 from me. :) \$\endgroup\$ Commented Jul 25, 2018 at 13:43
1
\$\begingroup\$

K (ngn/k), 27 26 bytes

-1 byte from using @ngn's approach

x,1_|x:`c$97++(!26)-32*=26

Try it online!

  • =26 build a 26-by-26 unit matrix (with 1's on the main diagonal)
  • +(!26)-32* convert to matrix of offsets from "a" (e.g. -32 => "A", 1 => "b", etc.)
  • `c$97+ convert offsets to actual characters
  • x,1_|x: "mirror" the result vertically
\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 byte: x,1_|x:`c$97++(!26)-32*=26 \$\endgroup\$
    – ngn
    Commented Jan 12, 2021 at 2:54
1
\$\begingroup\$

Python - 272 bytes

a=range(97,123)
d=False
i=0
while True:
    if d and i>24:
        i-=1
        continue
    for (x,l) in enumerate(a):
        print(chr(l) if x!=i else chr(l-32),end='')
    i-=1 if d else -1
    if i<0:
        break
    if i>26:
        i=25
        d=True
    print()

Try it online!
My first attempt at codegolf, so it definitely could be smaller.

\$\endgroup\$
1
1
\$\begingroup\$

Zsh, 48 bytes

b=({a..z})
for n ($b {y..a})<<<${(j::)b/$n/$n:u}

Try it online! Similar to the bash solution but using zsh substitutions.

\$\endgroup\$
1
\$\begingroup\$

Vim, 33 bytes

:h<_
jjYZZ51Pqr~jq25@rqlh~hjq24@l

Try it online!

Explanation

More or less combines the approaches of the two existing Vim answers, saving a couple of bytes along the way.

:h<_
jjYZZ

Yank the lowercase alphabet from the help file.

51P

Paste it 51 times, leaving the buffer's original empty line at the end.

qr~jq

Record macro r: swap case of the current letter and move right (~), then move down one row (j).

25@r

Run macro r 25 more times.

qlh~hjq

Record macro l: move left (h), swap case of the current letter and move right (~), move left again (h), and move down one row (j).

24@l

Run macro l 24 more times.

\$\endgroup\$
1
\$\begingroup\$

Vyxal jr, 8 bytes

kA∞ƛ⇩kaV

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 23 bytes

↑{1⎕C@⍵⎕C⎕A}¨(⍳26),⌽⍳25

Attempt This Online!

                    ⍳25  integers from 1 to 25
                   ⌽     reverse
                  ,      join with
             (⍳26)       integers from 1 to 26
 {         }¨            for each integer n
         ⎕A               uppercase alphabet
       ⎕C                 casefold
  1⎕C                     convert to uppercase
     @                    at
      ⍵                   index n
↑                        convert nested vector to simple matrix
\$\endgroup\$
0
\$\begingroup\$

Java: 918 characters

public class Sample{

    public static int i;

    public static int j;

    public static int z;

    public static String[] k={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};


public static void main(String args[]){
        for (i=0; i<k.length;i++){
            for(j=0 ;j<k.length;j++){
                if (i==j){
                System.out.print(k[j].toUpperCase());
                }
                else
                System.out.print(k[j].toLowerCase());
            }
            System.out.println();
            }
        for (i=1; i<k.length;i++){
            for(j=0 ;j<k.length;j++){
                if (i==(k.length-(j+1))){
                System.out.print(k[j].toUpperCase());
                }
                else
                System.out.print(k[j].toLowerCase());
            }
            System.out.println();
        }
        }
}
\$\endgroup\$
1
  • 6
    \$\begingroup\$ Welcome to PPCG! This is code golf, so the goal is to solve the problem with as few bytes of code as possible. For a start you could get rid of all the unnecessary whitespace amd braces. \$\endgroup\$ Commented Jul 28, 2015 at 11:06
0
\$\begingroup\$

ECMAScript 6 - 114 110 characters

(112 108 + 2 linebreaks)

b=(c,d)=>c<26?String.fromCharCode(c==d?c+65:c+97)+b(c+1,d):""
a=i=>b(0,i)+(i?"\n"+a(i-1)+"\n"+b(0,i):"")
a(25)

Two recursive functions a and b:

  • b creates an alphabet string with the letter at index d capitalized. Doesn't take very much advantage of recursion, but fat arrows make this function just a tiny bit shorter than a loop.
  • a creates line by line anagrams (sort of) of the strings generated by b

EDIT: Woops! The capitalization is backwards ('z' is capitalized on the first and last line, 'a' in the middle)

\$\endgroup\$
0
\$\begingroup\$

Haskell 128 Bytes

import Data.Char
a c=take 701(cycle(toUpper:replicate c id))
f=zipWith($)(a 27++drop 27(a 25))$cycle(['a'..'z']++"\n")
g=f++"z"

Not as short as the others but a very different approach. Defines the constant g as the string. save to a file and open interactively using ghci <filename>. Then enter g to the repl.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 78 bytes

mapM_ putStrLn[['a'..toEnum(x-2)]++toEnum(x-33):[toEnum(x)..'z']|x<-[98..123]]

This code takes for all 'numbers, x from 'a'(+1) to 'z'(+1):

  • the letters from a to the letter corresponding to x-2
  • the letter x-33 (x-1 uppercased)
  • the letters from the letter corresponding to x It concatenes them. We have the list of the strings. We print it.
\$\endgroup\$
0
\$\begingroup\$

Python 3, 103 characters

a="abcdefghijklmnopqrstuvwxyz"
for i in range(51):
 x=25-abs(25-i)
 print(a.replace(a[x],a[x].upper()))
\$\endgroup\$
3
  • \$\begingroup\$ Why calculating len(abc) 3 times? It will be always 26. See Tips for golfing in Python for a couple of hints to reduce code size. (By the way, the code you posted has only 141 bytes according to UTF-8 string length & byte counter.) \$\endgroup\$
    – manatwork
    Commented Feb 9, 2016 at 17:39
  • 2
    \$\begingroup\$ You keep being one step behind yourself: you fixed the count as 141 and reduced your code to 121 characters.☺ Given that you use long variable name and unnecessarily deep indentation, Tips for golfing in <all languages> may also give you a few golfing hints. And one hint for this particular case: count from 0. That way you can use range(51) and x=25-abs(25-i), which are shorter. \$\endgroup\$
    – manatwork
    Commented Feb 9, 2016 at 17:55
  • \$\begingroup\$ manatwork, thanks for all the advice, i was taking the size displayed at the file system, instead of the character count. \$\endgroup\$ Commented Feb 9, 2016 at 19:54
0
\$\begingroup\$

Japt v1.4.5 -R, 12 bytes

;C¬ê £CrX,_u

Try it online!

Somehow tied with top Pyth submission.

Unpacked & How it works

;Cq ê mX{CrX,Z{Zu
;                  Use alternative set of initial variables
 C                 "abc...z"
  q ê              Split into chars and palindromify [a,b,...,z,...,b,a]
      mX{          Map...
         CrX,Z{Zu    Replace the char X in C into uppercase

-R                 Join with newline
\$\endgroup\$
2
  • \$\begingroup\$ 11 bytes \$\endgroup\$
    – Shaggy
    Commented Jun 13, 2018 at 16:46
  • \$\begingroup\$ Alternative 11 bytes, if you want to stick with r. \$\endgroup\$
    – Shaggy
    Commented Jun 13, 2018 at 17:16
0
\$\begingroup\$

MS-SQL, 135 bytes

WITH t AS(SELECT-25n UNION ALL SELECT n+1FROM t WHERE n<25)SELECT
STUFF('abcdefghijklmnopqrstuvwxyz',26-ABS(n),1,CHAR(90-ABS(n)))FROM t

Inspired largely by MickyT's postgreSQL solution, but since we can't use generate_series, we use a CTE to create a number table, and use STUFF to replace each character.

\$\endgroup\$
0
\$\begingroup\$

Yabasic, 93 bytes

An anonymous function that takes no input and outputs to the console.

For i=-25To 25
For j=-25To 0
s$=Chr$(122+j)
If j=-abs(i)Then s$=Upper$(s$)Fi
?s$;
Next
?
Next

Try it online!


Alternate Solution, 93 bytes

For i=-25To 25
For j=-25To 0
s$=Chr$(122+j)
If j+abs(i)Then?s$;Else?Upper$(s$);Fi
Next
?
Next

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 107 bytes

a='abcdefghijklmnopqrstuvwxyz'
for c in range(51):
i=25-abs(25-c)
print a[:i]+a[i].upper()+a[i+1:]
\$\endgroup\$
2
  • \$\begingroup\$ the diagonals go forwards & back in the output. You're only outputting half of the required output. \$\endgroup\$
    – dzaima
    Commented Jun 7, 2018 at 19:29
  • \$\begingroup\$ Oh, My bad. Ill edit it \$\endgroup\$
    – Khalil
    Commented Jun 8, 2018 at 15:59
0
\$\begingroup\$

dc, 83 bytes

97sj1sxBDsy[32-]sU[liddlj=UP1+dsiBD>I]sI[97silIxAPljlx+dsjly!=J]dsJx_1sx96syBBsjlJx

Try it online!

Bit chunkier than I would have liked. Ultimately we're going to use two macros, I and J, and they're going to use some counters, i and j. The counters are decimal values converted to ASCII for printing. Macro I ([liddlj=UP1+dsiBD>I]sI)handles printing one line of the alphabet, letter at a time, checking each letter to see if it matches and running macro U ([32-]sU) to uppercase if need be. It runs until it hits 123, just past the letter 'z'. Macro J ([97silIxAPljlx+dsjly!=J]dsJx) resets i to the beginning of the alphabet, runs I, prints a linebreak (AP), adds the value of x to j, and keeps running as long as it doesn't match the value of y. The first time we run it, x is positive 1 and y is 123, so we increment until we're past the letter 'z'. After doing that, we put j back down to 121 (after ...xyZ we're supposed to print ...xYz and not a second ...xyZ), and set x to negative 1 and y to 96, running J with a decrement and lower bound just below the letter 'a'.

\$\endgroup\$
0
\$\begingroup\$

Python 3, 88 bytes

a=list(range(26));[print(''.join([chr([97+x,65+x][x==i])for x in a]))for i in a+a[::-1]]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

VBA, 80 bytes

A script that takes no input and outputs to the console.

For i=-25To 25:For j=-25To 0:s=Chr(122+j):?IIf(j+Abs(i),s,UCase(s));:Next:?:Next
\$\endgroup\$
0
\$\begingroup\$

Lua, 96 bytes

for j=1,51 do for i=1,26 do io.write(string.char(i+((j+i==52or i==j)and 64or 96)))end print()end

Try it online!


for j=1, 51 do -- a to z (26) + y to a (25), because the Z doesn't repeat
  for i=1, 26 do -- for each letter
     io.write(string.char(i+((j==52-i or j==i) and 64 or 96))) -- ↴
     io.write(string.char(i+(                               )) -- writes the character represented by the value i+(...) on the ASCII table 
                             (j==52-i or j==i)                 -- if the uppercase letter is equal to i or to 52-i
                                               and 64 or 96    -- then sum 64 else sum 96 (because 'A' is 65 and 'a' is 97)
  end
  print() -- jumps to the next line
end

If you have any questions, feel free to ask!

\$\endgroup\$
0
\$\begingroup\$

Excel, 101 bytes

Using Immediate Window and Output in CellA1:A51

[A1:A51]="=SUBSTITUTE(""abcdefghijklmnopqrstuvwxyz"",CHAR(122-ABS(ROW()-26)),CHAR(90-ABS(ROW()-26)))"
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 12 9 bytes

AûvAyDu:,

The existing 05AB1E answer got deleted for some reason, so I decided to post a new one.

-3 bytes thanks to @dzaima.

Try it online.

Explanation:

A           # Push the lowercase alphabet: "abcdefghijklmnopqrstuvwxyz"
 û          # Palindromize it: "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba"
  v         # Loop over each of its characters `y`
   A        #  Push the lowercase alphabet
    y  :    #  Replace the current lowercase letter `y`,
     Du     #  with it's uppercase variant
        ,   #  Print the modified alphabet with trailing newline
\$\endgroup\$
0
\$\begingroup\$

Japt -R, 11 bytes

;C£ChYXu̐

Test it

\$\endgroup\$
0
\$\begingroup\$

ARM Thumb-2 (with libc), 54 bytes

f7ff fffe is a 4 byte linker placeholder for puts.

Machine code:

b5fe 466f 241a 3c01 f104 0161 5539 d1fa
76bc 0038 0025 2d1a d009 bf88 f1c5 0533
5d46 f006 025f 5542 f7ff fffe 557e 3401
2c34 d3ee bdfe

Commented assembly:

        .thumb
        .arch armv6t2
        .globl main
        .thumb_func
main:
        // Create a 27 byte buffer on the stack by doing a massive push
        // 27 is for the buffer, 1 is padding, and the last is the link register
        // to return from main.
        push    {r1, r2, r3, r4, r5, r6, r7, lr}
        // Create a frame pointer in r7, since sp lacks narrow register offset
        // addressing.
        mov     r7, sp
        // Fill the stack buffer with "abcdefgh....\0"
        movs    r4, #26
.Lfill_loop:
        subs    r4, #1
        add.w   r1, r4, #'a' // wide
        strb    r1, [r7, r4]
        bne     .Lfill_loop
.Lfill_loop_end:
        // r4 is zero now. Serves as both a null terminator and a loop counter.
        // Null terminate the string.
        strb    r4, [r7, #26]
        // The print loop
.Lprint_loop:
        // Load buffer into r0 for puts
        movs    r0, r7
        // Make a copy of r4 into r5
        movs    r5, r4
        // Compare to 26.
        cmp     r5, #26
        // If equal, skip to prevent printing Z twice
        beq     .Lskip_double_Z
        // If r5 is > 26 change it to 51 - r5 to reverse the order.
        it      hi
        rsbhi   r5, #51 // wide
        // Load the byte from the stack
        ldrb    r6, [r0, r5]
        // toupper(r6) -> r6 & 0x5f
        and.w   r2, r6, #0x5f // wide
        strb    r2, [r0, r5]
        // Call puts on our modified buffer
        bl      puts
        // Reset the char we modified.
        strb    r6, [r7, r5]
.Lskip_double_Z:
        // Loop 52 times.
        adds    r4, #1
        cmp     r4, #52
        blo     .Lprint_loop
.Lloop_end:
        // restore stack (clobbering all registers but bionic libc seems to be
        // fine with this) and return by popping the link register into pc.
        pop     {r1, r2, r3, r4, r5, r6, r7, pc}
\$\endgroup\$
1 2
3

Your Answer

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

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