1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/usr/bin/gawk
# Challenge 003
#
# Challenge #2
# Create a script that generates Pascal Triangle. Accept number of rows from
# the command line. The Pascal Triangle should have at least 3 rows. For more
# information about Pascal Triangle, check this wikipedia page.
function alen(a, i, k) {
k = 0;
for(i in a) k++;
return k;
}
function push(a, v, n) {
n = alen(a);
while (n in a) n++;
a[n] = v;
}
function spaces(n, i, output) {
output = "";
for (i = 0; i < n; i++)
output = output " ";
return output;
}
function join(array, start, end, sep, result, i) {
if (sep == "")
sep = " ";
else if (sep == SUBSEP) # magic value
sep = "";
result = array[start];
for (i = start + 1; i <= end; i++)
result = result sep array[i];
return result;
}
function draw_pascal(rows, row, r, i, nrow) {
row[0] = 1;
print spaces(rows-1) join(row, 0, alen(row)-1, " ");
for (r = 1; r < rows; r++) {
# compute next row
for (i in nrow) delete nrow[i];
nrow[0] = 1;
for (i = 0; i < alen(row)-1; i++)
push(nrow, row[i]+row[i+1]);
push(nrow, 1);
# swap row
for (i in nrow)
row[i] = nrow[i];
# print
print spaces(rows-r-1) join(row, 0, alen(row)-1, " ");
}
}
BEGIN {
n = ARGV[1] ? ARGV[1] : 5;
draw_pascal(n);
exit(0);
}
|