From 4b6337b51c4f4715fd6abb380fed9307a6ad504a Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Sat, 13 Feb 2021 12:58:26 -0600 Subject: Challenge 99 - Raku --- challenge-099/aaronreidsmith/blog.txt | 1 + challenge-099/aaronreidsmith/raku/ch-1.raku | 28 ++++++++++++++++++++++++++++ challenge-099/aaronreidsmith/raku/ch-2.raku | 26 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 challenge-099/aaronreidsmith/blog.txt create mode 100644 challenge-099/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-099/aaronreidsmith/raku/ch-2.raku diff --git a/challenge-099/aaronreidsmith/blog.txt b/challenge-099/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..56cee9a8f8 --- /dev/null +++ b/challenge-099/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-099/ diff --git a/challenge-099/aaronreidsmith/raku/ch-1.raku b/challenge-099/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..7fd01ed7ac --- /dev/null +++ b/challenge-099/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +sub challenge(Str $S, Str $P) returns Int { + my $regex = '^' ~ $P.trans(['*', '?'] => ['.*', '.']) ~ '$'; + ($S ~~ /<$regex>/).Bool.Int; +} + +multi sub MAIN(Str $S, Str $P) { + say challenge($S, $P); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + ('abcde', 'a*e' , 1), + ('abcde', 'a*d' , 0), + ('abcde', '?b*d' , 0), + ('abcde', 'a*c?e', 1), + ('acde' , 'a*c?e', 1) # Treat '*' as '0 or more' + ); + + for @tests -> ($S, $P, $expected) { + is(challenge($S, $P), $expected); + } + + done-testing; +} diff --git a/challenge-099/aaronreidsmith/raku/ch-2.raku b/challenge-099/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..376d3c46dd --- /dev/null +++ b/challenge-099/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +sub challenge(Str $S, Str $T) returns Int { + my $regex = $T.comb.join('.*'); + ($S ~~ m:exhaustive/<$regex>/).Int; +} + +multi sub MAIN(Str $S, Str $T) { + say challenge($S, $T); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + ('littleit', 'lit', 5), + ('london' , 'lon', 3) + ); + + for @tests -> ($S, $T, $expected) { + is(challenge($S, $T), $expected); + } + + done-testing; +} + -- cgit