aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-03-14 01:01:16 -0400
committerrir <rirans@comcast.net>2024-03-14 01:01:16 -0400
commit60f1003122fbada697317d943c238593f86db579 (patch)
tree16d08c01e8053105bbff1b84227ab815af7adf10
parentdfe459e065d51e41d8cc7feb00a5f53b5080d79e (diff)
downloadperlweeklychallenge-club-60f1003122fbada697317d943c238593f86db579.tar.gz
perlweeklychallenge-club-60f1003122fbada697317d943c238593f86db579.tar.bz2
perlweeklychallenge-club-60f1003122fbada697317d943c238593f86db579.zip
1, 2, 3, 4, 260
-rw-r--r--challenge-001/0rir/raku/ch-1.raku26
-rw-r--r--challenge-001/0rir/raku/ch-2.raku39
-rw-r--r--challenge-002/0rir/raku/ch-1.raku36
-rw-r--r--challenge-002/0rir/raku/ch-2.raku22
-rw-r--r--challenge-003/0rir/raku/ch-2.raku30
-rw-r--r--challenge-004/0rir/raku/Pi1000.rakumod5
-rw-r--r--challenge-004/0rir/raku/ch-1.raku7
-rw-r--r--challenge-260/0rir/raku/ch-1.raku56
-rw-r--r--challenge-260/0rir/raku/ch-2.raku71
9 files changed, 292 insertions, 0 deletions
diff --git a/challenge-001/0rir/raku/ch-1.raku b/challenge-001/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..733acaac6b
--- /dev/null
+++ b/challenge-001/0rir/raku/ch-1.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+
+=begin comment
+001-1
+Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ is found in the string.
+=end comment
+
+my @Test =
+ 'Perl Weekly Challenge', 'PErl WEEkly ChallEngE', 5,
+;
+
+plan @Test ÷ 2;
+
+sub func( $a -->Array) {
+ my @ret;
+ $_ = $a;
+ @ret.push: .subst: 'e', 'E', :g ;
+ @ret.push: + .comb.grep( 'e');
+ @ret;
+}
+
+my @result = func @Test[0];
+say "Input: @Test[0]\nXlated: @result[0]\n 'e' count: @result[1]\n";
+
diff --git a/challenge-001/0rir/raku/ch-2.raku b/challenge-001/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..5c72c6388c
--- /dev/null
+++ b/challenge-001/0rir/raku/ch-2.raku
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+001-2
+Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any number divisible by 3 should be replaced by the word ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both divisible by 3 and 5 become ‘fizzbuzz’.
+=end comment
+
+my $exp =
+ "1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz"
+;
+
+sub fizzbuzz($sentinel = 20) {
+ my $ret;
+ for 1..$sentinel -> $n {
+ given $n {
+ when $n %% 3 {
+ $ret ~= 'fizz';
+ if $n %% 5 { $ret ~= 'buzz' }
+ $ret ~= ' ';
+ }
+ when $n %% 5 { $ret ~= 'buzz ' }
+ default { $ret ~= "$n " }
+ }
+ }
+ $ret.trim-trailing;
+}
+
+plan 1;
+is fizzbuzz(), $exp, 'fizzy';
+
+done-testing;
+
+say "\nfizzbuzzing: &fizzbuzz()";
+
+exit;
+
diff --git a/challenge-002/0rir/raku/ch-1.raku b/challenge-002/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..e7402ea791
--- /dev/null
+++ b/challenge-002/0rir/raku/ch-1.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+002-1
+Write a script or one-liner to remove leading zeros from positive numbers.
+=end comment
+
+my @Test =
+ '00000005.1', '5.1',
+ '0005.1', '5.1',
+ '005.1', '5.1',
+ '05.1', '5.1',
+ '5.1', '5.1',
+ 5.1, '5.1',
+ .051, '.051',
+ 0.0051, '.0051',
+ 0.1, '.1',
+;
+plan @Test ÷ 2;
+
+sub func( $n where $n.Rat > 0 -->Str ){
+ $n.subst( / ^ '0'+ /);
+}
+
+for @Test -> $in, $exp {
+ is func($in), $exp, "$exp <- $in";
+}
+
+done-testing;
+
+exit;
+
diff --git a/challenge-002/0rir/raku/ch-2.raku b/challenge-002/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..26a997e008
--- /dev/null
+++ b/challenge-002/0rir/raku/ch-2.raku
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+002-2
+Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y. Dave Jacoby came up with nice description about base35, in case you needed some background.
+=end comment
+
+plan 2001;
+
+sub func( Int $a) { $a.base(35) }
+
+my $n = -10_000_000;
+while $n < 10_000_000 {
+ $n += 9999;
+ is func( $n).parse-base(35), "$n", "N $n -> $n.base(35)";
+}
+
+done-testing;
diff --git a/challenge-003/0rir/raku/ch-2.raku b/challenge-003/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..7becc04b82
--- /dev/null
+++ b/challenge-003/0rir/raku/ch-2.raku
@@ -0,0 +1,30 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+002-2
+
+Create a script that generates Pascal Triangle. Accept number of rows from the command line. The Pascal Triangle should have at least 3 rows. For more information about Pascal Triangle, check this wikipedia page.
+=end comment
+
+my @Test =
+ 0,
+;
+plan @Test ÷ 2;
+
+sub func( $a) {
+ …
+}
+
+for @Test -> $in, $exp {
+ is func($in), $exp, "$exp <- $in";
+}
+
+done-testing;
+my @X = X;
+
+exit;
+
diff --git a/challenge-004/0rir/raku/Pi1000.rakumod b/challenge-004/0rir/raku/Pi1000.rakumod
new file mode 100644
index 0000000000..fb47d5fb49
--- /dev/null
+++ b/challenge-004/0rir/raku/Pi1000.rakumod
@@ -0,0 +1,5 @@
+
+unit module Pi1000;
+
+sub Pi( --> RatStr) is export {
+ <3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989> }
diff --git a/challenge-004/0rir/raku/ch-1.raku b/challenge-004/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..3ed1586966
--- /dev/null
+++ b/challenge-004/0rir/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#!/usr/bin/env raku
+use lib $?FILE.IO.cleanup.parent;
+use Pi1000;
+
+# 004-1 This script outputs PI w/ digit count == its file size.
+
+say Pi().substr( 0, $*PROGRAM.s + 1);
diff --git a/challenge-260/0rir/raku/ch-1.raku b/challenge-260/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..a1aeab2376
--- /dev/null
+++ b/challenge-260/0rir/raku/ch-1.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+260-1: Unique Occurrences Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints. Write a script to return 1 if
+the number of occurrences of each value in the given array is unique or 0
+otherwise.
+
+Example 1
+Input: @ints = (1,2,2,1,1,3)
+Output: 1
+
+The number 1 occurred 3 times.
+The number 2 occurred 2 times.
+The number 3 occurred 1 time.
+
+All occurrences are unique, therefore the output is 1.
+Example 2
+Input: @ints = (1,2,3)
+Output: 0
+Example 3
+Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9)
+Output: 1
+=end comment
+
+my @Test =
+ (1,2,2,1,1,3), True,
+ (1,2,3), False,
+ (-2,0,1,-2,1,1,0,1,-2,9), True,
+ (0…10000).List, False,
+ (1…100_000, 999_999).List, False,
+ (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat, True;
+;
+
+plan @Test ÷ 2;
+
+sub func( $l -->Bool) {
+ my $k = $l.Bag.values.sort.List;
+ $k eqv $k.unique.List;
+}
+
+for @Test -> $in, $exp {
+ is func($in), $exp, ($++).Str;
+}
+
+done-testing;
+my @int = (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat;
+
+say "\nInput: @int = @int[]\nOutput: ", func(@int).Int;
+
+
+exit;
+
diff --git a/challenge-260/0rir/raku/ch-2.raku b/challenge-260/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..a5195503c4
--- /dev/null
+++ b/challenge-260/0rir/raku/ch-2.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+Task 2: Dictionary Rank
+Submitted by: Mark Anderson You are given a word, $word. Write a
+script to compute the dictionary rank of the given word.
+
+Example 1
+Input: $word = 'CAT'
+Output: 3
+
+All possible combinations of the letters:
+CAT, CTA, ATC, TCA, ACT, TAC
+
+Arrange them in alphabetical order:
+ACT, ATC, CAT, CTA, TAC, TCA
+
+CAT is the 3rd in the list.
+Therefore the dictionary rank of CAT is 3.
+Example 2
+Input: $word = 'GOOGLE'
+Output: 88
+Example 3
+Input: $word = 'SECRET'
+Output: 255
+=end comment
+
+=begin comment
+I am just using the built-in &permutation, but this can be calculated more
+efficiently than generating the permutations.
+
+'TADE' and 'TEDA', since they start with the 4th ranked letter, will be in
+the index range 4! minus 3! … 4! So with bookkeeping the problem can be
+partitioned and repartitioned. This simplification doesn't address that
+duplicates share identity.
+=end comment
+
+my @Test =
+ Str, Int,
+ '', Int,
+ 'XYZ', 1,
+ "ooO", 1,
+ 'Deet', 1,
+ 'CAT', 3,
+ 'TADE', 19,
+ 'Cat', 3,
+ 'GOOGLE', 88,
+ 'SECRET', 255,
+;
+plan @Test ÷ 2;
+
+# grind it
+multi gen( Str:U $a -->Int) { Int }
+multi gen( Str:D $a where * eqv '' -->Int) { Int }
+multi gen( Str:D $a -->Int) {
+ ( $a.fc.comb.permutations».join).unique.sort.first( $a.fc, :k) + 1
+}
+
+for @Test -> $in, $exp {
+ is gen($in), gen($in), "$exp.raku() <- $in.raku()";
+}
+
+done-testing;
+my $word = 'Supercad';
+say "\nInput: \$word = $word\nOutput: &gen($word)";
+
+