aboutsummaryrefslogtreecommitdiff
path: root/challenge-245
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2023-12-03 15:54:30 -0500
committerrir <rirans@comcast.net>2023-12-03 15:54:30 -0500
commit44d6a8eff3d502df3dea6ea862ddaef74dca1858 (patch)
tree0350d75fb79ffde367b5a31ecf98a712d2c3b873 /challenge-245
parenta91d49494a545d745c5c622afa3a9646bf1ac774 (diff)
downloadperlweeklychallenge-club-44d6a8eff3d502df3dea6ea862ddaef74dca1858.tar.gz
perlweeklychallenge-club-44d6a8eff3d502df3dea6ea862ddaef74dca1858.tar.bz2
perlweeklychallenge-club-44d6a8eff3d502df3dea6ea862ddaef74dca1858.zip
245
Diffstat (limited to 'challenge-245')
-rw-r--r--challenge-245/0rir/raku/ch-1.raku48
-rw-r--r--challenge-245/0rir/raku/ch-2.raku114
2 files changed, 162 insertions, 0 deletions
diff --git a/challenge-245/0rir/raku/ch-1.raku b/challenge-245/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..0f2fc7db47
--- /dev/null
+++ b/challenge-245/0rir/raku/ch-1.raku
@@ -0,0 +1,48 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+245-1: Sort Language Submitted by: Mohammad S Anwar
+
+You are given two array of languages and its popularity.
+Write a script to sort the language based on popularity.
+
+Example 1
+Input: @lang = ('perl', 'c', 'python')
+ @popularity = (2, 1, 3)
+Output: ('c', 'perl', 'python')
+Example 2
+Input: @lang = ('c++', 'haskell', 'java')
+ @popularity = (1, 3, 2)
+Output: ('c++', 'java', 'haskell')
+=end comment
+
+my @Test =
+ ('perl', 'c', 'python'), (2, 1, 3), ('c', 'perl', 'python'),
+ ('c++', 'haskell', 'java'), (1, 3, 2), ('c++', 'java', 'haskell'),
+ <Perl Lisp R C SQL Ruby C++ Rust Lua >,
+ (29, 35, 19, 2, 9, 18,3, 20, 31),
+ <C C++ SQL Ruby R Rust Perl Lua Lisp>
+;
+
+plan @Test ÷ 3;
+
+sub sort-n-format-names-by-rank( @name, @rank) {
+ '(' ~ ( @name Z @rank).sort( *[1] ).map( *[0]).join(", ") ~ ")"
+}
+
+for @Test -> @name, @rank, @exp {
+ is sort-n-format-names-by-rank( @name, @rank), "(" ~ @exp.join( ", ") ~ ")";
+}
+done-testing;
+
+my @lang = <Perl Lisp R C SQL Ruby C++ Rust Lua >;
+my @popularity = 29, 35, 19, 2, 9, 18, 3, 20, 31;
+
+say "\nInput: @lang = @lang.raku()\n"
+ ~ " @popularity = @popularity[].raku()\nOutput: "
+ ~ sort-n-format-names-by-rank( @lang, @popularity);
+exit;
+
diff --git a/challenge-245/0rir/raku/ch-2.raku b/challenge-245/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..c23e723997
--- /dev/null
+++ b/challenge-245/0rir/raku/ch-2.raku
@@ -0,0 +1,114 @@
+#!/usr/bin/env raku
+use v6;
+use Test;
+
+=begin comment
+245-2: Largest of Three Submitted by: Mohammad S Anwar
+
+You are given an array of integers >= 0.
+Write a script to return the largest number formed by concatenating some of
+the given integers in any order which is also multiple of 3. Return -1 if
+none found.
+
+Example 1
+Input: @ints = (8, 1, 9)
+Output: 981
+
+981 % 3 == 0
+Example 2
+Input: @ints = (8, 6, 7, 1, 0)
+Output: 8760
+
+Example 3
+Input: @ints = (1)
+Output: -1
+=end comment
+
+my @Test =
+ (1,), -1,
+ (2,5,), -1,
+ (), -1,
+ (4,235), -1,
+ (63, 9, 39, 3, 30000,1,), 96339330000,
+ (63, 9,1,), 963,
+ (93, 6), 936,
+ (8, 1, 9), 981,
+ (3,), 3,
+ (1, 6, 7, 8, 0), 8760,
+ (3,9), 93,
+ (3,3), 33,
+ (8, 6, 7, 1, 0), 8760,
+ (1,1,1,1,1,1), 111111,
+ (1,12,9,14), 914121,
+ (1,2,3,4,5,10,11,12,13,14,15,16,17,18,19,20,21),
+ 54322120191817161514131211110,
+ (1,1,2,3,4,5,10,11,12,13,14,15,16,17,18,19,20,21),
+ 54322120191817161514131211110,
+;
+plan @Test ÷ 2;
+
+# Compare common positive numbers, i.e. 123, so that Order::More indicates
+# which should come first to create the greater number when concatenated.
+sub cat-cmp( Any:D $a, Any:D $b ) { # naming? salvage from challenge 217-2
+ my @a = $a.comb.Array;
+ my @b = $b.comb.Array;
+ my $span = (@a.elems, @b.elems).min;
+ for ^$span -> $i {
+ my $o = @a[$i] <=> @b[$i];
+ if ? $o { return $o } # Order::Same.Bool is False
+ }
+
+ given +@a <=> +@b {
+ when Same { return Same }
+ when More {
+ return cat-cmp @a[$span..*].join, @b.join;
+ }
+ when Less {
+ return cat-cmp @a.join, @b[$span..*].join;
+ }
+ }
+}
+
+# concat a list of common positive numbers to make the largest number.
+multi max-number( Int $a -->Int ) { $a };
+multi max-number( @a -->Int) {
+ (@a.sort: { cat-cmp $^b.Int, $^a.Int } ).join.Int;
+}
+
+multi func( @a = () ) { -1 }
+multi func( @a) {
+ my @trey = @a.combinations(1..*).grep( *.sum %% 3);
+ my $long = (@trey.map( *.join.chars)).max;
+ -∞ == (my $ret = max @trey.grep(
+ *.join.chars == $long).map( *.&max-number))
+ ?? -1
+ !! $ret;
+}
+
+multi func1( @a = () ) { -1 }
+multi func1( @a ) {
+ my @trey = @a.combinations(1..*).grep( *.sum %% 3);
+ my %by-size = @trey.classify: { .join.chars };
+ my @result;
+
+ for %by-size.keys.sort( { $^b <=> $^a } ) -> $k {
+ for %by-size{$k}.values -> @v {
+ @result.push: @v.&max-number;
+ }
+ next without @result;
+ return @result.max;
+ }
+ return -1;
+}
+
+for @Test -> $in, $exp {
+ #is func($in), $exp, "$exp\t<= @$in[].raku()";
+ is func1($in), $exp, "$exp\t<= @$in[].raku()";
+}
+
+my @int = (63, 9, 39, 3, 30000,1,);
+say "\nInput: @int = @int[]\n Output: &func(@int)";
+
+done-testing;
+exit;
+