aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-07 18:48:51 +0000
committerGitHub <noreply@github.com>2021-02-07 18:48:51 +0000
commit28d387605c821e812b69dda6301a5eed9e7030a3 (patch)
tree75fb7646f8fc632cfc03a06ba0da285eac9c2b74
parentf1ab52524dc39482613f3a333cd1dc0f2d234377 (diff)
parent3697da387b9409b5760c1ff78297f43dc0ff3499 (diff)
downloadperlweeklychallenge-club-28d387605c821e812b69dda6301a5eed9e7030a3.tar.gz
perlweeklychallenge-club-28d387605c821e812b69dda6301a5eed9e7030a3.tar.bz2
perlweeklychallenge-club-28d387605c821e812b69dda6301a5eed9e7030a3.zip
Merge pull request #3471 from mimosinnet/branch-for-challenge-098
Solution for challenge-098
-rw-r--r--challenge-098/mimosinnet/raku/ch-1.raku38
-rw-r--r--challenge-098/mimosinnet/raku/ch-2.raku63
-rw-r--r--challenge-098/mimosinnet/raku/input.txt1
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-098/mimosinnet/raku/ch-1.raku b/challenge-098/mimosinnet/raku/ch-1.raku
new file mode 100644
index 0000000000..e7cfb9379f
--- /dev/null
+++ b/challenge-098/mimosinnet/raku/ch-1.raku
@@ -0,0 +1,38 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-098/
+
+class Read-Chars {
+
+ has $!content;
+ has $!pointer;
+
+ submethod TWEAK {
+ $!content = 'input.txt'.IO.slurp.chomp;
+ $!pointer = 0;
+ }
+
+ method read-chars ( $chars ) {
+ my $string = substr($!content,$!pointer,$chars);
+ $!pointer = $!pointer + $chars;
+ $!pointer = $!content.chars if $!pointer > $!content.chars;
+ return $string;
+ }
+
+}
+
+multi sub MAIN( 'challenge' ) {
+ my $read = Read-Chars.new;
+ say 'Input: Suppose the file (input.txt) contains "1234567890"';
+ say 'Output:';
+ say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
+ say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
+ say "\tprint readN(\"input.txt\", 4): " ~ $read.read-chars(4);
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my $read = Read-Chars.new;
+ is $read.read-chars(4), '1234';
+ is $read.read-chars(4), '5678';
+ is $read.read-chars(4), '90';
+}
diff --git a/challenge-098/mimosinnet/raku/ch-2.raku b/challenge-098/mimosinnet/raku/ch-2.raku
new file mode 100644
index 0000000000..6c120ab761
--- /dev/null
+++ b/challenge-098/mimosinnet/raku/ch-2.raku
@@ -0,0 +1,63 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-098/
+
+sub challenge( @array, $target ) {
+ return 0 if $target < @array[0];
+ return @array.elems if $target > @array[*-1];
+ return @array.grep(/$target/, :k).Str if $target ∈ @array;
+ @array.first: * > $target, :k;
+}
+
+multi sub MAIN( @array, $target ) {
+ say 'Input: @N = ' ~ @array.raku ~ ' and $N = ' ~ $target;
+ say 'Output: ' ~ challenge(@array, $target), "\n";
+}
+
+multi sub MAIN( 'challenge' ) {
+ my @challenge = (
+ (
+ (1, 2, 3, 4), 3
+ ),
+ (
+ (1, 3, 5, 7), 6
+ ),
+ (
+ (12, 14, 16, 18),10
+ ),
+ (
+ (11, 13, 15, 17), 19
+ )
+ );
+ for @challenge -> [ @array, $target ] {
+ MAIN( @array, $target);
+ }
+}
+
+multi sub MAIN( 't' ) {
+ MAIN(
+ (1, 3, 5, 7), 6
+ );
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my @tests = (
+ (
+ ((1, 2, 3, 4), 3),2
+ ),
+ (
+ ((1, 3, 5, 7), 6),3
+ ),
+ (
+ ((12, 14, 16, 18),10),0
+ ),
+ (
+ ((11, 13, 15, 17), 19),4
+ )
+ );
+
+ for @tests -> [@a, $b] {
+ is challenge(@a[0],@a[1]), $b;
+ }
+
+}
diff --git a/challenge-098/mimosinnet/raku/input.txt b/challenge-098/mimosinnet/raku/input.txt
new file mode 100644
index 0000000000..a32a4347a4
--- /dev/null
+++ b/challenge-098/mimosinnet/raku/input.txt
@@ -0,0 +1 @@
+1234567890