Create an ABACABA city

Here is the 3rd ABACABA city:

_
A|_|
B|__|
A|_|_
C|___|
A|_|
B|__|
A|_|

It is made out of the ABACABA sequence, which is basically:

• A (1st iteration)
• place B - AB
• repeat A - ABA (2nd iteration)
• Place C - ABAC
• Repeat ABA - ABACABA (3rd iteration)

and you get the idea.

The buildings have a height (corresponded by no. of underscores) equal to the letters converted to numbers as A = 1, B = 2 e.t.c.

Input

An iteration number 1<=n<=26.

Output

The ABACABA city of order n, including the letters at the start of the lines.

• @DonMuesli Haha yes. Will hyperlink in question.
– user51533
Mar 25 '16 at 21:37
• What do we need to output when the number gets higher than 26? Mar 25 '16 at 21:42
• Yes please :D (it wasn't going to be easy was it?)
– user51533
Mar 25 '16 at 21:42
• That won't count as valid input.
– user51533
Mar 25 '16 at 21:43
• Can the input be zero, and if so, what should the output be? Also, it wouldn't hurt to list the first, say, 4 inputs and expected outputs. Mar 25 '16 at 22:03

Python 2, 82 bytes

f=lambda n,s=1:n*"'"and"  _"*s+f(n-1,0)+"_"*(n-2)+"\n%c|%s|"%(64+n,"_"*n)+f(n-1,0)

I noticed no one had posted the binary recursion method and decided to give it a shot...and now with a trick borrowed from Sherlock9, it is the shortest python answer! (Also, thanks to xnor for one more shortening.) (And then Dennis who shaved a handful more...)

Ungolfed:

def f(n,s=1):
if n>0:
strsofar = "  _" if s==1 else ""        #prepend overhang for top-level call
strsofar += f(n-1,0)                    #build the city above the current line
strsofar += "_"*(n-2)                   #add the overhang to reach the current tower
strsofar += "\n%c|%s|" % (64+n, "_"*n)  #write the current (center) line
strsofar += f(n-1,0)                    #build the city below the current line
return strsofar
else:
return ""                               #only this line will be executed when n==0 (base case)

print "  _"+f(input())
• I think I understand this and it's quite clever. I had totally missed this nice recursion. You can save some chars by concatenating onto both sides rather than storing s, and by making the second line an anon function: f=lambda n:n*"?"and f(n-1)+"_"*(n-2)+"\n%c|%s|"%(64+n,"_"*n)+f(n-1);lambda n:" _"+f(n)
– xnor
Mar 30 '16 at 4:27
• I was thinking about doing that next... Mar 30 '16 at 4:35
• @quintopia f=lambda n,s=1:n*"_"and" _"*s+f(n-1,0)+"_"*(n-2)+"\n%c|%s|"%(64+n,"_"*n)+f(n-1,0) should work. Mar 30 '16 at 22:47
• @Dennis i suggest implementing the above solution in Pyth. I suspect it could be shorter than 59 bytes... I would do it, but at this point, it's only half mine... Mar 31 '16 at 4:06
• 81 bytes as program, same length as a function.
– xnor
Apr 2 '16 at 6:41

Python 2, 99 bytes

b=1;i=2**input('  _\n')-1
while i:i-=1;a=len(bin(i&-i))-2;print'%c|%s|'%(64+b,'_'*b)+'_'*(a+~b);b=a

To find the ith number of the ABACABA sequence, write i in binary, count the number of trailing zeroes, and add one. We use the classic bit trick i&-i to find the greatest power of 2 that divides i, then compute bit length. Actually, we count i down from 2**n-1 to 0, which is fine because the ABACABA sequence is symmetric.

We track both the current and last number of the sequence with the help of a "previous" variable b. This tells us how many underscores to print as the "overhang". The final building is drawn correctly without overhang because 0 is treated as have bit length 1.

The string format for printing is taken from Sp3000, as is the trick of using the input to print the first line.

MATL, 59 bytes

vi:"t@wv]!-1hPXJtPvX>"'|'@Z"63+h6MJ2+X@)(]XhcJ64+!wh!32H:(!

This uses current release (15.0.0) of the language.

Try it online!

(If the letters didn't have to be included in the output: the following would work, 48 bytes):

vi:"t@wv]!-1hPXJtPvX>"' |'X@1=o)@Z"63+h6MJ2+X@)(

Explanation

v        % vertically concatenate the stack contents: gives an empty array
i:       % input number n. Generate [1,2,...,n]
"        % for each k in [1,2,...n]
t      %   duplicate
@      %   push k
wv     %   swap, vertically concatenate
]        % end. Poduces the numeric ABACABA: [1 2 1 3 1 2 1]: ceiling heights
!        % transpose into a row
-1h      % append -1
PXJ      % reverse array. Copy into clipboard J
tP       % duplicate. Reverse again, so undo the reversing
v        % vertically concatenate reversed and non-reversed row arrays
X>       % max of each column. Gives array of wall heights: [1 2 2 3 3 2 2 1]
"        % for each value in that array
'|'    %   push "floor" char
@      %   push height
Z"     %   create string with that many spaces
63+    %   transform spaces into "wall" chars, '_'
h      %   concatenate horizontally
6M     %   push "floor" char '|' again, to be used as ceiling
J      %   push array of ceiling heights
2+X@)  %   index into that to get height of current building
(      %   at that position, overwrite the string with '|'
]        % end
Xhc      % concatenate all strings into a 2D char array, padding with spaces
J        % push array of ceiling heights (numeric ABACABA sequence)
64+      % add 64 to transform into letters
!        % transpose into column array
wh       % swap, concatenate horizontally. This appends letters below the floor
!        % transpose
32H:(    % overwrite first two positions (in linear order) with spaces
!        % transpose back. Implicitly display
• Very nice answer, but you also need to output the letters in front of the buildings :p. Mar 25 '16 at 22:29
• Solved. Waiting for OP clarification anyway Mar 25 '16 at 22:46
• I actually already asked this, but I deleted my comment. This was the response though :p. Mar 25 '16 at 22:47
• A very elegant solution.
– user51533
Mar 26 '16 at 7:17

CJam, 37 35 bytes

SS'_Lri{[H)'_*_2>N@H'A+'|@'|6$]+}fH This is an iterative implementation of the recursive algorithm from @quintopia's answer. Try it online! How it works SS'_ e# Push two spaces and an underscore. L e# Push "". ri e# Read an integer I from STDIN. { e# For each H in [0 ... I-1]: [ e# Set an array marker. H) e# Push Push H+1. '_* e# Push a string of that many underscores. _2> e# Push a copy and remove the first two underscores. N e# Push a linefeed. @ e# Rotate the longer string of underscores on top of it. h'A+ e# Add H to the character 'A', pushing the corresponding letter. '| e# Push a vertical bar. @ e# Rotate the string of underscores on top of it. '| e# Push another vertical bar. 6$  e#    Push a copy of the previous iteration (initially "").
]     e#   Wrap everything up to the former marker in an array.
}fH     e#