diff options
| author | bagheera-sands <git@sandsscouts.org.uk> | 2019-04-08 06:32:57 +0100 |
|---|---|---|
| committer | bagheera-sands <git@sandsscouts.org.uk> | 2019-04-08 06:32:57 +0100 |
| commit | ffe7616b0f9f2cfa01510ce2a5db26eab5c3daeb (patch) | |
| tree | ba8c0ad5c8d0b0e59ab71a3997c6db9313bac84d /challenge-003/james-smith | |
| parent | 9c94bfad3b97186951379449451078181c3f2d3a (diff) | |
| download | perlweeklychallenge-club-ffe7616b0f9f2cfa01510ce2a5db26eab5c3daeb.tar.gz perlweeklychallenge-club-ffe7616b0f9f2cfa01510ce2a5db26eab5c3daeb.tar.bz2 perlweeklychallenge-club-ffe7616b0f9f2cfa01510ce2a5db26eab5c3daeb.zip | |
challenge 3
Diffstat (limited to 'challenge-003/james-smith')
| -rw-r--r-- | challenge-003/james-smith/README | 1 | ||||
| -rw-r--r-- | challenge-003/james-smith/README.md | 26 | ||||
| -rw-r--r-- | challenge-003/james-smith/perl5/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-003/james-smith/perl5/ch-2.pl | 12 | ||||
| -rw-r--r-- | challenge-003/james-smith/perl6/ch-1.p6 | 22 | ||||
| -rw-r--r-- | challenge-003/james-smith/perl6/ch-2.p6 | 14 |
6 files changed, 95 insertions, 1 deletions
diff --git a/challenge-003/james-smith/README b/challenge-003/james-smith/README deleted file mode 100644 index 573d9eb02a..0000000000 --- a/challenge-003/james-smith/README +++ /dev/null @@ -1 +0,0 @@ -Solution by James Smith diff --git a/challenge-003/james-smith/README.md b/challenge-003/james-smith/README.md new file mode 100644 index 0000000000..d620c52ba2 --- /dev/null +++ b/challenge-003/james-smith/README.md @@ -0,0 +1,26 @@ +Solution by James Smith + +## Problem 2 + +Wierdly I think problem 2 is the easier this week. I wrote this twice, +the second solution I'm using minimizes memory usage by writing an +iterator which extends the array rather than copying a new one... + +The important part is to remember to loop backwards...! + +## Problem 1 - Perl 6 + +I'm new to Perl 6 and so looking for what features of the language +give me an advantage of Perl 5 which I've been programming for over +20 years now... + +So I looked at using a generator with lazy evaluation using +`lazy gather` and `take` to generate the values... Looks interesting +as a "programming construct" but again like last weeks use of +currying - does not particularly look efficient in this case. + +I did though like the fact that perl6 does "big int"s much like +python which is good in these challenges as you don't flip to +'e' notation as quickly. + + diff --git a/challenge-003/james-smith/perl5/ch-1.pl b/challenge-003/james-smith/perl5/ch-1.pl new file mode 100644 index 0000000000..3651962b94 --- /dev/null +++ b/challenge-003/james-smith/perl5/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use feature ':5.10'; + +my @hammings = (1); + +sub next_hamming { + my $lowest; + local $_; + foreach my $n (2,3,5) { + foreach (@hammings) { + next if $_*$n <= $hammings[-1]; + $lowest = $_*$n unless $lowest && $lowest < $_*$n; + last; + } + } + push @hammings, $lowest; + return $lowest; +} + +say 1; +say next_hamming() foreach 1..shift; diff --git a/challenge-003/james-smith/perl5/ch-2.pl b/challenge-003/james-smith/perl5/ch-2.pl new file mode 100644 index 0000000000..421e799c33 --- /dev/null +++ b/challenge-003/james-smith/perl5/ch-2.pl @@ -0,0 +1,12 @@ +use strict; +use feature ':5.10'; + +my @line = (); + +sub next_line { + $line[$_] = $line[$_-1]+$line[$_] foreach reverse 1..@line; + $line[0]=1; + return \@line; +} + +say "@{next_line()}" foreach 1..shift; diff --git a/challenge-003/james-smith/perl6/ch-1.p6 b/challenge-003/james-smith/perl6/ch-1.p6 new file mode 100644 index 0000000000..66711b7f81 --- /dev/null +++ b/challenge-003/james-smith/perl6/ch-1.p6 @@ -0,0 +1,22 @@ +use strict; + +my @hammings = lazy gather { + take 1; + my $last = 0; + while 1 { + my $lowest; + for 2,3,5 -> $n { + for (@hammings) { + next if $_*$n <= $last; + $lowest = $_*$n unless $lowest && $lowest < $_*$n; + last; + } + } + take $lowest; + $last = $lowest; + } +} + +sub MAIN($n) { + say @hammings[$_-1] for 1..$n; +} diff --git a/challenge-003/james-smith/perl6/ch-2.p6 b/challenge-003/james-smith/perl6/ch-2.p6 new file mode 100644 index 0000000000..dade655d5b --- /dev/null +++ b/challenge-003/james-smith/perl6/ch-2.p6 @@ -0,0 +1,14 @@ +use strict; + +my @line = (); + +sub next_line { + @line[$_] = @line[$_-1]+(@line[$_]||0) for reverse 1..@line; + @line[0]=1; + return @line; +} + +sub MAIN($n) { + say join ' ',next_line() for 1..$n; +} + |
