aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-17 13:54:17 +0000
committerGitHub <noreply@github.com>2024-03-17 13:54:17 +0000
commit627452fbfad41f765ef1b02a65a4ef9645d213be (patch)
tree2cfc9f864eba46a8c9e40fae900ef2e180bcdc42
parent24b0c58d2dc13cbc482e6309d349e1322beb7912 (diff)
parentd55c21a7a95f34b4d1958c646a1efba0639fdc00 (diff)
downloadperlweeklychallenge-club-627452fbfad41f765ef1b02a65a4ef9645d213be.tar.gz
perlweeklychallenge-club-627452fbfad41f765ef1b02a65a4ef9645d213be.tar.bz2
perlweeklychallenge-club-627452fbfad41f765ef1b02a65a4ef9645d213be.zip
Merge pull request #9752 from 0rir/work
260, 001, 002, 003, 004
-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.raku33
-rw-r--r--challenge-002/0rir/raku/ch-2.raku21
-rw-r--r--challenge-003/0rir/raku/ch-1.raku49
-rw-r--r--challenge-003/0rir/raku/ch-2.raku41
-rw-r--r--challenge-004/0rir/raku/Pi1000.rakumod5
-rw-r--r--challenge-004/0rir/raku/ch-1.raku22
-rw-r--r--challenge-260/0rir/raku/ch-1.raku56
-rw-r--r--challenge-260/0rir/raku/ch-2.raku71
10 files changed, 363 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..453f5825bd
--- /dev/null
+++ b/challenge-002/0rir/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+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..5458963675
--- /dev/null
+++ b/challenge-002/0rir/raku/ch-2.raku
@@ -0,0 +1,21 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+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-1.raku b/challenge-003/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..a640cc96de
--- /dev/null
+++ b/challenge-003/0rir/raku/ch-1.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+INIT $*RAT-OVERFLOW = FatRat;
+use Test;
+
+=begin comment
+003-1
+Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5.
+They are also called Hamming/Regular/Ugly numbers. For more information, please check this
+wikipedia.
+=end comment
+
+my @Test =
+ 2, 3, 4, 5, 6, 8, 9, 10, 12, 15,
+ 16, 18, 20, 24, 25, 27, 30, 32, 36, 40,
+ 45, 48, 50, 54, 60, 64, 72, 75, 80, 81,
+ 90, 96, 100, 108, 120, 125, 128, 135, 144, 150,
+ 160, 162, 180, 192, 200, 216, 225, 240, 243, 250,
+ 256, 270, 288, 300, 320, 324, 360, 375, 384, 400,
+ 405,
+;
+
+sub is-five-smooth( Int $n -->Bool) { return not so prime-factors($n).any > 5; }
+
+constant @prime = 2,3, { $_+=2; $_+=2 until .is-prime; $_; } … ∞;
+
+constant @five-smooth = 2,3,4,5,6, { $_+=1; $_+=1 until is-five-smooth($_); $_ } … ∞;
+
+sub prime-factors( $int -->Array) is export {
+ my $n = $int;
+ my @ret;
+ my $ix = 0;
+ while $n > 1 {
+ my $candi = @prime[$ix];
+ $ix++;
+ next unless ( $n %% $candi );
+ $ix = 0;
+ $n ÷= $candi;
+ @ret.push: $candi;
+ }
+ return @ret;
+}
+
+plan 1;
+is @five-smooth[0..60], @Test, '@five-smooth[0..60]';
+done-testing;
+
+exit;
diff --git a/challenge-003/0rir/raku/ch-2.raku b/challenge-003/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..df2ee6d60f
--- /dev/null
+++ b/challenge-003/0rir/raku/ch-2.raku
@@ -0,0 +1,41 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+
+=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 $rows = prompt( "Input number of rows wanted: ").Int;
+while $rows < 1 {
+ say 'Need a positive integer answer.';
+ $rows = prompt( "Input number of rows wanted").Int;
+}
+
+say pascal-fmt80( pascal-triangle( $rows));
+
+exit;
+
+sub deepflat(+@list) { gather @list.deepmap: *.take }
+
+multi pascal-triangle( Int $in where * == 1 --> Array) { [[1],] }
+multi pascal-triangle( Int $in where * > 1 --> Array) {
+ my @a = [[1],];
+ for 1..^$in -> $r {
+ @a.push: [deepflat [1, @a[$r - 1].rotor(2 => -1)».sum || Empty, 1]];
+ }
+ return @a;
+}
+
+sub pascal-fmt80( @a) {
+ die 'Cannot format more than 16 rows.' if +@a > 16;
+ my $ret;
+ my $wid = 5;
+ my $dent = 40;
+ my $fmt = '%' ~ $wid ~ 'd';
+ for @a -> @row { $ret ~= ' ' x ($dent -= $wid/2) ~ @row.fmt($fmt, '') ~ "\n" }
+ $ret;
+}
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..835f1e8651
--- /dev/null
+++ b/challenge-004/0rir/raku/ch-1.raku
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+INIT $*RAT-OVERFLOW = FatRat;
+use lib $?FILE.IO.cleanup.parent;
+use Pi1000;
+
+# 004-1 This script outputs PI w/ digit count == its file size.
+
+#________0_________0_________0_________0_________0_________0_________0_______80
+#________0_________0_________0_________0_________0_________0_________0______160
+#________0_________0_________0_________0_________0_________0_________0______240
+#________0_________0_________0_________0_________0_________0_________0______320
+#________0_________0_________0_________0_________0_________0_________0______400
+#________0_________0_________0_________0_________0_________0_________0______480
+#________0_________0_________0_________0_________0_________0_________0______560
+#________0_________0_________0_________0_________0______620
+#say $*PROGRAM.s;
+
+constant π = +Pi();
+my $scale = $*PROGRAM.s - 2;
+die 'Too precise' if $scale ≥ 1002;
+die 'Bad programmer' unless $scale + 2 == + π.round( 10**-$scale).chars;
+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)";
+
+