diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-01 17:26:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-01 17:26:02 +0100 |
| commit | 3cd13a75427c426df87fc4e7395ad59d3c9e0e0d (patch) | |
| tree | 2c88f09090da9612f53d3ee2e827d42bf89c286b | |
| parent | d67822d626374fde369ee19ba98553661df3c3b3 (diff) | |
| parent | 2b822785f85321c29d4d9e6a55b797ab9140afd0 (diff) | |
| download | perlweeklychallenge-club-3cd13a75427c426df87fc4e7395ad59d3c9e0e0d.tar.gz perlweeklychallenge-club-3cd13a75427c426df87fc4e7395ad59d3c9e0e0d.tar.bz2 perlweeklychallenge-club-3cd13a75427c426df87fc4e7395ad59d3c9e0e0d.zip | |
Merge pull request #1782 from fluca1978/pwc63
Pwc63
| -rw-r--r-- | challenge-063/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-063/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-063/luca-ferrari/raku/ch-1.p6 | 46 | ||||
| -rw-r--r-- | challenge-063/luca-ferrari/raku/ch-2.p6 | 45 |
4 files changed, 93 insertions, 0 deletions
diff --git a/challenge-063/luca-ferrari/blog-1.txt b/challenge-063/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..fea9d92892 --- /dev/null +++ b/challenge-063/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/01/PerlWeeklyChallenge63.html#task1 diff --git a/challenge-063/luca-ferrari/blog-2.txt b/challenge-063/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..99bc798d26 --- /dev/null +++ b/challenge-063/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/01/PerlWeeklyChallenge63.html#task2 diff --git a/challenge-063/luca-ferrari/raku/ch-1.p6 b/challenge-063/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..e96cf8c939 --- /dev/null +++ b/challenge-063/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,46 @@ +#!env raku + + +# Define sub last_word($string, $regexp) that returns the last word matching $regexp found in the given string, or undef if the string does not contain a word matching $regexp. +# +# For this challenge, a “word” is defined as any character sequence consisting of non-whitespace characters (\S) only. That means punctuation and other symbols are part of the word. +# +# The $regexp is a regular expression. Take care that the regexp can only match individual words! See the Examples for one way this can break if you are not careful. +# Examples +# +# last_word(' hello world', qr/[ea]l/); # 'hello' +# last_word("Don't match too much, Chet!", qr/ch.t/i); # 'Chet!' +# last_word("spaces in regexp won't match", qr/in re/); # undef +# last_word( join(' ', 1..1e6), qr/^(3.*?){3}/); # '399933' +# + + + +sub last_word( $string, $regexp ){ + my $last-word = Nil; + for $string.split( " " ) { + $last-word = $_ if $_ ~~ $regexp; + } + + return $last-word; +} + + +sub last_word_shorter( $string, $regexp ){ + for $string.split( " " ).reverse { + return $_ if $_ ~~ $regexp; + } +} + + +sub MAIN(){ + 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:s/ in re / ); # undef + say last_word( join(' ', 1..1e6), rx/ ^ (3.*?) ** 3 / ); # '399933' + + say last_word_shorter(' hello world', rx/<[ea]>l/); # 'hello' + say last_word_shorter("Don't match too much, Chet!", rx:i/ch.t/); # 'Chet!' + say last_word_shorter("spaces in regexp won't match", rx:s/ in re / ); # undef + say last_word_shorter( join(' ', 1..1e6), rx/ ^ (3.*?) ** 3 / ); # '399933' +} diff --git a/challenge-063/luca-ferrari/raku/ch-2.p6 b/challenge-063/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..e80cf66f58 --- /dev/null +++ b/challenge-063/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,45 @@ +#!env raku + + +# Given a word made up of an arbitrary number of x and y characters, that word can be rotated as follows: For the ith rotation (starting at i = 1), i % length(word) characters are moved from the front of the string to the end. Thus, for the string xyxx, the initial (i = 1) % 4 = 1 character (x) is moved to the end, forming yxxx. On the second rotation, (i = 2) % 4 = 2 characters (yx) are moved to the end, forming xxyx, and so on. See below for a complete example. +# +# Your task is to write a function that takes a string of xs and ys and returns the maximum non-zero number of rotations required to obtain the original string. You may show the individual rotations if you wish, but that is not required. +# Example +# +# Input: $word = 'xyxx'; +# +# Rotation 1: you get yxxx by moving x to the end. +# Rotation 2: you get xxyx by moving yx to the end. +# Rotation 3: you get xxxy by moving xxy to the end. +# Rotation 4: you get xxxy by moving nothing as 4 % length(xyxx) == 0. +# Rotation 5: you get xxyx by moving x to the end. +# Rotation 6: you get yxxx by moving xx to the end. +# Rotation 7: you get xyxx by moving yxxx to the end which is same as the given word. +# +# Output: 7 + +sub rotate( $string where { $string.split( '', :skip-empty ).chars > 1 }, $verbose = False ){ + my $step = 1; + my @chars = $string.split( '', :skip-empty ); + + + while ( $step == 1 || $string ne @chars.join( '' ) ) { + say "Rotation $step the string is: {@chars}" if $verbose; + + # move the first character to the end + @chars.push: @chars.shift for 0 ..^ ( $step % $string.chars ); + + + say "After step $step the string is: { @chars }" if $verbose; + + $step++; + } + + return $step - 1; +} + + +sub MAIN(){ + say rotate( 'xyxx' ); + say rotate( 'xyxx', True ); +} |
