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LMNOPQRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNONMLKJIHGFEDCBA
ABCDEFGHIJKLMNMLKJIHGFEDCBA
ABCDEFGHIJKLMLKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A

Specs

  • Extra trailing newlines are allowed at the end of the output.
  • Extra trailing spaces (U+0020) are allowed at the end of each line, including the extra trailing newlines.
  • You can use all lowercase instead of all uppercase, but you cannot print partially lowercase partially uppercase.
  • You can return the text as a function output instead of printing it in a full program.

Scoring

Since this is a triangle, and a triangle has 3 sides, and 3 is a small number, your code should be small in terms of byte-count.

\$\endgroup\$
  • 22
    \$\begingroup\$ So many alphabets recently \$\endgroup\$ – downrep_nation Aug 3 '16 at 11:15
  • 1
    \$\begingroup\$ My synesthesia is going hog wild, @downrep_nation \$\endgroup\$ – DoctorHeckle Aug 3 '16 at 13:33
  • 5
    \$\begingroup\$ "Since a triangle has 3 sides and 3 is a small number, so your code should be small in terms of byte count." seems legitimate \$\endgroup\$ – Rohan Jhunjhunwala Aug 4 '16 at 22:00
  • 1
    \$\begingroup\$ Squares of numbers composed only of 1 seems related: 1*1 = 1 ~= A , 11*11 = 121 ~= ABA , 111*111 = 12321 ~= ABCBA ... \$\endgroup\$ – Caridorc Aug 5 '16 at 13:28
  • 1
    \$\begingroup\$ "Since a triangle has 3 sides and..." Illuminati Confirmed. \$\endgroup\$ – HyperNeutrino Oct 28 '16 at 0:47

88 Answers 88

2
\$\begingroup\$

Powershell, 61 52 bytes

Thanks to TimmyD for saving 9 bytes!

65..90+89..65|%{-join[char[]]((65..$_-ne$_)+$_..65)}

Loops through ASCII values for capital letters forwards, then backwards. For each number, this creates an array of the first X numbers, removes the X-1st number, then adds the reverse of the first X numbers, which is all then cast to chars and joined into a string.

\$\endgroup\$
  • \$\begingroup\$ Hey, that's good, thanks! I had plugged in 65..90..65 on a whim earlier with no success. Guess I forgot I could just add the ranges together. \$\endgroup\$ – Ben Owen Aug 3 '16 at 13:51
2
\$\begingroup\$

Python 2, 117 111 105 100 bytes

s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"*2
for i in range(51):print(s[:-i-1]+s[-i-3::-1],s[:i]+s[i::-1])[26>i]

Run it

Thanks to @LeakyNun and @manatwork for pointing out a few byte saves.

Non-Golfed:

import string

def print_alphatriangle(n):
    offset = 1
    tail_offset = 3
    alpha = string.ascii_uppercase * n

    for i in range(len(alpha) - offset):
        if len(alpha) / 2 > i:
            print alpha[:i] + alpha[i::-offset]
            continue
        print alpha[:-i-offset] + alpha[-i-tail_offset::-offset]

This method works simply by string splicing an alphabet string that is concatenated together. Depending on whether i has reached a mid-way point of the string, it then starts to print out decreasing strings.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove both instances of 0 \$\endgroup\$ – Leaky Nun Aug 3 '16 at 16:53
  • \$\begingroup\$ Good catch! Thanks, updated @LeakyNun \$\endgroup\$ – ospahiu Aug 3 '16 at 16:54
  • 1
    \$\begingroup\$ Huh? len(s)/2? Why not 26? \$\endgroup\$ – manatwork Aug 3 '16 at 17:00
  • \$\begingroup\$ Good catch! @manatwork \$\endgroup\$ – ospahiu Aug 3 '16 at 17:04
  • 1
    \$\begingroup\$ See and apply marcog's tip from Tips for golfing in Python: print[s[:-i-1]+s[-i-3::-1],s[:i]+s[i::-1]][26>i]. \$\endgroup\$ – manatwork Aug 3 '16 at 17:09
