diff options
| author | rir <rirans@comcast.net> | 2022-11-20 16:49:07 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2022-11-20 16:49:07 -0500 |
| commit | a847559d35578fc660e52b4fdd68d729c7b7be01 (patch) | |
| tree | 83664e67252018fe86d9e15ce2741b6d1040a08d | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-a847559d35578fc660e52b4fdd68d729c7b7be01.tar.gz perlweeklychallenge-club-a847559d35578fc660e52b4fdd68d729c7b7be01.tar.bz2 perlweeklychallenge-club-a847559d35578fc660e52b4fdd68d729c7b7be01.zip | |
191
| -rw-r--r-- | challenge-191/0rir/raku/ch-1.raku | 55 | ||||
| -rw-r--r-- | challenge-191/0rir/raku/ch-2.raku | 120 |
2 files changed, 175 insertions, 0 deletions
diff --git a/challenge-191/0rir/raku/ch-1.raku b/challenge-191/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..c2a648d707 --- /dev/null +++ b/challenge-191/0rir/raku/ch-1.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +191-1: Twice Largest Submitted by: Mohammad S Anwar + +Given a list of integers, @list, find if its largest element is at least + twice as large as every other items. +=end comment + +=begin queries + () ? Here I rule "Empty" is allowed. For example, need a list of + integers between the integers x and y. Could be x + 1 = y. + Simplest, for all, seems to return the Bool type object. + Easy to invent here, to fix here, and to accommodate there. + I just don't know 'best practice', for handling anomalies. +=end queries + +multi max-gte-allx2 ( @a where * === List ) { die 'What am I doing?' } +multi max-gte-allx2 ( @a where * ~~ Empty --> Bool ) { Bool } +multi max-gte-allx2 ( @a where *.elems == 1 --> Bool ) { True } +multi max-gte-allx2 ( @a --> Bool ) { + my $max = @a.max/2; + 1 == @a.grep( * > $max); +} + +multi MAIN ( $t where * = < T t>.any ) { + my @Test = { in => (1,2,3,4), exp => False, }, + { in => (1,2,0,5), exp => True, }, + { in => (2,6,3,1), exp => True, }, + { in => (4,5,2,3), exp => False, }, + { in => (3,), exp => True, }, + ; + plan 2 + @Test; + + is max-gte-allx2( ()), Bool, "Empty --> " ~ 'Bool type object'; + + for @Test -> %t { + is max-gte-allx2( %t<in>), %t<exp> > 0, "%t<in>.raku() --> %t<exp>"; + } + dies-ok { max-gte-allx2( List) }, "List type object dies"; + + done-testing; +} + +multi MAIN( $list = (2,6,3,1) ) { + { in => (2,6,3,1), exp => -1, }, + # XXX + say "Input: @list = $list.raku()\n" + ~"Output: ", max-gte-allx2( $list) ?? 1 !! -1; +} + diff --git a/challenge-191/0rir/raku/ch-2.raku b/challenge-191/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..111c49cdab --- /dev/null +++ b/challenge-191/0rir/raku/ch-2.raku @@ -0,0 +1,120 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment + +191-2: Cute List Submitted by: Mohammad S Anwar +Given an integer, 0 < $n <= 15, find the number of orderings of numbers +that form a cute list. + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + +1) $list[$i] is evenly divisible by $i +or +2) $i is evenly divisible by $list[$i] +Example +Input: $n = 2 +Ouput: 2 + +Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. +Therefore we can have two list i.e. (1,2) and (2,1). + +@list = (1,2) is cute since $list[1] = 1 is divisible by 1 + and $list[2] = 2 is divisible by 2. + +=end comment + +# naive implementation -- I don't have much math +sub swap( @a, $l, $r --> Nil ) { + (@a[$l], @a[$r]) = @a[$r], @a[$l]; + return; +} + +multi is-cute( @a --> Bool) { + + for (0 ..^@a) -> $i { # check hi-prime first + return False unless ( @a[$i] %% ($i+1) or ($i+1) %% @a[$i] ); + } + return True; +} + +=begin comment +multi make-cute( @a ) { + my $in = @a.elems; + my $middle = $in div 2; # end of swappables + my $ret = 1; # count of valid permutations + + my @factor = 2 .. $middle; # 1 is special case + my @swap = X=> @factor, @factor; + +# purge conflicting swaps + + for 1..^@swap -> $window-size { + for 2 .. (@swap.elems - $window-size ) + } + + # get index zero swap count + #my $izsc = +@half-rtn + +@half-rtn × ($in -1); + #say "\$izsc: ", $izsc; + + return ; +} + +=end comment + +multi gen-cute( $n --> Int) { + return $n if $n ≤ 3; + + # state $prev = 3; # if called as a sequence maker, could expand + # previous permutations. + + my $rtn = 0; + my @pmute = ( 1 .. $n ).permutations; + for @pmute -> @a { + ++$rtn if is-cute @a ; + } + return $rtn; +} + +say gen-cute( 8); + + +=finish + +multi MAIN ( $t where * = < T t>.any ) { + my @Test = + 0 => Any, 1 => 1, 2 => 2, 3 => 3, 4 => 8, + 5 => 10, 6 => 36, 7 => 41, 8 => 132, 9 => 250, + 10 => 700, 11 => 750, + # 12 => 4010, 13 => 4237, 14 => 10680, 15 => 24679 + ; + my @T-is-cute-true = + (1,2,3,4), (1,4,3,2), (2,1,3,4), (2,4,3,1), + (3,2,1,4), (3,4,1,2), (4,1,3,2), (4,2,3,1), + (1,8,3,4,10,6,7,2,9,5,11,12), + ; + my @T-is-cute-false = + (1,2,4,3), (1,3,2,4), (1,3,4,2), (1,4,2,3), + (2,1,4,3), (2,3,1,4), (2,3,4,1), (2,4,1,3), + (3,1,2,4), (3,1,4,2), (3,2,4,1), (3,4,2,1), + (4,1,2,3), (4,2,1,3), (4,3,1,2), (4,3,2,1), + (1,3,2,4,5,6,7,8,9,11,10,12), + (1,3,2,4,5,6,7,8,9,10,11,12), + ; + plan @Test.elems + @T-is-cute-true + @T-is-cute-false; + + for @T-is-cute-true -> @t { + ok is-cute( @t), " is cute @t[]"; + } + for @T-is-cute-false -> @t { + nok is-cute( @t), "not cute @t[]"; + } + + for @Test -> $p { + is @cute-count[ $p.key], $p.value, "$p.key() --> "~ $p.value.raku; + } + done-testing; +} + |
