aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-08-27 18:09:57 -0400
committerrir <rirans@comcast.net>2024-08-28 09:44:18 -0400
commitfb03f313063a2b76874fbff77f26dc967e9c6bfc (patch)
treecb1458bef51e7465dce10e026c3de3558ebd9946
parent4c3ef4c4a6fa0e07ad7c8b5bf158a06bce0bb614 (diff)
downloadperlweeklychallenge-club-fb03f313063a2b76874fbff77f26dc967e9c6bfc.tar.gz
perlweeklychallenge-club-fb03f313063a2b76874fbff77f26dc967e9c6bfc.tar.bz2
perlweeklychallenge-club-fb03f313063a2b76874fbff77f26dc967e9c6bfc.zip
284
-rw-r--r--challenge-284/0rir/raku/ch-1.raku51
-rw-r--r--challenge-284/0rir/raku/ch-2.raku60
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-284/0rir/raku/ch-1.raku b/challenge-284/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..69c209b388
--- /dev/null
+++ b/challenge-284/0rir/raku/ch-1.raku
@@ -0,0 +1,51 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰βŠ† ≑ β‰’ «␀ Β» ∴
+use v6.d;
+use Test;
+
+=begin comment
+284-1 Lucky Integer Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints, find the lucky integer if found
+otherwise return -1. If there are more than one then return the largest.
+A lucky integer is an integer that has a frequency in the array equal
+to its value.
+
+Example 1
+Input: @ints = (2, 2, 3, 4)
+Output: 2
+Example 2
+Input: @ints = (1, 2, 2, 3, 3, 3)
+Output: 3
+Example 3
+Input: @ints = (1, 1, 1, 3)
+Output: -1
+
+=end comment
+
+my @Test =
+ (), Int,
+ (1), 1,
+ (3,3,3), 3,
+ (2, 2, 3, 4), 2,
+ (1, 2, 2, 3, 3, 3), 3,
+ (1, 1, 1, 3), Int,
+ (-1,-2,-2), Int,
+ ( 1, ∞, 2, ∞ ), 1,
+;
+plan @Test Γ· 2;
+
+sub task( $list -->Int) {
+ my %h = $list.BagHash;
+ quietly %h.values.grep( {%h{$_} == $_}).sort.reverse[0] // Int;
+}
+
+for @Test -> $in, $exp {
+ is task($in), $exp, "$exp.raku() <- $in";
+}
+
+done-testing;
+
+my @int = (-1,2,2,2,2,1,3,3,2,3,);
+say "\nInput: @int = @int[]\nOutput: { task( @int) // 1}";
+
diff --git a/challenge-284/0rir/raku/ch-2.raku b/challenge-284/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..5b4be54966
--- /dev/null
+++ b/challenge-284/0rir/raku/ch-2.raku
@@ -0,0 +1,60 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰βŠ† ≑ β‰’ «␀ Β» ∴
+use v6.d;
+INIT $*RAT-OVERFLOW = FatRat;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+284-2: Relative Sort Submitted by: Mohammad Sajid Anwar
+
+You are given two list of integers, @list1 and @list2. The elements in the @list2 are distinct and also in the @list1.
+
+Write a script to sort the elements in the @list1 such that the relative order of items in @list1 is same as in the @list2. Elements that is missing in @list2 should be placed at the end of @list1 in ascending order.
+
+Example 1
+Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5)
+ @list2 = (2, 1, 4, 3, 5, 6)
+Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9)
+Example 2
+Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3)
+ @list2 = (1, 3, 2)
+Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6)
+Example 3
+Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1)
+ @list2 = (1, 0, 3, 2)
+Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5)
+=end comment
+
+my @Test = # sort-me by-order / expect
+ (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5), (2, 1, 4, 3, 5, 6),
+ (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9),
+ (3, 3, 4, 6, 2, 4, 2, 1, 3), (1, 3, 2),
+ (1, 3, 3, 3, 2, 2, 4, 4, 6),
+ (3, 0, 5, 0, 2, 1, 4, 1, 1), (1, 0, 3, 2),
+ (1, 1, 1, 0, 0, 3, 2, 4, 5),
+;
+plan @Test Γ· 3;
+
+sub task( List $to-sort, List $by-order --> List) {
+ my @ret;
+ my %h = $to-sort.BagHash;
+ for $by-orderΒ».Int -> \n {
+ if %h{n} :exists { @ret.append: n xx ( %h{ n} :delete); }
+ }
+ for %h.keys.sort { @ret.append: $_ xx %h{$_} ; }
+ @ret;
+}
+
+for @Test -> $to-sort, $by-order, $exp {
+ is task($to-sort, $by-order), $exp, "$exp <- $to-sort ∘∘ $by-order";
+}
+
+done-testing;
+my @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5);
+my @list2 = (2, 1, 4, 3, 5, 6);
+
+say "\n Input: @list1 = @list1[]\n @list2 = @list2[]\n"
+ ~ " Output: { task @list1, @list2 }";
+
+