สร้างเมือง ABACABA


17

นี่คือเมืองที่ 3 ของ ABACABA:

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

มันทำมาจากลำดับABACABAซึ่งโดยพื้นฐานแล้ว:

  • A (การทำซ้ำครั้งที่ 1)
  • วาง B - AB
  • ทำซ้ำ A - ABA (การวนซ้ำครั้งที่ 2)
  • เพลส C - ABAC
  • ทำซ้ำ ABA - ABACABA (การทำซ้ำครั้งที่ 3)

และคุณจะได้รับความคิด

อาคารมีความสูง (ติดต่อกันโดยไม่ขีดเส้นใต้) เท่ากับตัวอักษรที่แปลงเป็นตัวเลขเป็น A = 1, B = 2 เป็นต้น

อินพุต

หมายเลขซ้ำ 1 <= n <= 26

เอาท์พุต

เมืองสั่งซื้อ ABACABA nรวมถึงตัวอักษรที่จุดเริ่มต้นของบรรทัด


@DonMuesli ฮ่าฮ่าใช่ จะเชื่อมโยงหลายมิติในคำถาม

1
เราต้องเอาท์พุทอะไรเมื่อจำนวนที่ได้รับสูงกว่า 26
Adnan

ใช่ได้โปรด: D (มันจะไม่ง่ายใช่ไหม?)

1
นั่นจะไม่นับเป็นอินพุตที่ถูกต้อง

2
อินพุตสามารถเป็นศูนย์และถ้าเป็นเช่นนั้นเอาต์พุตควรเป็นอย่างไร นอกจากนี้มันจะไม่เจ็บรายการแรกพูด 4 อินพุทและเอาท์พุทที่คาดหวัง
Zgarb

คำตอบ:


6

Python 2, 82 ไบต์

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

ฉันสังเกตว่าไม่มีใครโพสต์วิธีการเรียกซ้ำแบบไบนารีและตัดสินใจที่จะยิงมัน ... และตอนนี้ด้วยเคล็ดลับที่ยืมมาจาก Sherlock9 มันเป็นคำตอบที่สั้นที่สุดของงูหลาม! (และขอขอบคุณ xnor สำหรับการย่อให้สั้นลงอีกหนึ่งครั้ง) (แล้วเดนนิสผู้ซึ่งได้โกนอีกไม่กี่คน ... )

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())

ฉันคิดว่าฉันเข้าใจสิ่งนี้และมันค่อนข้างฉลาด ฉันพลาดการสอบถามที่ดีนี้ไปทั้งหมด คุณสามารถบันทึกตัวอักษรบางตัวได้โดยต่อเข้ากับทั้งสองด้านมากกว่าการจัดเก็บsและทำให้บรรทัดที่สองเป็นฟังก์ชั่น anon:f=lambda n:n*"?"and f(n-1)+"_"*(n-2)+"\n%c|%s|"%(64+n,"_"*n)+f(n-1);lambda n:" _"+f(n)
xnor

ฉันกำลังคิดที่จะทำสิ่งต่อไป ...
quintopia

@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)ควรทำงาน
Dennis

@Dennis ฉันขอแนะนำให้ใช้โซลูชันข้างต้นใน Pyth ฉันสงสัยว่ามันอาจจะสั้นกว่า 59 ไบต์ ... ฉันจะทำ แต่ ณ จุดนี้มันเป็นของฉันเพียงครึ่งเดียว ...
quintopia

1
81 ไบต์เป็นโปรแกรมความยาวเท่ากับฟังก์ชัน
xnor

3

Python 2, 99 ไบต์

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

ในการค้นหาiลำดับที่ th ของลำดับ ABACABA ให้เขียนเป็นiไบนารี่นับจำนวนของศูนย์ต่อท้ายและเพิ่มอีกหนึ่งเลข เราใช้เคล็ดลับบิตคลาสสิคi&-iเพื่อค้นหาพลังที่ยิ่งใหญ่ที่สุดของการ2หารiนั้นจากนั้นคำนวณความยาวบิต ที่จริงแล้วเรานับถอยหลังiจาก2**n-1ถึง0เป็นซึ่งก็ดีเพราะลำดับของ ABACABA นั้นสมมาตร

