diff options
| author | Scimon <simon.proctor@gmail.com> | 2023-06-05 11:34:38 +0100 |
|---|---|---|
| committer | Scimon <simon.proctor@gmail.com> | 2023-06-05 11:34:38 +0100 |
| commit | 6b3f6682a56f9f9f92bcb21d371dac59a2266778 (patch) | |
| tree | cec4e05bce72d3c92bf025cbfb88e9590001dbe9 | |
| parent | 1e3daa9a29a5e6d39b95e526a360ca89c344aa56 (diff) | |
| download | perlweeklychallenge-club-6b3f6682a56f9f9f92bcb21d371dac59a2266778.tar.gz perlweeklychallenge-club-6b3f6682a56f9f9f92bcb21d371dac59a2266778.tar.bz2 perlweeklychallenge-club-6b3f6682a56f9f9f92bcb21d371dac59a2266778.zip | |
PArt 2 (and some part 1 fixes)
| -rw-r--r-- | challenge-220/simon-proctor/raku/ch-1.raku | 3 | ||||
| -rw-r--r-- | challenge-220/simon-proctor/raku/ch-2.raku | 30 |
2 files changed, 32 insertions, 1 deletions
diff --git a/challenge-220/simon-proctor/raku/ch-1.raku b/challenge-220/simon-proctor/raku/ch-1.raku index c6043d4548..5f67297575 100644 --- a/challenge-220/simon-proctor/raku/ch-1.raku +++ b/challenge-220/simon-proctor/raku/ch-1.raku @@ -5,10 +5,11 @@ multi sub MAIN( *@words ) { found-letters(@words).join(' ').say; } -multi sub MAIN('TEST') { +multi sub MAIN('TEST') is hidden-from-USAGE { use Test; is found-letters( 'Perl', 'Rust', 'Raku' ), ('r'); is found-letters( 'love', 'live', 'leave' ), ('e','l','v'); + done-testing; } sub found-letters ( *@words ) { diff --git a/challenge-220/simon-proctor/raku/ch-2.raku b/challenge-220/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..dec82ba7dc --- /dev/null +++ b/challenge-220/simon-proctor/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env raku + +#|(Given a list of number print all permutations where each pair in the list +where the sum of the pair is a perfect square +) +multi sub MAIN( *@values where @values.all ~~ Int() ) { + all-perfect(@values).map(*.join(',')).join("\n").say; +} + +multi sub MAIN('TEST') is hidden-from-USAGE { + use Test; + ok perfect( 25 ); + ok ! perfect( 24 ); + ok perfect-list((1,8,17)); + is all-perfect((1,17,8)), ( (1,8,17), (17,8,1) ); + is all-perfect((2,2,2)), ( (2,2,2) ); + done-testing; +} + +sub all-perfect( @list ) { + @list.permutations.unique(:as(-> @a {@a.join(',')})).grep( -> @a { perfect-list(@a) } ); +} + +sub perfect-list( @list ) { + [&] @list.rotor(2 => -1).map(-> ($a,$b) {perfect( $a+$b )}); +} + +sub perfect( Int $v ) { + $v.sqrt.Int() ~~ $v.sqrt; +} |
