aboutsummaryrefslogtreecommitdiff
path: root/challenge-289
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-10-12 13:16:11 -0400
committerrir <rirans@comcast.net>2024-10-12 13:18:02 -0400
commit570a0f83364811f906d76f0ca623ff59342f4bd4 (patch)
treee63467d711484bdd5eef6941fc777279c0e2e945 /challenge-289
parente2c61589b0995bd2a23e5ff62ef99f16d71392cc (diff)
downloadperlweeklychallenge-club-570a0f83364811f906d76f0ca623ff59342f4bd4.tar.gz
perlweeklychallenge-club-570a0f83364811f906d76f0ca623ff59342f4bd4.tar.bz2
perlweeklychallenge-club-570a0f83364811f906d76f0ca623ff59342f4bd4.zip
289
Diffstat (limited to 'challenge-289')
-rw-r--r--challenge-289/0rir/raku/ch-1.raku71
-rw-r--r--challenge-289/0rir/raku/ch-2.raku82
2 files changed, 153 insertions, 0 deletions
diff --git a/challenge-289/0rir/raku/ch-1.raku b/challenge-289/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..1a0ab97736
--- /dev/null
+++ b/challenge-289/0rir/raku/ch-1.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+INIT die 'Rakudo version is too old; need max with named args.'
+ unless $*RAKU.compiler.version cmp v2023.07 == More;
+use Test;
+
+=begin comment
+289-1: Third Maximum Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints.
+Write a script to find the third distinct maximum in the given array.
+If third maximum doesn’t exist then return the maximum number.
+
+Example 1
+Input: @ints = (5, 6, 4, 1)
+Output: 4
+
+The first distinct maximum is 6.
+The second distinct maximum is 5.
+The third distinct maximum is 4.
+Example 2
+Input: @ints = (4, 5)
+Output: 5
+
+In the given array, the third maximum doesn't exist therefore returns the
+maximum.
+
+Example 3
+Input: @ints = (1, 2, 2, 3)
+Output: 1
+
+The first distinct maximum is 3.
+The second distinct maximum is 2.
+The third distinct maximum is 1.
+
+=end comment
+
+my @Test =
+ (), Int,
+ (1,), 1,
+ (1, 1), 1,
+ (1, 1, 1), 1,
+ (1, 1, 1, 1), 1,
+ (1, 2, 1, -1), -1,
+ (1, 2, 2, 1), 2,
+ (1, 2, 2, 3), 1,
+ (4, 5), 5,
+ (5, 6, 4, 1), 4,
+ (1, 2, 2, 3, 3), 1,
+;
+plan @Test ÷ 2;
+
+multi task( () ) {Int}
+multi task( $in) {
+ my Int @a = |$in;
+ my $max = @a.max;
+ for ^2 {
+ @a[ @a.max( :k) ]:delete;
+ return $max if @a ~~ [];
+ }
+ @a.max;
+}
+
+for @Test -> @in, $exp {
+ is task(@in), $exp, "$exp.Int.raku() <- @in.raku()";
+}
+
+done-testing;
+
+my @int = 1,2,2,2,2;
+say "\nInput: @ints = { @int.raku }\n Output: { task @int }";
diff --git a/challenge-289/0rir/raku/ch-2.raku b/challenge-289/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..b6de56b278
--- /dev/null
+++ b/challenge-289/0rir/raku/ch-2.raku
@@ -0,0 +1,82 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+289-2: Jumbled Letters Submitted by: Ryan Thompson
+
+Your task is to write a program that takes English text as its input and outputs a jumbled version as follows:
+
+The first and last letter of every word must stay the same
+The remaining letters in the word are scrambled in a random order (if that happens to be the original order, that is OK).
+Whitespace, punctuation, and capitalization must stay the same
+The order of words does not change, only the letters inside the word
+So, for example, “Perl” could become “Prel”, or stay as “Perl,” but it could not become “Pelr” or “lreP”.
+=end comment
+
+my @T-upper =
+ "'", "'",
+ '_&&\$', '_&&\$',
+ 'tc', 'tc',
+ 'ten', 'tEn',
+ 'hell', 'hELl',
+ ' a', ' a',
+ 'a ', 'a ',
+ ' a ', ' a ',
+ 'a b', 'a b',
+ ' a b', ' a b',
+ ' a b ', ' a b ',
+ 'a b c d', 'a b c d',
+ ' a b c d', ' a b c d',
+ 'a b c d ', 'a b c d ',
+ ' a b c d ', ' a b c d ',
+ '%a"b-c,d<e.', '%a"b-c,d<e.',
+
+ 'isn\'t it a grandiosity', 'iSn\'t it a gRANDIOSITy',
+ 'isn\'t it a grand-iosity', 'iSn\'t it a gRANd-iOSITy',
+ 'isn\'t it a grand\iose city', 'iSn\'t it a gRANd\iOSe cITy',
+;
+
+my @T-alf =
+ "Hello my name is Jonas Saul Jones.",
+ rx/H [ell|lel|lle] "o my n" [am | ma] "e is J" [ona|oan|nao|noa|ano|aon]
+ "s S" [au|ua] "l J" [one|oen|neo|noe|eno|eon] "s." /,
+;
+
+plan @T-upper ÷ 2 + @T-alf ÷ 2;
+
+constant $min = 3; # minimum length of 'word' to alter.
+
+sub ctr-jumble( $str is copy -->Str) {
+ my @a = $str.comb;
+ my ($pref, $suff) = @a.shift, @a.pop;
+ $pref ~ @a.pick( +@a).join ~ $suff;
+}
+sub ctr-uc( $str -->Str) {
+ my @a = $str.comb;
+ my ($pref, $suff) = @a.shift, @a.pop;
+ @a = @a>>.uc;
+ $pref ~ @a.join ~ $suff;
+}
+
+sub task( $str, &alter = &ctr-jumble ) {
+ my @a = $str.split: :v, / <:L>**{$min..*}/; # Undoc interpolation syn???
+ my $ret; # Range is codish here?
+ for @a -> $s {
+ if $s ~~ Str { $ret ~= $s }
+ elsif $s ~~ Match {
+ $ret ~= &alter( $s.Str);
+ } else { die "It's the programmer's fault." }
+ }
+ return $ret;
+}
+
+for @T-upper -> $in, $exp {
+ is task($in, &ctr-uc ), $exp, "uc $in";
+}
+for @T-alf -> $in, $exp {
+ my $got = task($in, &ctr-jumble );
+ ok $got ~~ $exp, "jumbled $got";
+}
+done-testing;