aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Smith <asmith@sumologic.com>2021-02-06 15:24:16 -0600
committerAaron Smith <asmith@sumologic.com>2021-02-06 15:24:37 -0600
commitb202e74e4ae549825fef4d853357170030526bfa (patch)
treea181779ac3001415bf874c40782de6c9f8e36130
parentac60bddb13f96402c3026283fe223388ed54fc27 (diff)
downloadperlweeklychallenge-club-b202e74e4ae549825fef4d853357170030526bfa.tar.gz
perlweeklychallenge-club-b202e74e4ae549825fef4d853357170030526bfa.tar.bz2
perlweeklychallenge-club-b202e74e4ae549825fef4d853357170030526bfa.zip
Challenge 98 - Raku
-rw-r--r--challenge-098/aaronreidsmith/blog.txt1
-rw-r--r--challenge-098/aaronreidsmith/raku/ch-1.raku34
-rw-r--r--challenge-098/aaronreidsmith/raku/ch-2.raku27
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-098/aaronreidsmith/blog.txt b/challenge-098/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..bae7addfbc
--- /dev/null
+++ b/challenge-098/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-098/
diff --git a/challenge-098/aaronreidsmith/raku/ch-1.raku b/challenge-098/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..cefbf5770c
--- /dev/null
+++ b/challenge-098/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,34 @@
+#!/usr/bin/env raku
+
+sub readN(Str $file-name, Int $chars-to-read where $chars-to-read > 0) returns Str {
+ state %file-map;
+
+ if %file-map{$file-name}:!exists {
+ %file-map{$file-name} = IO::CatHandle.new($file-name)
+ }
+
+ %file-map{$file-name}.readchars($chars-to-read);
+}
+
+multi sub MAIN(Str $FILE, Int $N) {
+ say readN($FILE, $N);
+ say readN($FILE, $N);
+ say readN($FILE, $N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ # Create a file for testing
+ my $test-file = 'test.txt';
+ $test-file.IO.spurt('1234567890');
+
+ is(readN($test-file, 4), '1234');
+ is(readN($test-file, 4), '5678');
+ is(readN($test-file, 4), '90');
+
+ # Delete when we're done
+ $test-file.IO.unlink;
+
+ done-testing;
+}
diff --git a/challenge-098/aaronreidsmith/raku/ch-2.raku b/challenge-098/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..0c40c7f758
--- /dev/null
+++ b/challenge-098/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,27 @@
+#!/usr/bin/env raku
+
+sub challenge(@N, Int $N) returns Int {
+ my @new = $N ∉ @N ?? (|@N, $N).sort !! @N;
+ @new.first($N, :k);
+}
+
+multi sub MAIN(Int $N, *@N where all(@N) ~~ Int) {
+ say challenge(@N, $N)
+}
+
+multi sub MAIN(Bool :$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 -> (@N, $N, $expected) {
+ is(challenge(@N, $N), $expected);
+ }
+
+ done-testing;
+}