bเราจะติดตามทั้งจำนวนในปัจจุบันและลำดับสุดท้ายด้วยความช่วยเหลือของตัวแปร "ก่อนหน้านี้ที่" สิ่งนี้บอกเราว่าขีดเส้นใต้ที่จะพิมพ์เป็น "overhang" อาคารสุดท้ายจะถูกดึงมาได้อย่างถูกต้องโดยไม่ต้องยื่นเพราะจะถือว่าเป็นมีความยาวบิต01

รูปแบบสตริงสำหรับการพิมพ์นั้นมาจาก Sp3000เช่นเดียวกับเคล็ดลับในการใช้inputเพื่อพิมพ์บรรทัดแรก


3

MATL , 59 ไบต์

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

นี้ใช้รุ่นปัจจุบัน (15.0.0)ของภาษา

ลองออนไลน์!


(หากไม่จำเป็นต้องรวมตัวอักษรในผลลัพธ์: ตัวอักษรต่อไปนี้สามารถใช้งานได้ 48 ไบต์):

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

คำอธิบาย

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

คำตอบที่ดีมาก แต่คุณต้องเอาท์พุทตัวอักษรที่ด้านหน้าของอาคาร: p
Adnan

แก้ไข รอการชี้แจง OP อยู่ดี
Luis Mendo

1
จริง ๆ แล้วฉันถามสิ่งนี้ แต่ฉันลบความคิดเห็น นี่คือการตอบสนองแม้ว่า: p
Adnan

ทางออกที่หรูหรามาก

2

CJam, 37 35 ไบต์

SS'_Lri{[H)'_*_2>N@H'A+'|@'|6$]+}fH

นี้คือการดำเนินการซ้ำของขั้นตอนวิธีเวียนเกิดจากคำตอบของ @ quintopia

ลองออนไลน์!

มันทำงานอย่างไร

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#

1

JavaScript (ES6), 162 ไบต์

n=>(a=[...Array(1<<n)]).map((_,i)=>i?(a[i]=String.fromCharCode(64+(n=1+Math.log2(i&-i)))+`|${"_".repeat(n)}|`,a[i-1]+='_'.repeat(--n&&--n)):a[i]='  _')&&a.join`\n`

\nตัวอักษรขึ้นบรรทัดใหม่อยู่ที่ไหน


\nอยู่ที่ท้ายถ้าใครสงสัย
CalculatorFeline

1

Python 2, 123 121 ไบต์

f=lambda n:n*[n]and f(n-1)+[n]+f(n-1)
L=f(input('  _\n'))
for i,j in zip(L,L[1:]+L):print'%c|%s|'%(64+i,'_'*i)+'_'*(j+~i)

ลิงค์ ideone (-2 ไบต์ขอบคุณ @xsot)

fสร้างลำดับ ABACABA f(3) = [1, 2, 1, 3, 1, 2, 1]เป็นรายการของตัวเลขเช่น ออฟเซ็ตของอินพุต 1 เมื่อเทียบกับความท้าทายลำดับ ABACABAทำให้เราสามารถเล่นแบบไบท์fได้

บรรทัดแรกจะพิมพ์แยกกันหลังจากนั้นบรรทัดอื่นทั้งหมดจะถูกพิมพ์โดยใช้นิพจน์ซึ่งคำนึงถึงหมายเลขปัจจุบันและหมายเลขถัดไป input()เพียงเพื่อความสนุกสนานบรรทัดแรกจะมีการพิมพ์โดยใช้


คุณสามารถแทนที่ด้วย[0] L
xsot

@xsot Ah ขอบคุณที่ใช้งานได้ดี :) (เช่นเดียวกับ xnor โพสต์คำตอบ!)
Sp3000

1

Pyth - 64 62 ไบต์

น่าจะเป็นสนามกอล์ฟได้มากกว่านี้ แต่ก็ดีพอสำหรับตอนนี้