2
\$\begingroup\$

Mathematica 98 96 92 bytes

There has to be a shorter way, even in Mathematica.

6 bytes saved thanks to Martin Ender.

r=Reverse@*Most; Print/@FromCharacterCode@Join[t=Join[s=Range[65,64+k],r@s]~Table~{k,26},r@t]
\$\endgroup\$
  • \$\begingroup\$ Can't Range[65,64+k] just be 65~Range~64+k? \$\endgroup\$ – Yytsi Aug 3 '16 at 10:31
  • \$\begingroup\$ That doesn't work. I think the +k somehow is left out. \$\endgroup\$ – DavidC Aug 3 '16 at 10:55
  • \$\begingroup\$ ~Table~ and Column@ should work though. And since you seem to use r only in conjunction with Most you might as well define it as Reverse@*Most. \$\endgroup\$ – Martin Ender Aug 3 '16 at 14:36
  • \$\begingroup\$ Table and Column now have infix notation. r=Reverse@Most appears not to work. \$\endgroup\$ – DavidC Aug 3 '16 at 15:00
  • \$\begingroup\$ It should be Reverse@*Most (the asterisk wasn't a typo ;)). I'm also not sure how legitimate Column actually is since it only displays like a list of lines in a Mathematica notebook. Running this code from a script file wouldn't display anything. However, Print/@ is the same number of bytes and works in either case. \$\endgroup\$ – Martin Ender Aug 4 '16 at 10:14
2
\$\begingroup\$

Javascript (ES5), 105 bytes

Beating all other ES6 answers using some good ol' vanilla JS looping!

for(a="",n=g=1;n++<52;n<27?g++:g--){for(z=x=0;z++<g*2-1;z<g?x++:x--)a+=String.fromCharCode(65+x);a+="\n"}a

I could save 1 byte by using ES6 and changing

a+="\n"

to

a+=`
`

but nah.

\$\endgroup\$
  • \$\begingroup\$ Since you don't have console.log anywhere I imagine this is supposed to be the contents of a function. If you wrapped it in the ()=>{} that you need to actually execute your code and tried it, it would return undefined. It needs to be return a. If you had done everything so that it can actually be run (including the ()=>{} and return ) it would be 119 bytes. tl;dr this code doesn't work. \$\endgroup\$ – Pandacoder Aug 5 '16 at 18:37
  • \$\begingroup\$ Nice. Also (10+x).toString(36) is shorter than String.fromCharCode(65+x) and like @Pandacoder said, you should put something like alert(a) at the end to make it valid. \$\endgroup\$ – user81655 Aug 14 '16 at 10:11
2
\$\begingroup\$

C#, 289 215 206 190 179 171 159 157 156 153 147 bytes

(n,i,s)=>{s=new string[51];for(i=0;i<26;){for(n=65;n-65<i;)s[i]+=(char)n++;for(;n>64;)s[i]+=(char)n--;s[50-i]=s[i++];}return string.Join("\n",s);};

Saved 3 bytes thanks to VisualMelon

Saved 6 bytes thanks to mdfst13

Formatted version:

(n,i,s)=>
{
    s=new string[51];

    for(i=0;i<26;)
    {
        for(n=65;n-65<i;)
            s[i]+=(char)n++;

        for(;n>64;)
            s[i] += (char)n--;

        s[50-i]=s[i++];
    }

    return string.Join("\n",s);
};

156 byte version (when the irrelevant whitespace is removed) of counting backwards, uses a similar concept to the above method but does so in reverse.

(n, i, s) =>
{
    s = new string[i = 51];
    for (; --i > 24;)
    {
        for (n = 65; i + n < 116;)
            s[i] += (char)n++;

        for (n = 50 - i + 64; n > 64;)
            s[i] += (char)n--;

        s[50 - i] = s[i];
    }

    return string.Join("\n", s);
};
\$\endgroup\$
  • \$\begingroup\$ Feel free to share any improvements to my code if you find any \$\endgroup\$ – TheLethalCoder Aug 4 '16 at 14:03
  • \$\begingroup\$ An explanation would be nice! You can use a for loop instead of a while, saving you the ; on the statement prior. The last statement in the outer loop can be put inside the last inner for loop (s[...]=s[i]+=) (need to juggle the i++ a bit, can certainly be done with only one extra byte). You've also got 51-i-1 in there, which can be reduced ;) I would be looking into reducing the two inner loops into one, and counting i backwards (so you assign it to 51 inside the array constructor). \$\endgroup\$ – VisualMelon Aug 4 '16 at 17:51
  • \$\begingroup\$ @VisualMelon I've been looking at it for ages and I never saw the 51 - I - 1 haha, and I can't put the last statement in the last inner loop as it doesn't get called on the first pass but didn't know I could chain the assignment like that so that's cool! And I've been looking into combining the loops, getting there \$\endgroup\$ – TheLethalCoder Aug 5 '16 at 8:06
  • \$\begingroup\$ @VisualMelon I reversed the outer loop and ended up adding 3 bytes when I got it working, will see if I can golf it down though \$\endgroup\$ – TheLethalCoder Aug 5 '16 at 9:39
2
\$\begingroup\$

JavaScript (ES6), 89 bytes

R=(n,s=10,c=s.toString(36))=>n?c+R(n-1,s+1)+c:c
C=(n=0,r=R(n))=>n<25?r+`
`+C(n+1)+`
`+r:r

Not the shortest, but I wanted to try my hand at recursion. Call with C().

\$\endgroup\$
2
\$\begingroup\$

Perl 5 with -M5.010, 54 bytes

for$@(A..Z){@;=($_.$/,@;);s/$*/$*$@$*/;say;$*=$@}say@;

Try it online!

Saved bytes thanks to @Xcali!

\$\endgroup\$
  • \$\begingroup\$ Found a method which takes 56 bytes \$\endgroup\$ – Xcali Jun 14 at 22:58
  • \$\begingroup\$ Thanks @Xcali! With you suggestion managed to get it down to 54. And some added obfuscation for entertainment... \$\endgroup\$ – Dom Hastings yesterday
1
\$\begingroup\$

JavaScript (ES6), 141

for(a=65,q=[],z=[];a<91;z.push(a++))q.push(String.fromCharCode(...z,a,...[...z].reverse()))
a=q.pop(),alert([...q,a,...q.reverse()].join`\n`)

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

Retina, 90 88 86 76 bytes

I'm sure it can be made shorter, maybe by using non-verbose replacements and simply outputting at the end. (I tried. I don't think I can do it shorter that way.)

