diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-05 23:56:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-05 23:56:22 +0000 |
| commit | 40d6776af2df4f9bfc4b4b46395c325c1e394c0a (patch) | |
| tree | 8345535cb402aa590396918b4fbc9b87697056e8 /challenge-241 | |
| parent | a9e561833540af26b3d131ae77fc36009629ad74 (diff) | |
| parent | e992cb7eb43ed4f3f802ec1fba85da6e44fcf72e (diff) | |
| download | perlweeklychallenge-club-40d6776af2df4f9bfc4b4b46395c325c1e394c0a.tar.gz perlweeklychallenge-club-40d6776af2df4f9bfc4b4b46395c325c1e394c0a.tar.bz2 perlweeklychallenge-club-40d6776af2df4f9bfc4b4b46395c325c1e394c0a.zip | |
Merge pull request #9002 from 0rir/241
241
Diffstat (limited to 'challenge-241')
| -rwxr-xr-x | challenge-241/0rir/raku/ch-2.raku | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/challenge-241/0rir/raku/ch-2.raku b/challenge-241/0rir/raku/ch-2.raku new file mode 100755 index 0000000000..f81c2e062f --- /dev/null +++ b/challenge-241/0rir/raku/ch-2.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +241-2: Prime Order Submitted by: Mohammad S Anwar +Given an array of unique positive integers greater than 2, sort them in +ascending order of the count of their prime factors, tie-breaking by ascending +value. + +Example 1 +Input: @int = (11, 8, 27, 4) +Output: (11, 4, 8, 27)) + +Prime factors of 11 => 11 +Prime factors of 4 => 2, 2 +Prime factors of 8 => 2, 2, 2 +Prime factors of 27 => 3, 3, 3 +=end comment + +my @Test = + [11,8,27,4], [11,4,8,27], + [8,27], [8,27], + [27,8], [8,27], +; + +plan @Test ÷ 2; + +subset Positive of Int where * ≥ 1; + +constant @prime = lazy 2,3, (* + 2 if *.is-prime) … ∞; + +sub prime-factors( $n is copy -->Array) is export { + my @ret; + my $ix = 0; + while $n > 1 { + my $candi = @prime[$ix]; + $ix++; + next unless ( $n %% $candi ); + $ix = 0; + $n ÷= $candi; + @ret.push: $candi; + } + return @ret; +} + +sub sort-by-prime-factor-ct-then-val( @a) { + @a.sort: { .&prime-factors.elems, $_ }; +} + +for @Test -> @in, @exp { + is sort-by-prime-factor-ct-then-val(@in), @exp, "@in.raku() -> @exp.raku()"; +} + +done-testing; + +my @a = 11, 2³, 3³, 2², 3, 97, 3²×11, 2²×5², 2³x3²×7×17×37, 2⁹; +say "Input: @int = @a.raku()\nOutput: &sort-by-prime-factor-ct-then-val( @a)"; + +exit; + |
