diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-28 00:33:56 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-28 00:33:56 +0000 |
| commit | e233d3a019dda275d8d790076d25ec92e47b4ffe (patch) | |
| tree | 48516daf26833baca8695204a4d83c81d58a6dd7 /challenge-003/paulo-custodio | |
| parent | 022a9f62eda3b5e188b9f6719a63566d9d7efc8b (diff) | |
| download | perlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.tar.gz perlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.tar.bz2 perlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.zip | |
Add Basic, C, C++, Forth, Lua and Python to challenge 003
Diffstat (limited to 'challenge-003/paulo-custodio')
| -rw-r--r-- | challenge-003/paulo-custodio/basic/ch-1.bas | 148 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/basic/ch-2.bas | 26 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/c/ch-1.c | 95 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/c/ch-2.c | 56 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/cpp/ch-1.cpp | 52 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/cpp/ch-2.cpp | 44 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/forth/ch-1.fs | 90 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/forth/ch-2.fs | 63 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/lua/ch-1.lua | 36 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/lua/ch-2.lua | 30 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/perl/ch-2.pl | 11 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/python/ch-2.py | 26 | ||||
| -rw-r--r-- | challenge-003/paulo-custodio/test.pl | 58 |
15 files changed, 764 insertions, 20 deletions
diff --git a/challenge-003/paulo-custodio/basic/ch-1.bas b/challenge-003/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..83b3973361 --- /dev/null +++ b/challenge-003/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,148 @@ +' Challenge 003 +' +' Challenge #1 +' Create a script to generate 5-smooth numbers, whose prime divisors are less +' or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +' information, please check this wikipedia. + +' deque of integers +type ElemType + nValue as integer + pNext as ElemType ptr +end type + +type QType + pFront as ElemType ptr + pBack as ElemType ptr +end type + +function QEmpty(byref q as QType) as boolean + if q.pFront = 0 then + QEmpty = true + else + QEmpty = false + end if +end function + +function QFront(byref q as QType) as integer + QFront = q.pFront->nValue +end function + +function QBack(byref q as QType) as integer + QBack = q.pBack->nValue +end function + +sub QPushBack(byref q as QType, nValue as integer) + dim pElem as ElemType ptr + + pElem = callocate(1, sizeof(ElemType)) + pElem->nValue = nValue + + if q.pFront = 0 then + q.pFront = pElem + q.pBack = pElem + else + q.pBack->pNext = pElem + q.pBack = pElem + end if +end sub + +sub QPopBack(byref q as QType) + dim pElem as ElemType ptr + + if q.pFront = 0 then ' empty + elseif q.pFront = q.pBack then ' only one element + deallocate q.pFront + q.pFront = 0 + q.pBack = 0 + else ' more than one element + pElem = q.pFront + do while pElem->pNext <> q.pBack + pElem = pElem->pNext + loop + deallocate q.pBack + q.pBack = pElem + pElem->pNext = 0 + end if +end sub + +sub QPushFront(byref q as QType, nValue as integer) + dim pElem as ElemType ptr + + pElem = callocate(1, sizeof(ElemType)) + pElem->nValue = nValue + + if q.pFront = 0 then + q.pFront = pElem + q.pBack = pElem + else + pElem->pNext = q.pFront + q.pFront = pElem + end if +end sub + +sub QPopFront(byref q as QType) + dim pElem as ElemType ptr + + if q.pFront = 0 then ' empty + elseif q.pFront = q.pBack then ' only one element + deallocate q.pFront + q.pFront = 0 + q.pBack = 0 + else ' more than one element + pElem = q.pFront + q.pFront = pElem->pNext + deallocate pElem + end if +end sub + + +function min(a as integer, b as integer) as integer + if a < b then + min = a + else + min = b + end if +end function + +function min3(a as integer, b as integer, c as integer) as integer + min3 = min(a, min(b, c)) +end function + + +' Hamming generator +dim shared q2 as QType +dim shared q3 as QType +dim shared q5 as QType + +function next_hamming() as integer + dim n as integer + + ' init queues at start + if QEmpty(q2) then + QPushBack q2, 1 + QPushBack q3, 1 + QPushBack q5, 1 + end if + + ' get the smallest of the queue heads + n = min3(QFront(q2), QFront(q3), QFront(q5)) + + ' shift used multiples + if n = QFront(q2) then QPopFront q2 + if n = QFront(q3) then QPopFront q3 + if n = QFront(q5) then QPopFront q5 + + ' push next multiples + QPushBack q2, 2*n + QPushBack q3, 3*n + QPushBack q5, 5*n + + next_hamming = n +end function + +' main +dim i as integer +for i=1 to val(command(1)) + print trim(str(next_hamming())) +next i diff --git a/challenge-003/paulo-custodio/basic/ch-2.bas b/challenge-003/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..5987ed7ee5 --- /dev/null +++ b/challenge-003/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,26 @@ +' 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. + +sub draw_pascal(rows as integer) + dim n(1, rows) as integer + dim cur as integer, row as integer, col as integer + + cur = 0 + n(cur, 0) = 1 + for row=1 to rows + ' print current row + print space(rows-row); + for col=0 to row-1: print trim(str(n(cur,col)));" ";: next col: print + + ' compute next row + n(1-cur, 0) = 1: n(1-cur,row) = 1 + for col=1 to row-1: n(1-cur,col) = n(cur,col-1)+n(cur,col): next col + cur = 1-cur + next row +end sub + +draw_pascal val(command(1)) diff --git a/challenge-003/paulo-custodio/c/ch-1.c b/challenge-003/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..1558785351 --- /dev/null +++ b/challenge-003/paulo-custodio/c/ch-1.c @@ -0,0 +1,95 @@ +/* +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less +or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. + +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> + +// implement deque +#define Q_SIZE 100 + +typedef struct Q { + size_t front; + size_t back; + int data[Q_SIZE]; +} Q; + +bool q_empty(Q* q) { + return q->front == q->back; +} + +int q_front(Q* q) { + return q->data[q->front]; +} + +int q_back(Q* q) { + return q->data[(q->back + Q_SIZE - 1) % Q_SIZE]; +} + +void q_push_back(Q* q, int n) { + q->data[q->back] = n; + q->back = (q->back + 1) % Q_SIZE; +} + +void q_pop_back(Q* q) { + q->back = (q->back + Q_SIZE -1) % Q_SIZE; +} + +void q_push_front(Q* q, int n) { + q->front = (q->front + Q_SIZE - 1) % Q_SIZE; + q->data[q->front] = n; +} + +void q_pop_front(Q* q) { + q->front = (q->front +1) % Q_SIZE; +} + +int min(int a, int b) { + return a<b ? a : b; +} + +int min3(int a, int b, int c) { + return min(a, min(b, c)); +} + +// hamming number generator +Q q2, q3, q5; + +int next_hamming(void) { + // init three queues with 1 + if (q_empty(&q2)) { + q_push_back(&q2, 1); + q_push_back(&q3, 1); + q_push_back(&q5, 1); + } + + // get the smallest of the queue heads + int n = min3(q_front(&q2), q_front(&q3), q_front(&q5)); + + // shift used multiples + if (n == q_front(&q2)) q_pop_front(&q2); + if (n == q_front(&q3)) q_pop_front(&q3); + if (n == q_front(&q5)) q_pop_front(&q5); + + // push next multiples + q_push_back(&q2, 2*n); + q_push_back(&q3, 3*n); + q_push_back(&q5, 5*n); + + return n; +} + +int main(int argc, char* argv[]) { + if (argc == 2) { + for (int i = 0; i < atoi(argv[1]); i++) + printf("%d\n", next_hamming()); + } +} diff --git a/challenge-003/paulo-custodio/c/ch-2.c b/challenge-003/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..ac5d68fd9a --- /dev/null +++ b/challenge-003/paulo-custodio/c/ch-2.c @@ -0,0 +1,56 @@ +/* +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. + +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +void draw_pascal(int rows) { + // allocate current and next row + int* data[2]; + for (int i = 0; i < 2; i++) + data[i] = check_mem(calloc(rows+1, sizeof(int))); + + int cur = 0; + data[cur][0] = 1; + for (int row = 1; row <= rows; row++) { + // print current row + for (int col = 0; col < rows-row; col++) + putchar(' '); + for (int col = 0; col < row; col++) + printf("%d ", data[cur][col]); + putchar('\n'); + + // compute next row + int nxt = 1-cur; + data[nxt][0] = 1; + data[nxt][row] = 1; + for (int col = 1; col < row; col++) + data[nxt][col] = data[cur][col-1] + data[cur][col]; + cur = nxt; + } + + // free data + for (int i = 0; i < 2; i++) + free(data[i]); +} + +int main(int argc, char* argv[]) { + if (argc == 2) + draw_pascal(atoi(argv[1])); +} diff --git a/challenge-003/paulo-custodio/cpp/ch-1.cpp b/challenge-003/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..d9ca7ee9f0 --- /dev/null +++ b/challenge-003/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,52 @@ +/* +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less +or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. + +*/ + +#include <iostream> +#include <deque> +#include <string> +#include <algorithm> + +int min3(int a, int b, int c) { + return std::min(a, std::min(b, c)); +} + +// hamming number generator +std::deque<int> q2, q3, q5; + +int next_hamming() { + // init three queues with 1 + if (q2.empty()) { + q2.push_back(1); + q3.push_back(1); + q5.push_back(1); + } + + // get the smallest of the queue heads + int n = min3(q2.front(), q3.front(), q5.front()); + + // shift used multiples + if (n == q2.front()) q2.pop_front(); + if (n == q3.front()) q3.pop_front(); + if (n == q5.front()) q5.pop_front(); + + // push next multiples + q2.push_back(2*n); + q3.push_back(3*n); + q5.push_back(5*n); + + return n; +} + +int main(int argc, char* argv[]) { + if (argc == 2) { + for (int i = 0; i < atoi(argv[1]); i++) + std::cout << next_hamming() << std::endl; + } +} diff --git a/challenge-003/paulo-custodio/cpp/ch-2.cpp b/challenge-003/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..cf64c51a86 --- /dev/null +++ b/challenge-003/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,44 @@ +/* +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. + +*/ + +#include <iostream> +#include <string> +#include <vector> + +void draw_pascal(int rows) { + // allocate current and next row + std::vector<int> data[2]; + for (int i = 0; i < 2; i++) + data[i].resize(rows+1); + + int cur = 0; + data[cur][0] = 1; + for (int row = 1; row <= rows; row++) { + // print current row + for (int col = 0; col < rows-row; col++) + std::cout << ' '; + for (int col = 0; col < row; col++) + std::cout << data[cur][col] << ' '; + std::cout << std::endl; + + // compute next row + int nxt = 1-cur; + data[nxt][0] = 1; + data[nxt][row] = 1; + for (int col = 1; col < row; col++) + data[nxt][col] = data[cur][col-1] + data[cur][col]; + cur = nxt; + } +} + +int main(int argc, char* argv[]) { + if (argc == 2) + draw_pascal(atoi(argv[1])); +} diff --git a/challenge-003/paulo-custodio/forth/ch-1.fs b/challenge-003/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..a3c140dee4 --- /dev/null +++ b/challenge-003/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,90 @@ +#! /usr/bin/env gforth + +\ Challenge 003 +\ +\ Challenge #1 +\ Create a script to generate 5-smooth numbers, whose prime divisors are less +\ or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +\ information, please check this wikipedia. + +\ circular queue of integers +1024 CONSTANT Q_SIZE \ must be power of 2 + +: q.pFront ( q -- front-addr ) ; +: q.pBack ( q -- back-addr ) 1 CELLS + ; +: q.pData ( q -- data-addr ) 2 CELLS + ; +: e.empty ( q -- f ) DUP q.pFront @ Q_SIZE MOD + SWAP q.pBack @ Q_SIZE MOD = ; +: q.pElem ( q index - addr ) Q_SIZE MOD CELLS SWAP q.pData + ; + +: create_q ( -- ) + CREATE 0 , 0 , Q_SIZE ALLOT \ front back data...data +; + +: q.Front ( q -- front-elem ) + DUP q.pFront @ ( q index ) + q.pElem @ +; + +: q.Back ( q -- back-elem ) + DUP q.pBack @ 1- ( q index ) + q.pElem @ +; + +: q.PushBack ( q elem -- ) + OVER q.pBack @ ( q elem back ) + 2 PICK SWAP ( q elem q back ) + q.pElem ( q elem elem-addr ) + ! ( q ) + q.pBack 1 SWAP +! \ increment +; + +: q.PopBack ( q -- ) + q.pBack -1 SWAP +! +; + +: q.PushFront ( q elem -- ) + OVER q.pFront -1 SWAP +! \ decrement front + SWAP dup q.pFront @ q.pElem ! +; + +: q.PopFront ( q -- ) + q.pFront 1 SWAP +! +; + + +\ queue or powers of 2, 3 and 5 +create_q q2 q2 1 q.PushBack +create_q q3 q3 1 q.PushBack +create_q q5 q5 1 q.PushBack + + +\ generate the next hamming number +: next_hamming ( -- n ) + \ get the smallest of the queue heads + q2 q.Front q3 q.Front MIN q5 q.Front MIN ( n ) + + \ shift used multiples + q2 q.Front OVER = IF q2 q.PopFront THEN + q3 q.Front OVER = IF q3 q.PopFront THEN + q5 q.Front OVER = IF q5 q.PopFront THEN + + \ push next multiples + 2 OVER * q2 SWAP q.PushBack + 3 OVER * q3 SWAP q.PushBack + 5 OVER * q5 SWAP q.PushBack +; + + +\ generate sequence of hamming numbers +: hamming ( n -- ) + 0 ?DO + next_hamming . CR + LOOP +; + + +\ get command line argument, call hamming +NEXT-ARG S>NUMBER? 0= THROW DROP +hamming +BYE diff --git a/challenge-003/paulo-custodio/forth/ch-2.fs b/challenge-003/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..9c8084b4df --- /dev/null +++ b/challenge-003/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,63 @@ +#! /usr/bin/env gforth + +\ 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. + +100 CONSTANT MAX_ROWS + +\ array to hold current and next row +CREATE data MAX_ROWS 2* CELLS ALLOT + +\ toggle for current row and function to compute next row +0 VALUE cur \ 0/1 of current data +: nxt 1 cur - ; \ 1/0 of next data +: toggle 1 cur - TO cur ; \ toggle current + +\ index current and next arrays +: data[] ( index cur -- addr ) + SWAP CELLS SWAP ( index*cells cur ) + MAX_ROWS CELLS * + ( index*cells cur*max_rows ) + data + +; + +: cur[] ( index -- addr ) cur data[] ; +: nxt[] ( index -- addr ) nxt data[] ; + + +: print_row ( rows row -- ) + 2DUP - SPACES + NIP ( row ) + 0 ?DO + I cur[] @ . + LOOP + CR +; + +: next_row ( row -- ) + 1 0 nxt[] ! + 1 OVER nxt[] ! + 1 ?DO + I 1- cur[] @ i cur[] @ + + I nxt[] ! + LOOP + toggle +; + +: draw_pascal ( rows ) + 1 0 cur[] ! + DUP 1+ 1 ?DO + DUP I print_row + I next_row + LOOP + DROP +; + + +\ get command line argument, call draw_pascal +NEXT-ARG S>NUMBER? 0= THROW DROP +draw_pascal +BYE diff --git a/challenge-003/paulo-custodio/lua/ch-1.lua b/challenge-003/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..525469191a --- /dev/null +++ b/challenge-003/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,36 @@ +#!/usr/bin/env lua + +--[[ +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less +or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. +--]] + +-- sequence is a merge of all multiples of 2, 3 and 5 +seq2 = {1} +seq3 = {1} +seq5 = {1} + +function next_hamming() + -- get the smallest of the queue heads + n = math.min(seq2[1], seq3[1], seq5[1]) + + -- shift used multiples + if n == seq2[1] then table.remove(seq2, 1); end + if n == seq3[1] then table.remove(seq3, 1); end + if n == seq5[1] then table.remove(seq5, 1); end + + -- push next multiples + table.insert(seq2, 2*n) + table.insert(seq3, 3*n) + table.insert(seq5, 5*n) + + return n +end + +for i=1,tonumber(arg[1]) do + io.write(next_hamming(), "\n") +end diff --git a/challenge-003/paulo-custodio/lua/ch-2.lua b/challenge-003/paulo-custodio/lua/ch-2.lua new file mode 100644 index 0000000000..d8c661014c --- /dev/null +++ b/challenge-003/paulo-custodio/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/usr/bin/env lua + +--[[ +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 draw_pascal(rows) + data = {1} + for row=1,rows do + -- print current row + for col=1,rows-row do io.write(" "); end + for col=1,row do io.write(data[col], " "); end + io.write("\n") + + -- compute next row + nxt = {1} + for col=1,row-1 do + table.insert(nxt, data[col] + data[col+1]) + end + table.insert(nxt, 1) + data = nxt + end +end + +draw_pascal(tonumber(arg[1])) diff --git a/challenge-003/paulo-custodio/perl/ch-1.pl b/challenge-003/paulo-custodio/perl/ch-1.pl index 6301db9a04..e1c2d5b0f8 100644 --- a/challenge-003/paulo-custodio/perl/ch-1.pl +++ b/challenge-003/paulo-custodio/perl/ch-1.pl @@ -3,12 +3,21 @@ # Challenge 003 # # Challenge #1 -# Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more information, please check this wikipedia. +# Create a script to generate 5-smooth numbers, whose prime divisors are less +# or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +# information, please check this wikipedia. use strict; use warnings; use 5.030; -use List::Util 'min'; + +sub min { + my($min, @a) = @_; + for (@a) { + $min = $_ if $min > $_; + } + return $min; +} # return an iterator to generate the sequence # the sequence is a merge of all multiples of 2, 3 and 5 diff --git a/challenge-003/paulo-custodio/perl/ch-2.pl b/challenge-003/paulo-custodio/perl/ch-2.pl index 4612edcd09..bb3afc06e2 100644 --- a/challenge-003/paulo-custodio/perl/ch-2.pl +++ b/challenge-003/paulo-custodio/perl/ch-2.pl @@ -3,7 +3,9 @@ # 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. +# 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. use strict; use warnings; @@ -16,7 +18,7 @@ draw_pascal($rows); sub draw_pascal { my($rows) = @_; my @row = (1); - say " " x $rows, "@row"; + say " " x ($rows-1), "@row"; for my $row (1 .. $rows-1) { # compute next row my @next = (1); @@ -26,9 +28,6 @@ sub draw_pascal { push @next, 1; @row = @next; - say " " x ($rows-$row), "@row"; + say " " x ($rows-$row-1), "@row"; } } - - - diff --git a/challenge-003/paulo-custodio/python/ch-1.py b/challenge-003/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..413228bd57 --- /dev/null +++ b/challenge-003/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +# Challenge 003 +# +# Challenge #1 +# Create a script to generate 5-smooth numbers, whose prime divisors are less +# or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +# information, please check this wikipedia. + +import sys + +# return an iterator to generate the sequence +# the sequence is a merge of all multiples of 2, 3 and 5 +def hamming_gen(): + seq = [[1], [1], [1]] + base = [2, 3, 5] + + while True: + # get the smallest of the multiples + n = min(seq[0][0], seq[1][0], seq[2][0]) + + for i in range(0, 3): + # shift used multiples + if seq[i][0] == n: + seq[i].pop(0) + + # push next multiple + seq[i].append(n*base[i]) + + yield n + + +# main +iter = hamming_gen() +for i in range(0, int(sys.argv[1])): + print(next(iter)) diff --git a/challenge-003/paulo-custodio/python/ch-2.py b/challenge-003/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..d97750c634 --- /dev/null +++ b/challenge-003/paulo-custodio/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# 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. + +import sys + +def draw_pascal(rows): + data = [1] + for row in range(1,rows+1): + # print current row + print(" "*(rows-row) + " ".join([str(x) for x in data])) + + # compute next row + nxt = [1] + for col in range(0, len(data)-1): + nxt.append(data[col] + data[col+1]) + nxt.append(1) + data = nxt + +# main +draw_pascal(int(sys.argv[1])) diff --git a/challenge-003/paulo-custodio/test.pl b/challenge-003/paulo-custodio/test.pl index 9dea6b866c..7884fcb0e4 100644 --- a/challenge-003/paulo-custodio/test.pl +++ b/challenge-003/paulo-custodio/test.pl @@ -5,7 +5,13 @@ use warnings; use Test::More; use 5.030; -is capture("perl perl/ch-1.pl 20"), <<END; +my $LUA = ($^O eq 'msys') ? "lua.exe" : "lua"; + +run("gcc c/ch-1.c -o c/ch-1"); +run("g++ cpp/ch-1.cpp -o cpp/ch-1"); +run("fbc basic/ch-1.bas -o basic/ch-1"); + +for ([20 => <<END]) { 1 2 3 @@ -27,20 +33,43 @@ is capture("perl perl/ch-1.pl 20"), <<END; 32 36 END + my($in, $out) = @$_; + + is capture( "$LUA lua/ch-1.lua $in"), $out; + is capture( "perl perl/ch-1.pl $in"), $out; + is capture( "gforth forth/ch-1.fs $in"), $out; + is capture("python python/ch-1.py $in"), $out; + is capture( "c/ch-1 $in"), $out; + is capture( "cpp/ch-1 $in"), $out; + is capture( "basic/ch-1 $in"), $out; +} + +run("gcc c/ch-2.c -o c/ch-2"); +run("g++ cpp/ch-2.cpp -o cpp/ch-2"); +run("fbc basic/ch-2.bas -o basic/ch-2"); -is capture("perl perl/ch-2.pl 10"), <<END; - 1 - 1 1 - 1 2 1 - 1 3 3 1 - 1 4 6 4 1 - 1 5 10 10 5 1 - 1 6 15 20 15 6 1 - 1 7 21 35 35 21 7 1 - 1 8 28 56 70 56 28 8 1 - 1 9 36 84 126 126 84 36 9 1 +for ([10 => <<END]) { + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 + 1 6 15 20 15 6 1 + 1 7 21 35 35 21 7 1 + 1 8 28 56 70 56 28 8 1 +1 9 36 84 126 126 84 36 9 1 END + my($in, $out) = @$_; + is capture( "$LUA lua/ch-2.lua $in "), $out; + is capture( "perl perl/ch-2.pl $in "), $out; + is capture( "gforth forth/ch-2.fs $in "), $out; + is capture( "python python/ch-2.py $in "), $out; + is capture( "c/ch-2 $in "), $out; + is capture( "cpp/ch-2 $in "), $out; + is capture( "basic/ch-2 $in "), $out; +} done_testing; @@ -50,3 +79,8 @@ sub capture { $out =~ s/[ \t\v\f\r]*\n/\n/g; return $out; } + +sub run { + my($cmd) = @_; + ok 0==system($cmd), $cmd; +} |