:`
A
A
ABA
{:`

(.)([^Z]\1)
$1$2$2
}T1`__L`L`(?<=(.)[^Z])\1
+:`(.).\1|^A$
$1

Try it online

Thanks to LeakyNun for 14 bytes off

\$\endgroup\$
  • \$\begingroup\$ @LeakyNun Thanks, idk why I thought I needed that part... \$\endgroup\$ – mbomb007 Aug 3 '16 at 16:21
  • \$\begingroup\$ Match the second stage with ^ and insert only AB. \$\endgroup\$ – Martin Ender Aug 4 '16 at 14:20
1
\$\begingroup\$

Swift 2.2, 157 142 Bytes

let t={(l:Int)in[Int](1..<l)+[Int](l.stride(to:0,by:-1))}
for x in(t(n).map{t($0).map{"\(UnicodeScalar($0+64))"}.reduce(""){$0+$1}}){print(x)}

ungolfed:

let makeTriangleArray = {(limit: Int) -> [Int] in
    return [Int](1 ..< limit) + [Int](limit.stride(to: 0, by: -1))
}

let n = 26
let lines = makeTriangleArray(n).map{
    makeTriangleArray($0).map{String(UnicodeScalar($0 + 64))}
                         .reduce(""){ $0 + $1}
}

for line in lines {
    print(line)
}

