diff options
| author | Roger Bell_West <roger@firedrake.org> | 2020-09-07 10:27:36 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2020-09-07 10:27:36 +0100 |
| commit | 086e57c56348285068f346a5a0053f61a939512b (patch) | |
| tree | 0b9e6d0a7e0e7800df5b7a825c2b97dc0f9f8396 | |
| parent | 088221170be5d4e05e4b8cc3966443e9e15d6393 (diff) | |
| download | perlweeklychallenge-club-086e57c56348285068f346a5a0053f61a939512b.tar.gz perlweeklychallenge-club-086e57c56348285068f346a5a0053f61a939512b.tar.bz2 perlweeklychallenge-club-086e57c56348285068f346a5a0053f61a939512b.zip | |
Solutions for challenge #77.
| -rwxr-xr-x | challenge-077/roger-bell-west/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-077/roger-bell-west/perl/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-077/roger-bell-west/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-077/roger-bell-west/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-077/roger-bell-west/raku/ch-1.p6 | 18 | ||||
| -rwxr-xr-x | challenge-077/roger-bell-west/raku/ch-2.p6 | 43 |
6 files changed, 196 insertions, 0 deletions
diff --git a/challenge-077/roger-bell-west/perl/ch-1.pl b/challenge-077/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..14aeab908c --- /dev/null +++ b/challenge-077/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use List::Util qw(sum); +use Algorithm::Combinatorics qw(combinations); + +use Test::More tests => 2; + +is_deeply(psum(6),[[1,5],[1,2,3]],"example 6"); +is_deeply(psum(9),[[1,8],[1,3,5]],"example 9"); + +sub psum { + my $n=shift; + my @p=(1,1); + while ($p[-1] < $n) { + push @p,$p[-1]+$p[-2]; + } + shift @p; + my @o; + foreach my $l (1..scalar @p) { + foreach my $comb (combinations(\@p,$l)) { + if (sum(@{$comb})==$n) { + push @o,$comb; + } + } + } + return \@o; +} diff --git a/challenge-077/roger-bell-west/perl/ch-2.pl b/challenge-077/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..6a8e18fda0 --- /dev/null +++ b/challenge-077/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,45 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is_deeply(nx([[qw(O O X)],[qw(X O O)],[qw(X O O)]]),1,"example 1"); +is_deeply(nx([[qw(O O X O)],[qw(X O O O)],[qw(X O O X)],[qw(O X O O)]]),2,"example 2"); + +sub nx { + my $n=shift; + my $mr=$#{$n}; + my $mc=$#{$n->[0]}; + my $isol=0; + foreach my $r (0..$mr) { + foreach my $c (0..$mc) { + unless ($n->[$r][$c] eq 'X') { + next; + } + my $isolated=1; + foreach my $dr (-1,0,1) { + if ($r+$dr<0 || $r+$dr>$mr) { + next; + } + foreach my $dc (-1,0,1) { + if ($dc==0 && $dr==0) { + next; + } + if ($c+$dc<0 || $c+$dc>$mc) { + next; + } + if ($n->[$r+$dr][$c+$dc] eq 'X') { + $isolated=0; + last; + } + } + } + if ($isolated) { + $isol++; + } + } + } + return $isol; +} diff --git a/challenge-077/roger-bell-west/python/ch-1.py b/challenge-077/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..14e3f9cefb --- /dev/null +++ b/challenge-077/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest +from itertools import combinations + +def psum(n): + p=[1,1] + while (p[-1]<=n): + p.append(p[-1]+p[-2]) + del p[0] + o=list() + for l in range (1,len(p)): + for c in combinations(p,l): + if (sum(c)==n): + o.append(c) + break + return o + +class TestMajority(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(psum(6),[(1, 5), (1, 2, 3)],'example 6-2') + + def test_ex2(self): + self.assertEqual(psum(9),[(1, 8), (1, 3, 5)],'example 9-2') + +unittest.main() diff --git a/challenge-077/roger-bell-west/python/ch-2.py b/challenge-077/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..d5ad3c7cab --- /dev/null +++ b/challenge-077/roger-bell-west/python/ch-2.py @@ -0,0 +1,33 @@ +#! /usr/bin/python3 + +import unittest + +def nx(n): + mr=len(n)-1 + mc=len(n[0])-1 + isol=0 + for r in (range(0,mr+1)): + for c in (range(0,mc+1)): + if n[r][c] != 'X': + continue + isolated=1 + for dr in (-1,0,1): + if (r+dr >= 0 and r+dr <= mr): + for dc in (-1,0,1): + if ((dc != 0 or dr != 0) and c+dc >= 0 and c+dc <= mc): + if n[r+dr][c+dc] == 'X': + isolated=0 + break + if (isolated): + isol += 1 + return isol + +class TestMajority(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(nx(('O O X'.split(),'X O O'.split(),'X O O'.split())),1,'example 1') + + def test_ex2(self): + self.assertEqual(nx(('O O X O'.split(),'X O O O'.split(),'X O O X'.split(),'O X O O'.split())),2,'example 2') + +unittest.main() diff --git a/challenge-077/roger-bell-west/raku/ch-1.p6 b/challenge-077/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..046d830345 --- /dev/null +++ b/challenge-077/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is-deeply(psum(6),[(1,5),(1,2,3)],"example 6-2"); +is-deeply(psum(9),[(1,8),(1,3,5)],"example 9-2"); + +sub psum($n) { + my @p=(1,1); + while (@p[@p.end] < $n) { + push @p,@p[@p.end]+@p[@p.end-1]; + } + shift @p; + my @o=grep {sum(@($_))==$n}, @p.combinations(1..@p.elems); + return @o; +} diff --git a/challenge-077/roger-bell-west/raku/ch-2.p6 b/challenge-077/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..b79881b635 --- /dev/null +++ b/challenge-077/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,43 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is-deeply(nx(([<O O X>],[<X O O>],[<X O O>])),1,"example 1"); +is-deeply(nx(([<O O X O>],[<X O O O>],[<X O O X>],[<O X O O>])),2,"example 2"); + +sub nx(@n) { + my $mr=@n.end; + my $mc=@n[0].end; + my $isol=0; + for 0..$mr -> $r { + for 0..$mc -> $c { + unless (@n[$r][$c] eq 'X') { + next; + } + my $isolated=1; + for (-1,0,1) -> $dr { + if ($r+$dr < 0 || $r+$dr > $mr) { + next; + } + for (-1,0,1) -> $dc { + if ($dc==0 && $dr==0) { + next; + } + if ($c+$dc < 0 || $c+$dc > $mc) { + next; + } + if (@n[$r+$dr][$c+$dc] eq 'X') { + $isolated=0; + last; + } + } + } + if ($isolated) { + $isol++; + } + } + } + return $isol; +}
\ No newline at end of file |
