diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-15 15:02:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-15 15:02:26 +0100 |
| commit | 40d84dd157abdebc9ba229e69f94e79557898a92 (patch) | |
| tree | 48dc60ecfa5bd7ee76975f0266a74be712e87084 | |
| parent | 5bba57922fd66d731b0f6e3380823d632a4e3411 (diff) | |
| parent | 731e2715bf3a5fee96d708e19bbb8497db98de0c (diff) | |
| download | perlweeklychallenge-club-40d84dd157abdebc9ba229e69f94e79557898a92.tar.gz perlweeklychallenge-club-40d84dd157abdebc9ba229e69f94e79557898a92.tar.bz2 perlweeklychallenge-club-40d84dd157abdebc9ba229e69f94e79557898a92.zip | |
Merge pull request #1827 from fluca1978/pwc65
Pwc65
| -rw-r--r-- | challenge-065/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-065/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-065/luca-ferrari/raku/ch-1.p6 | 30 | ||||
| -rw-r--r-- | challenge-065/luca-ferrari/raku/ch-2.p6 | 32 |
4 files changed, 64 insertions, 0 deletions
diff --git a/challenge-065/luca-ferrari/blog-1.txt b/challenge-065/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..9599f014c5 --- /dev/null +++ b/challenge-065/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/15/PerlWeeklyChallenge65.html#task1 diff --git a/challenge-065/luca-ferrari/blog-2.txt b/challenge-065/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..f5d845885d --- /dev/null +++ b/challenge-065/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2020/06/15/PerlWeeklyChallenge65.html#task2 diff --git a/challenge-065/luca-ferrari/raku/ch-1.p6 b/challenge-065/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..15d37d9766 --- /dev/null +++ b/challenge-065/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,30 @@ +#!raku + +# You are given two positive numbers $N and $S. +# +# Write a script to list all positive numbers having +# exactly $N digits where sum of all digits equals to $S. +# Example +# +# Input: +# $N = 2 +# $S = 4 +# +# Output: +# 13, 22, 31, 40 + + + +sub MAIN( Int $N where { $N > 0 }, + Int $S where { $S >= 1 && $S <= 9 } ) { + + + my @found; + "Searching numbers with $N digits that sum to $S between { 1 ~ ( 0 x $N - 1 ) } and { $S ~ ( 0 x $N - 1 ) }".say; + for ( 1 ~ ( 0 x $N - 1 ) ).Int .. ( $S ~ ( 0 x $N - 1 ) ).Int { + @found.push: $_ if ( [+] $_.split( '', :skip-empty ) ) == $S ; + } + + @found.say; + +} diff --git a/challenge-065/luca-ferrari/raku/ch-2.p6 b/challenge-065/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..a30cca8246 --- /dev/null +++ b/challenge-065/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,32 @@ +#!raku + +# You are given a string $S. Write a script print all possible partitions that gives Palindrome. +# Return -1 if none found. +# Please make sure, partition should not overlap. +# For example, for given string “abaab”, +# the partition “aba” and “baab” would not be valid, since they overlap. + + +sub MAIN( Str $S where { $S.chars > 0 } = "abaab" ){ + "Searching non overlapping palindrome partitions on $S".say; + my @palindromes; + + my $last = -1; + for 0 ..^ $S.chars -> $start { + # avoid overlapping + next if $start <= $last; + + for $start + 1 ..^ $S.chars -> $end { + my $string = $S.split( '', :skip-empty )[ $start .. $end ]; + if $string ~~ $string.reverse { + @palindromes.push: $string; + # avoid overlapping + $last = $end; + last; + } + + } + } + + @palindromes ?? @palindromes.join( ',' ).say !! "-1"; +} |