Lsl.&Jhb_J"  _"Vtt^2Qpr@G=ZyN1p"|_"p*\_Zp\|W<=hZyhNp\_)d)"A|_|

ลองที่นี่!

คำอธิบาย:

            |Predefined vars: Q = evaluated input, G = lowercase alphabet
L           |Lambda definition. y(b) = return (following code)
   .&       |bitwise and
     Jhb    |J = b + 1, pass b + 1 to the bitwise and
        _J  |-J
  l         | base 2
 s          |̲c̲o̲n̲v̲e̲r̲t̲ ̲t̲o̲ ̲i̲n̲t̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲
          "  _"                              |print "  _" with a trailing newline
               Vtt^2Q                        |For N in 2^Q - 2
                     pr      1               |print in caps
                         =ZyN                |Z = y(N) remember the first lambda?
                       @G                    |G[Z], basically convert 1-26 to A-Z
                              p"|_"          |print "|_", no trailing newline
                                   p*\_Z     |print "_" Z times
                                        p\|  |̲p̲r̲i̲n̲t̲ ̲"̲|̲"̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲
                                           W<=hZyhN             |While ++Z<y(N+1)
                                                   p\_          |print "_"
                                                      )k        |end while,
                                                                |print newline
                                                        )"A|_|  |end for,
                                                                |print "A|_|"

0

Python 3.5 - 262 236 220 ไบต์:

-16 ไบต์ขอบคุณ @CatsAreFluffy! ฟังก์ชั่นทั้งหมดของฉันตอนนี้สามารถอยู่ในบรรทัดเดียว :)

from collections import*
def a(v):o=OrderedDict;j=[chr(i+97)for i in range(26)];d=o((j[i],('  '+'_'*(i+1)+'\n'+j[i]+'|'+'_'*(i+1)+'|'))for i in range(26));f=lambda w:'a'[w:]or f(w-1)+j[w]+f(w-1);[print(d[g])for g in f(v)]

มันอาจจะยาวไปหน่อยและมันก็อาจจะพิมพ์บรรทัดใหม่ระหว่างสิ่งปลูกสร้าง แต่สิ่งที่มันต้องการ คุณสามารถทดสอบด้วยตัวเองเพื่อยืนยัน

แก้ไข:

รหัส golfed ก่อนหน้าของฉันไม่ได้พิมพ์รูปแบบที่ถูกต้อง แต่อย่างใด อย่างไรก็ตามตอนนี้สิ่งที่แสดงด้านบนทำและมันก็ดีในความคิดของฉัน คุณสามารถเรียกใช้ด้วยตัวคุณเองเพื่อยืนยันว่า

บันทึก:โปรแกรมจะพิมพ์อักษรตัวพิมพ์เล็กทั้งหมดที่อยู่ด้านหลัง "สิ่งปลูกสร้าง" ทั้งหมด ฉันหวังว่าไม่เป็นไร

เวอร์ชันที่ไม่อัปโหลดพร้อมคำอธิบาย:

from collections import*
def a(v):
    o=OrderedDict # Assign the OrderedSict function to "o"
    j=[chr(i+97)for i in range(26)] # Create a list with all 26 lowercase letters of the alphabet
    d=o((j[i],('  '+'_'*(i+1)+'\n'+j[i]+'|'+'_'*(i+1)+'|'))for i in range(26)) # Create a dict assigning each letter it's corresponding building with its corresponding length
    f=lambda w:'a'[w:]or f(w-1)+j[w]+f(w-1) # Return the ABACABA sequence based on the user input
    [print(d[g])for g in f(v)] # Print out the building according to the sequence returned by the above lambda function (thanks to @CatsAreFluffy for this method to print it! :) )