157 bytes:

let t={(l:Int)in[Int](1..<l)+[Int](l.stride(to:0,by:-1))}
print(t(n).map{t($0).map{"\(UnicodeScalar($0+64))"}.joinWithSeparator("")}.joinWithSeparator("\n"))

ungolfed:

let makeTriangleArray = {(limit: Int) -> [Int] in // "called t" above
    return [Int](1 ..< limit) + [Int](limit.stride(to: 0, by: -1))
}

let n = 26
let s = makeTriangleArray(n).map{
    makeTriangleArray($0).map{String(UnicodeScalar($0 + 64))}
                         .joinWithSeparator("")
}.joinWithSeparator("\n")
print(s)
\$\endgroup\$
1
\$\begingroup\$

Jelly, 16 bytes

ŒRAạ
25ÇÇ€‘ịØAj⁷

This uses the same method in my Python answer.

Try it online.

Explanation

ŒRAạ  Define a helper link. Input: n
ŒR    Create the range [-abs(n), , ..., 0, ..., abs(n)]
  A   Take the absolute value of each
   ạ  Return the absolute difference between each value and n

25ÇÇ€‘ịØAj⁷  Main link. No arguments
25           The constant 25
  Ç          Call the helper link on 25 to get [0, 1, ..., 25, ..., 1, 0]
   ǀ        Call the helper link on each value in the previous
     ‘       Increment every value
       ØA    The alphabet (the string 'A..Z')
      ị      Select the char at each index in the alphabet
         j⁷  Join them using newlines and return
\$\endgroup\$
  • \$\begingroup\$ This is extremely close to this answer \$\endgroup\$ – Leaky Nun Aug 3 '16 at 18:59
  • \$\begingroup\$ @LeakyNun It generates the indices into the alphabet using a different method. Since there is only one output the intermediate values only finally become identical after the increment. \$\endgroup\$ – miles Aug 3 '16 at 19:19
1
\$\begingroup\$

Batch, 226 224 bytes

@set z=ABCDEFGHIJKLMNOPQRSTUVWXYZ
@set a=YXWVUTSRQPONMLKJIHGFEDCBA
@for /l %%i in (0,1,25)do @call echo %%z:~0,%%i%%%%z:~%%i,1%%%%a:~-%%i,%%i%%
@for /l %%i in (24,-1,0)do @call echo %%z:~0,%%i%%%%z:~%%i,1%%%%a:~-%%i,%%i%%

Edit: Saved 2 bytes thanks to @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.

