From ea20ed349a9ef101246e58ad227e44ab9e4716e9 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 1 Jun 2020 13:29:26 +0100 Subject: Last words --- challenge-063/simon-proctor/raku/ch-1.raku | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-063/simon-proctor/raku/ch-1.raku diff --git a/challenge-063/simon-proctor/raku/ch-1.raku b/challenge-063/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..dd557ba8d8 --- /dev/null +++ b/challenge-063/simon-proctor/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + +use v6.d; + +sub last_word( Str $sentence, Regex $test ) { + $sentence.words.reverse.first( $test ); +} + +# Test Last_word +say last_word(' hello world', rx/<[ea]>l/); # 'hello' +say last_word("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!' +say last_word("spaces in regexp won't match", rx/"in" " " "re"/); # undef +say last_word( join(' ', 1..1e6), rx/^(3.*?) ** 3/); # '399933' -- cgit From 307ff71cd6cd4f473a7aa2a76d2de009b97646db Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 1 Jun 2020 13:32:18 +0100 Subject: Small fix to space in regex test --- challenge-063/simon-proctor/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-063/simon-proctor/raku/ch-1.raku b/challenge-063/simon-proctor/raku/ch-1.raku index dd557ba8d8..ed1092c70c 100644 --- a/challenge-063/simon-proctor/raku/ch-1.raku +++ b/challenge-063/simon-proctor/raku/ch-1.raku @@ -9,5 +9,5 @@ sub last_word( Str $sentence, Regex $test ) { # Test Last_word say last_word(' hello world', rx/<[ea]>l/); # 'hello' say last_word("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!' -say last_word("spaces in regexp won't match", rx/"in" " " "re"/); # undef +say last_word("spaces in regexp won't match", rx/"in re"/); # undef say last_word( join(' ', 1..1e6), rx/^(3.*?) ** 3/); # '399933' -- cgit From 1cf60888493d988d87466a46aa702097ebe48f07 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 1 Jun 2020 13:50:57 +0100 Subject: Rotating strings --- challenge-063/simon-proctor/raku/ch-2.raku | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-063/simon-proctor/raku/ch-2.raku diff --git a/challenge-063/simon-proctor/raku/ch-2.raku b/challenge-063/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..2696b581f6 --- /dev/null +++ b/challenge-063/simon-proctor/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +use v6.d; + +#| Given a string home many rotations are needed (see the challenge for the rules on rotations). +sub MAIN ( Str $input where m!^<[xy]>+$! ) { + my Int $i = 1; + my Str $rotated = rotate_string( $input, $i ); + + while ( $rotated !~~ $input ) { + $i++; + $rotated = rotate_string( $rotated, $i ); + } + + say $i; +} + +sub rotate_string( Str $rotated, Int $i ) { + my $midpoint = $i % $rotated.codes; + "{$rotated.substr($midpoint,$rotated.codes-$midpoint)}{$rotated.substr(0,$midpoint)}"; +} -- cgit