Domanda di colloquio di Meta

Pascal's Triangle - print a row

Risposte di colloquio

Anonimo

2 feb 2012

Any given row? So: // n is the row desired, and i'm assuming 1-index on rows, i.e. // the 1st row is 1, 2nd is 1, 1 rathere than 0th row is 1 void pascal(int a[ ], int size, int n) { n--; if(n == 0) { for(i = 0; i < size; i++) printf("%d", a[i]); return; } int b[size + 1]; b[0] = 1; b[size + 1] = 1; for(int i = 1; i < size; i++) b[i] = a[i - 1] + a[i]; pascal(b, size + 1, n); }

Anonimo

30 nov 2010

Just prints the numbers; doesn't handle spatial separators between numbers. // Computes the value at row r and column c def v(r:Int, c:Int) : Int = if (c == 0) 1 else v(r, c-1) * (r-c)/c def printRow(r:Int) = for (col <- 0 to r) print( v(r+1, col) )