\$\endgroup\$
  • \$\begingroup\$ I think you can do )do, but not any other space (except in(), maybe... \$\endgroup\$ – Erik the Outgolfer Aug 3 '16 at 9:20
  • \$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ Oddly enough I have done this before, just the once: codegolf.stackexchange.com/a/68936/17602 \$\endgroup\$ – Neil Aug 4 '16 at 10:04
1
\$\begingroup\$

QBasic, 151 122 bytes

Really just for fun (via QB64) - it doesn't golf well

Taking some ABS() inspiration from Joffan's VBA answer but applying it differently:

FOR i=-25 TO 25
FOR n=0 TO 25-abs(i)
s$=s$+chr$(n+65)
IF n THEN t$=t$+chr$(90-abs(i)-abs(n))
NEXT
? s$+t$
s$=""
t$=""
NEXT

Previously:

FOR i=0 TO 25
p(i)
NEXT
FOR i=0 to 25
p(25-i)
NEXT
SUB p(i)
FOR n=0 TO i
s$=s$+chr$(n+65)
IF n THEN t$=t$+chr$(65+i-n)
NEXT
? s$+t$
s$=""
t$=""
END SUB
\$\endgroup\$
1
\$\begingroup\$

C#, 162 bytes

void p(){for(int i=1;i<54;i++){var a=Enumerable.Range(65,i>27?54-i:i).Select(n=>(char)n);Console.WriteLine(new string(a.Concat(a.Reverse().Skip(1)).ToArray()));}}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I was wondering what the linq version would look like, you need System.Console.. though right? and shouldn't you also include using System.Linq in the byte count although I'm unsure about that \$\endgroup\$ – TheLethalCoder Aug 4 '16 at 12:15
  • \$\begingroup\$ Change your for loop to for(int i=0;++i<54;) to save some bytes \$\endgroup\$ – TheLethalCoder Aug 4 '16 at 12:25
1
\$\begingroup\$

HTML, 1412 bytes

<pre>A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA
ABCDEFGHIJKLMLKJIHGFEDCBA
ABCDEFGHIJKLMNMLKJIHGFEDCBA
ABCDEFGHIJKLMNONM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FEDCBA
ABCDEFGHGFEDCBA
ABCDEFGFEDCBA
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A</pre>

\$\endgroup\$
  • 4
    \$\begingroup\$ Please – – – – – \$\endgroup\$ – Leaky Nun Aug 4 '16 at 15:52
  • 1
    \$\begingroup\$ Actually, with native HTML it's impossible to do better than this \$\endgroup\$ – Richard Hamilton Aug 5 '16 at 17:36
  • 1
    \$\begingroup\$ @LeakyNun You must be on a whole 'nother level. What is – – – – – – and how do I make it work? \$\endgroup\$ – MonkeyZeus Aug 5 '16 at 17:44
  • \$\begingroup\$ A possible interpretation of @LeakyNun's comment: codegolf.stackexchange.com/a/88984 \$\endgroup\$ – manatwork Aug 6 '16 at 10:56
1
\$\begingroup\$

R, 95 bytes

L=LETTERS;cat("A\n");for(i in 2:50)cat(L[1:(26-abs(i-26))],L[(25-abs(i-26)):1],'\n');cat("A\n")

Ungolfed:

L=LETTERS; #Took idea from @Frederic, I apologize if I shouldn't (first time posting)
cat("A\n")
for(i in 2:50)
{
    cat(L[1:(26-abs(i-26))],L[(25-abs(i-26)):1],'\n')
}
cat("A\n")

Basically, the formula in the cat function in the for loop is a transformation on the absolute value function, mapping n -> n for Letters 2 through 26 and then mapping Letters 27 -> 25, 28 -> 24, ..., 50 -> 2. I don't think it would work if I included the 'A' in this formula, which is why I printed it out separately.

\$\endgroup\$
  • \$\begingroup\$ 100 bytes and actually valid. \$\endgroup\$ – Giuseppe Jan 15 '18 at 17:57
1
\$\begingroup\$

PHP, 80 83 bytes

Making half of the triangle then print it in reverse did the trick.

for($a="A";$a<"Z";$b.=$a++)$c.=$b.$a.strrev($b)."
";echo"$c{$b}Z".strrev($c.$b);
\$\endgroup\$
1
\$\begingroup\$

MATLAB, 53 Bytes

MATLAB is not made for strings, so this needs some looping. I haven't found a faster way to create a list 1 2 3 4 3 2 1, than [1:4,3:-1:1], which seems really verbose.

Anyway, it's only beaten by 11/55 other submissions, and most of those are golfing languages.

for i=[1:26,25:-1:1]
disp(['',64+[1:i,i-1:-1:1]])
end
\$\endgroup\$
  • 1
    \$\begingroup\$ Just spent an entertaining fifteen minutes proving you right that this is indeed the shortest - even inline definition of an anonymous function (Octave-only) is still longer. \$\endgroup\$ – Sanchises Jul 25 '17 at 11:42
1
\$\begingroup\$

R, 71 69 bytes

L=LETTERS;for(i in c(1:26,25:1))cat(c(L[1:i],L[i:1][-1]),"\n",sep="")

See here on an online interpreter.

Edit: moved LETTERS outside the loop in order to avoid the curly brackets.

\$\endgroup\$
  • \$\begingroup\$ 59 bytes \$\endgroup\$ – Giuseppe Jan 15 '18 at 17:59
  • \$\begingroup\$ oh I didn't notice that my suggestion was identical to this one. \$\endgroup\$ – Giuseppe Jan 15 '18 at 17:59
1
\$\begingroup\$

Javascript (ES6), 113 bytes

i=>{for(i=0;i<51;i++,console.log(L))for(j=s=i<26?i:50-i,L="";j>=0;)k=String.fromCharCode(65+j),L=k+(j---s?L+k:L)}

Thanks to manatwork for pointing me to a tip written by William Barbosa to reduce by a byte.

\$\endgroup\$
  • \$\begingroup\$ As William Barbosa wrote in his tip, is shorter to declare an unused dummy parameter. \$\endgroup\$ – manatwork Aug 5 '16 at 18:45
1
\$\begingroup\$

Rust, 120 bytes

fn main(){for j in -25i8..26{let k=25-j.abs();for i in -k..k+1{print!("{}",(65+k-i.abs())as u8 as char);}println!("");}}

My first attempt at codegolfing in Rust... codegolfing in anything, really. A straightforward attempt, just using for loops and abs().

fn main() {
    for j in -25i8..26 {
        let k=25-j.abs();
        for i in -k..k+1{
            print!("{}",(65+k-i.abs())as u8 as char);
        }
        println!("");
    }
}
\$\endgroup\$
1
\$\begingroup\$

PHP, 78 Bytes

foreach(range(A,Z)as$r)$o.="\n$t".strrev($t.=$r);echo$o.substr(strrev($o),51);
\$\endgroup\$
1
\$\begingroup\$

SOGL V0.12, 10 bytes

øZ{⁴Κ}¹╬,k

Try it Here!

Explanation:

ø           push an empty string
 Z{  }      for each character in the alphabet do
   ⁴          duplicate the item before the top item
    Κ         reverse add to that
              or simpler - on each iteration creates a duplicate of ToS but with the current character appended.
      ¹     wrap the stack in an array
       ╬,   quad-palindromize without spacing to a square
         k  remove the 1st line

The last k could be removed if the question allowed leading newlines.

\$\endgroup\$
  • \$\begingroup\$ Your interpreter site runs fine half of the time and half of the time, the run button seems to do nothing! \$\endgroup\$ – officialaimm Sep 15 '17 at 3:43
  • 1
    \$\begingroup\$ @officialaimm ..huh. It's probably that Processing.js hasn't yet downloaded/transpiled the source. When I get the time I'll make the run button blank out while its loading \$\endgroup\$ – dzaima Sep 15 '17 at 5:09
  • \$\begingroup\$ ah, makes sense. \$\endgroup\$ – officialaimm Sep 15 '17 at 5:59
1
\$\begingroup\$

Kotlin, 99 93 88 bytes

(('A'..'Y')+'Z'.downTo('A')).map{println((('A'..it-1)+it.downTo('A')).joinToString(""))}

That's still longer than I expected but I doubt I'll be able to golf it further.

Factorisation doesn't save the day, best I could do is 93 bytes :

fun r(a:Char,b:Char)=(a..b-1)+b.downTo(a)
r('A','Z').map{println(r('A',it).joinToString(""))}
\$\endgroup\$
1
\$\begingroup\$

brainfuck, 109 bytes

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

Try it online!

Explanation

+++++[<+++++>-] 26 times
<+[
  >>[>[.>]]     print every letter up to new char position
  >++++++++++<<[>>.<]  if any letter exists print lf
  >[<]>         resync pointer position
  --[<++++++++>-] set new char to A minus 1
  <[+.<]        increment each letter and print it
  <-            decrement counter
]
>>,             delete last letter
>[.>]           print rest of middle line
+++++ +++++.    print lf
[               while chars exist
  <[.<]         print all
  >,            delete last
  >[.>]<        print second half (including lf)
]

Alternative

If a leading newline would be alowed, this could also be done with 97 bytes.

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

Try it online!

Explanation

+++++[<+++++>-]     26 times
<+[
  >>[>[.>]]           print every letter up to new char position
  >++++++++++.        print lf
  --[<++++++++>-]     set new char to A minus 1
  <[+.<]              increment each letter and print it
  <-                  decrement counter
]
>>,                 delete last letter
>[.>]               print rest of middle line
+++++ +++++.        print lf
[                   while chars exist
  <[.<]               print all
  >,                  delete last
  >[.>]<              print second half (including lf)
]

Both codes need interpreters that use "end of input = 0".

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

Pepe, 94 bytes

rEeEeEEeEeREeEeeeeeEREEEEEEerEEReeeReeEREEEeEREEEeEREEeeREEEEEreererEEReeeReeEReReREEeEreeReee

Try it online!

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

Common Lisp, 170 bytes

(let ((r "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))(loop for i from 1 to 51 do(let ((l (subseq r 0 (- i (* 2 (- i 26) (floor i 26))))))(format t "~a~a~%" l (subseq (reverse l) 1)))))

Run it: TIO here.

Formatted:

(let ((r "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  (loop for i from 1 to 51
    do(let ((l (subseq r 0 (- i (* 2 (- i 26) (floor i 26))))))
      (format t "~a~a~%" l (subseq (reverse l) 1)))))

Breakdown:

  • (let ((r "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) - variable r holds string range of all characters
  • variable l holds a substring from the r of length based on the line number, where l = r[0:i - (2 * (i - 26) * i % 26)] ... (2 * (i - 26) * i % 26) this part ensures the i to be this sequence 0...26...0
  • format prints the l string and its reversed counter part with skipped first letter (to avoid ABBA, ABCCBA , etc.)
New contributor
David Horák is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • \$\begingroup\$ Welcome! Consider adding a detailed explanation. We also like to use online interpreters to allow easy testing of code. I like to use TIO. Here is its Common Lisp interpreter. It also has a permalink tool that can automatically generate markup for an answer post. \$\endgroup\$ – mbomb007 Jun 14 at 19:08
  • \$\begingroup\$ @mbomb007 Thank you for the suggestions, will add the notes. The REPL code is in the title "Common Lisp" ... the TIO looks better, good to know. \$\endgroup\$ – David Horák Jun 14 at 19:13
  • \$\begingroup\$ Ah, I didn't notice. Most answers with linked language names are links to the language's home page or documentation. \$\endgroup\$ – mbomb007 Jun 14 at 19:15
  • \$\begingroup\$ @mbomb007 updated with with the link to TIO and with the breakdown. ... Common Lisp is just my newest guilty pleasure, so there is definitely a better approach, just this is my current best try. Looking forward for any suggestions. \$\endgroup\$ – David Horák Jun 14 at 19:26
  • 1
    \$\begingroup\$ See if this helps: Tips for golfing is Lisp \$\endgroup\$ – mbomb007 Jun 14 at 19:33
0
\$\begingroup\$

Python 2, 86 85 bytes

n=0;exec'x=25-abs(n-25);print bytearray(65+x-abs(y-x)for y in range(2*x+1));n+=1;'*51

Try it at Ideone.

This follows a simple process. First, create the range [0, 1, ..., 24, 25, 24, ..., 1, 0]. Then, for each value x in that range, create another range [0, 1, ..., x, ..., 1, 0] and map each of those values to a character in 'A..Z'. Finally, print each string of characters.

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

JavaScript (ES6), 122 bytes

_=>(s='',r=x=>[...x].reverse().join``,t=[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].map(c=>s+c+r(s,s+=c)).join`
`)+r(t.slice(0,650))

As an anonymous function. (4 more bytes if you want to alert it)

Test

document.write('<pre>'+(

_=>(
  s='',
  r=x=>[...x].reverse().join``,
  t=
    [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ']
    .map(c=>
      s+c+r(s,s+=c)
    ).join`\n`
)+r(t.slice(0,650))

)());

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

Pyke, 19 16 bytes

27Gm<m[D_t+)RK[X

Try it here!

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