โดยทั่วไปสิ่งที่ฉันกำลังทำคือการนำเข้าฟังก์ชั่นพจนานุกรม Ordered Dictionary ของโมดูลคอลเลกชันจากนั้นสร้างพจนานุกรมที่เรียงลำดับด้วยตัวอักษรตัวเล็กแต่ละตัวในรายการ "j" ที่ถูกกำหนดให้กับสิ่งปลูกสร้างที่สอดคล้องกัน จากนั้นฉันคำนวณลำดับตามอินพุตของผู้ใช้โดยใช้f=lambda w:"a"[w:]or f(w-1)+j[w]+f(w-1)ฟังก์ชั่นแล้วตามลำดับที่ส่งคืนโดยสิ่งปลูกสร้างด้วยตัวอักษรที่สอดคล้องกันแต่ละอันด้านหลังจะถูกพิมพ์ออกมา


คุณสามารถนำเข้าOrderedDictเป็นoแทน? และการเปลี่ยนแปลงopไปpและitemจะjยังใช้งานได้
Rɪᴋᴇʀ

คุณสามารถวางif(ปัจจัยการผลิตทั้งหมดจะ1≤v≤26) เปลี่ยนแปลงrange(26)ไปrange(v)และใช้แทนreturn"\n".join(f(v)) for
CalculatorFeline

-2bytes: ใช้from collections import*และo=OrderedDictแทนfrom collections import OrderedDict as o
CalculatorFeline

@CatsAreFluffy ที่จริงการเปลี่ยนแปลงrange(26)เพื่อผลในrange(v) Index Errorการทำเช่นreturn"\n".join(f(v))นั้นจะส่งกลับเฉพาะลำดับ แต่ไม่ใช่ตัวอาคารเอง เคล็ดลับของคุณค่อนข้างดี ขอบคุณ! :)
R. Kap

ดีฉันไม่ได้ค่อนข้าง Python 3.5 (ฉันได้ 3.4.1) อาจถึงเวลาที่จะอัพเกรด ...
CalculatorFeline

0

ทับทิมขนาด 129 ไบต์

ฟังก์ชั่นไม่ระบุชื่อส่งคืนสตริงหลายบรรทัด

->x{a=->n{n<1?[]:(b=a[n-1];b+[n]+b)}
r="  _
"
a[x].zip(a[x][1,9**x]<<0).map{|n,m|r+=(64+n).chr+"|#{?_*n}|#{?_*(m+~n)if m>n}
"}
r}

0

JavaScript (ES6), 143

มีการขึ้นบรรทัดใหม่ 2 บรรทัดใน backticks ที่สำคัญและนับ

n=>`  _
`+(r=n=>n?[...r(n-1),n,...r(n-1)]:[])(n).map((x,i,t,u=n=>'|'+'_'.repeat(n>0&&n))=>String.fromCharCode(x+64)+u(x)+u(t[i+1]-x-1)).join`
`

... หรือ 138 ถ้าตัวอักษรสามารถเป็นตัวพิมพ์เล็กได้

n=>`  _
`+(r=n=>n?[...r(n-1),n,...r(n-1)]:[])(n).map((x,i,t,u=n=>'|'+'_'.repeat(n>0&&n))=>(x+9).toString(36)+u(x)+u(t[i+1]-x-1)).join`

น้อย golfed

n=>{
  // recursive function for ABACABA returning an array of integers
  var r=n=>n?[...r(n-1),n,...r(n-1)]:[]
  // function to build "|__..."
  // if argument <=0 just returns the vertical bar
  var u=n=>'|'+'_'.repeat(n>0&&n)
  var t = r(n)
  t = t.map((x,i)=>
    // current building + extension to the len of next building if needed
    String.fromCharCode(x+64)+u(x)+u(t[i+1]-x-1)
  )
  return ' _\n' // the top line is fixed
    + t.join('\n')
}

ทดสอบ

solution=
n=>`  _
`+(r=n=>n?[...r(n-1),n,...r(n-1)]:[])(n).map((x,i,t,u=n=>'|'+'_'.repeat(n>0&&n))=>String.fromCharCode(x+64)+u(x)+u(t[i+1]-x-1)).join`
`

function update() {
  var n=+N.value
  if (n>=0 && n<=26) O.textContent=solution(n)
}

update()
#N { width: 2em }
N:<input id=N value=4 oninput='update()'><pre id=O></pre>


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.