diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
| commit | ef14c20e2e2d61abe4cc73e5389733122df02d42 (patch) | |
| tree | d4346068d07aac04bae6094c595e1350fa6fe2c2 /challenge-003 | |
| parent | 40abdaf0147a405f36b5d43d7919eda15e131a3d (diff) | |
| download | perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.gz perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.bz2 perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.zip | |
Add Awk solution to challenge 004
Fix Awk syntax on previouws submissions - semicolon is needed at the end of statements
Diffstat (limited to 'challenge-003')
| -rw-r--r-- | challenge-003/paulo-custodio/awk/ch-1.awk | 50 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/awk/ch-2.awk | 51 |
2 files changed, 50 insertions, 51 deletions
diff --git a/challenge-003/paulo-custodio/awk/ch-1.awk b/challenge-003/paulo-custodio/awk/ch-1.awk index 12c7cea74a..024d960e91 100644 --- a/challenge-003/paulo-custodio/awk/ch-1.awk +++ b/challenge-003/paulo-custodio/awk/ch-1.awk @@ -12,54 +12,54 @@ function min(a, b) { } function min3(a, b, c) { - return min(a, min(b, c)) + return min(a, min(b, c)); } function alen(a, i, k) { - k = 0 - for(i in a) k++ - return 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 + n = alen(a); + while (n in a) n++; + a[n] = v; } function shift(a, i, v) { - v = a[0] + v = a[0]; for (i=1; i<alen(a); i++) - a[i-1] = a[i] - delete a[alen(a)-1] - return v + a[i-1] = a[i]; + delete a[alen(a)-1]; + return v; } function hamming( n) { # get the smallest of the multiples - n = min3(seq2[0], seq3[0], seq5[0]) + n = min3(seq2[0], seq3[0], seq5[0]); # shift used multiples - if (seq2[0] == n) shift(seq2) - if (seq3[0] == n) shift(seq3) - if (seq5[0] == n) shift(seq5) + if (seq2[0] == n) shift(seq2); + if (seq3[0] == n) shift(seq3); + if (seq5[0] == n) shift(seq5); # push next multiple - push(seq2, n*2) - push(seq3, n*3) - push(seq5, n*5) + push(seq2, n*2); + push(seq3, n*3); + push(seq5, n*5); - return n + return n; } BEGIN { # sequences of hamming numbers 2*n, 3*n, 5*n - seq2[0] = 1 - seq3[0] = 1 - seq5[0] = 1 + seq2[0] = 1; + seq3[0] = 1; + seq5[0] = 1; - n = ARGV[1] ? ARGV[1] : 5 + n = ARGV[1] ? ARGV[1] : 5; for (i=0; i<n; i++) - print hamming() - exit(0) + print hamming(); + exit 0; } diff --git a/challenge-003/paulo-custodio/awk/ch-2.awk b/challenge-003/paulo-custodio/awk/ch-2.awk index ab7703737c..40522f9b12 100644 --- a/challenge-003/paulo-custodio/awk/ch-2.awk +++ b/challenge-003/paulo-custodio/awk/ch-2.awk @@ -8,58 +8,57 @@ # information about Pascal Triangle, check this wikipedia page. function alen(a, i, k) { - k = 0 - for(i in a) k++ - return 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 + n = alen(a); + while (n in a) n++; + a[n] = v; } function spaces(n, i, output) { - output = "" + output = ""; for (i = 0; i < n; i++) - output = output " " - return output + output = output " "; + return output; } function join(array, start, end, sep, result, i) { if (sep == "") - sep = " " + sep = " "; else if (sep == SUBSEP) # magic value - sep = "" - result = array[start] + sep = ""; + result = array[start]; for (i = start + 1; i <= end; i++) - result = result sep array[i] - return result + 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, " ") + 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 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) + push(nrow, row[i]+row[i+1]); + push(nrow, 1); # swap row for (i in nrow) - row[i] = nrow[i] + row[i] = nrow[i]; # print - print spaces(rows-r-1) join(row, 0, alen(row)-1, " ") + print spaces(rows-r-1) join(row, 0, alen(row)-1, " "); } } BEGIN { - n = ARGV[1] ? ARGV[1] : 5 - draw_pascal(n) - exit(0) - + n = ARGV[1] ? ARGV[1] : 5; + draw_pascal(n); + exit(0); } |
