สามเหลี่ยมปาสคาลเป็นรายการสองมิติ


11

สร้างสามเหลี่ยมของ Pascal ที่เป็นรายการซ้อนกันและมีค่าศูนย์ในจุดที่ไม่ได้ใช้

ในอาร์เรย์ผลลัพธ์ตัวเลขของสามเหลี่ยมของ Pascal จะถูกคั่นด้วยศูนย์และเติมด้วยศูนย์ในแต่ละด้านเพื่อให้อยู่กึ่งกลาง ตัวอย่างเช่นแถวด้านล่าง (อาร์เรย์ย่อยสุดท้าย) ต้องไม่มีเลขศูนย์ทางซ้ายและขวา อาร์เรย์ย่อยที่สองมีการเว้นศูนย์หนึ่งครั้งในแต่ละด้านและอื่น ๆ

นี่คือเอาต์พุตสำหรับอินพุต5:

[[0,0,0,0,1,0,0,0,0],
[0,0,0,1,0,1,0,0,0],
[0,0,1,0,2,0,1,0,0],
[0,1,0,3,0,3,0,1,0],
[1,0,4,0,6,0,4,0,1]]

ตามปกติการแก้ปัญหาด้วยไบต์ที่น้อยที่สุดชนะ


5
ซ้ำกันนี้ การเปลี่ยนรูปแบบผลลัพธ์ไม่ได้เป็นการท้าทาย ลองโพสต์ใน Stack Overflow หากคุณยังต้องการความช่วยเหลือ
GamrCorps

2
ก็มีเลขศูนย์พิเศษอยู่
CalculatorFeline

โปรแกรมนี้พิมพ์รายการที่คุณต้องการ (Python 3):print("def pascal(n):\n #make the nested list\n a=[[0 for i in range(2*n+1)] for j in range(n+1)] #make the list\n a[0][n]=1 #add the initial 1\n for i in range(1,n+1):\n for j in range(2*n+1):\n a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(2*n+1)] #the main part\n return a")
CalculatorFeline

1
@CatsAreFluffy ศูนย์พิเศษเพียงแค่แทนที่ช่องว่างในการทำซ้ำก่อนหน้านี้ - นี่คือปัญหาเดียวกันแน่นอนหน้าที่
ricdesi

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

คำตอบ:




2

เยลลี่ 12 ไบต์

NR¬ṙ-,1S$³Ð¡

ลองที่นี่

คำอธิบาย

                   This is a list of functions, each operating on the input, n:
NR                 Get the range [-n -n+1 ... 0 ... n-1 n].
  ¬                Logical NOT the entire range: [0 0 ... 1 ... 0 0].
         ³Ð¡       Repeat n times, and cumulate the results:
   ṙ-,1                Rotate by both -1 and 1
       S               Sum the results.
        $              (Joins the above two functions)

1

Haskell, 66 ไบต์

q n|d<-0<$[2..n]=scanl(\(s:t)_->zipWith(+)(0:s:t)$t++[0])(d++1:d)d

ตัวอย่างการใช้งาน: ->q 4[[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,2,0,1,0],[1,0,3,0,3,0,1]]

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

d <- 0<$[2..n]                      -- bind d to a list of (length n)-1 zeros
scanl                               -- build a list
                         (d++1:d)   -- starting with  [d ++ 1 ++ d]
      \(s:t)_                    d  -- by combining the previous element with the
                                    -- elements of d, but ignoring them, i.e.
                                    -- build a list of (length d) by repeatedly
                                    -- modifying the start element by
          zipWith(+)                -- adding element-wise
                    (0:s:t)         -- the previous element prepended by 0  
                           t++[0]   -- and the tail of the previous element
                                    -- followed by a 0 

1

Python 3, 172 158 133 ไบต์

def p(n):
 x=2*n+1;y=range
 a=[[0]*x]*n;a[0][n]=1
 for i in y(1,n+1):
  for j in y(x):a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(x)]
 return a

ทำให้ดีขึ้นเรื่อย ๆ


1
นี่มันยังไม่สมบูรณ์ใช่ไหม?
แมว

อืมใช่ นี้ (ในรูปแบบ golfed น้อยกว่าเล็กน้อย) ถูกพิมพ์โดยโปรแกรมที่ฉันทิ้งไว้ให้ความเห็นเกี่ยวกับคำถาม
CalculatorFeline

1

MATL , 24 22 21 ไบต์

tEq:=Gq:"t5BX+8L)]N$v

แก้ไข (20 พฤษภาคม 2016): ณ ภาษารุ่น 18.0.0 ของรหัสข้างต้นต้องมีการเปลี่ยนแปลงเล็กน้อยเพื่อให้ทำงานได้ ลิงค์ด้านล่างรวมถึงการแก้ไขเหล่านั้น

ลองออนไลน์!

ใช้การวนซ้ำเพื่อผลักแถวใหม่แต่ละแถวลงบนสแต็ก แถวใหม่จะคำนวณจากแถวก่อนหน้าโดยใช้การแปลงด้วย[1,0,1]และรักษาเฉพาะขนาดที่ต้องการ หลังจากวนรอบแถวทั้งหมดจะถูกต่อกันเป็นอาร์เรย์ 2D ซึ่งจะปรากฏขึ้น อาร์เรย์ 2 มิติแสดงใน MATL เป็นตารางตัวเลขที่เรียงตามแนวคอลัมน์

t           % implicit input n. Duplicate
Eq          % 2*n-1
:           % range [1,2,...,2*n-1]
=           % gives [0,0,...1,...0,0]. This is the first row
Gq:         % range [1,2,...,n-1]
"           % for each. Repeat n-1 times
  t         %   duplicate latest row. This duplicate will become the next row
  5B        %   push array [1,0,1] (5 converted to binary)
  X+        %   convolution
  8L        %   predefined literal [2,-1i]. Used for indexing
  )         %   apply that index: remove one element at each end
]           % end for each
N$v         % concatenate all rows into a 2D array. Implicitly display

0

Javascript, 152 146 bytes

f=i=>[...Array(i)].map((x,j)=>(z=[...Array(i*2-1)].map((_,k)=>+!!~[i-j,i+j].indexOf(k+1)),y=j?z.map((_,k)=>_||(k&&(k+1 in y)?y[k-1]+y[k+1]:_)):z))


0

อย่างจริงจัง 33 ไบต์

╩╜r`╣;lD0nkdZΣ`M╜rRZ`i0nkd@;)kΣ`M

ลองออนไลน์

ฉันค่อนข้างแน่ใจว่าอย่างน้อย 7 หรือมากกว่านั้นของไบต์เหล่านั้นสามารถโกนออกดังนั้นฉันจะรอที่จะโพสต์คำอธิบายจนกว่าฉันจะเล่นกอล์ฟนี้ต่อไป


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