aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-06 21:34:54 +0000
committerGitHub <noreply@github.com>2021-02-06 21:34:54 +0000
commit0b3697fb5a4464acd27c5f0ea46d23cdfa698235 (patch)
tree64e426ea4f23fa8fbb3e0f7bbb954819a3f93442 /challenge-098
parent6109e2b651df95a98748a269c6665c6b129f7027 (diff)
parentb202e74e4ae549825fef4d853357170030526bfa (diff)
downloadperlweeklychallenge-club-0b3697fb5a4464acd27c5f0ea46d23cdfa698235.tar.gz
perlweeklychallenge-club-0b3697fb5a4464acd27c5f0ea46d23cdfa698235.tar.bz2
perlweeklychallenge-club-0b3697fb5a4464acd27c5f0ea46d23cdfa698235.zip
Merge pull request #3466 from aaronreidsmith/challenge-098
Challenge 98 - Raku
Diffstat (limited to 'challenge-098')
-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;
+}