diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-06-21 15:43:01 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-06-21 15:43:01 +0200 |
| commit | 2928d12581d18146cc8ade6c906fd9f61e2d028e (patch) | |
| tree | 1887e6c33854cd8fa874039a109c72e4f85b0fe3 /challenge-065 | |
| parent | d21e1f0cecb76bad14978902f80a9379f7d6b953 (diff) | |
| download | perlweeklychallenge-club-2928d12581d18146cc8ade6c906fd9f61e2d028e.tar.gz perlweeklychallenge-club-2928d12581d18146cc8ade6c906fd9f61e2d028e.tar.bz2 perlweeklychallenge-club-2928d12581d18146cc8ade6c906fd9f61e2d028e.zip | |
Solutions to challenge 65 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-065')
| -rw-r--r-- | challenge-065/noud/raku/ch-1.p6 | 19 | ||||
| -rw-r--r-- | challenge-065/noud/raku/ch-2.p6 | 118 |
2 files changed, 137 insertions, 0 deletions
diff --git a/challenge-065/noud/raku/ch-1.p6 b/challenge-065/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..3dbf97fc07 --- /dev/null +++ b/challenge-065/noud/raku/ch-1.p6 @@ -0,0 +1,19 @@ +# 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 digits-sum($N, $S) { + ([X] ((1..9), |(^9 xx ^($N-1)))).grep({ ([+] $_) eq $S; }).map({ [~] $_ }); +} + +digits-sum(2, 4).say; diff --git a/challenge-065/noud/raku/ch-2.p6 b/challenge-065/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..2c0073b191 --- /dev/null +++ b/challenge-065/noud/raku/ch-2.p6 @@ -0,0 +1,118 @@ +# 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. +# +# Example 1 +# +# Input: $S = 'aabaab' +# Ouput: +# There are 3 possible solutions. +# a) 'aabaa' +# b) 'aa', 'baab' +# c) 'aba' +# +# Example 2 +# +# Input: $S = 'abbaba' +# Output: +# There are 3 possible solutions. +# a) 'abba' +# b) 'bb', 'aba' +# c) 'bab' + + +# I don't understand the task. In my oppinion a single character is a +# palindrome as well. Also I don't agree that 'aba' of string 'aabaab' is a +# palindrome, because the rest of the string 'a' and 'ab' are not taken into +# account. Even if you add the extra restriction that every palindrome should +# have at least 2 characters, the string 'aabaab' also has to solution 'aa' and +# 'aa'. +# +# I created two other solutions instead: +# 1. weak-palindromes, where non-used parts are allowed. +# 2. strong-palindromes, where the whole string has to be used. + +sub all-partitions-weak($S) { + if ($S.chars == 1) { + return [[$S],]; + } + + my @ret = [[$S],]; + for 1..($S.chars - 1) -> $i { + for $i..($S.chars - 1) -> $j { + my $s1 = $S.substr(0, $i); + my $s2 = $S.substr($j, $S.chars); + for all-partitions-weak($s2) -> $r { + @ret.push([$s1, |($r)]); + } + @ret.push([$s1]); + @ret.push([$s2]); + } + } + + return @ret; +} + +sub all-partitions-strong($S) { + if ($S.chars == 1) { + return [[$S],]; + } + + my @ret = [[$S],]; + for 1..($S.chars - 1) -> $i { + my $s1 = $S.substr(0, $i); + my $s2 = $S.substr($i, $S.chars); + for all-partitions-strong($s2) -> $r { + @ret.push([$s1, |($r)]); + } + } + + return @ret; +} + +sub all-palindrome(@A) { + for @A -> $a { + if (not $a.flip eq $a) { + return False; + } + } + return True; +} + +say 'Weak palindromes:'; +all-partitions-weak('aabaab').grep({ all-palindrome($_ ); }).say; +all-partitions-weak('abbaba').grep({ all-palindrome($_ ); }).say; + +say 'Strong palindromes:'; +all-partitions-strong('aabaab').grep({ all-palindrome($_ ); }).say; +all-partitions-strong('abbaba').grep({ all-palindrome($_ ); }).say; + +# With the extra restriction of at least two characters for a palindrome. + +sub at-least-two(@A) { + for @A -> $a { + if ($a.chars < 2) { + return False; + } + } + return True; +} + +say 'Weak palindromes (len > 1):'; +all-partitions-weak('aabaab').grep({ + all-palindrome($_ ) and at-least-two($_); +}).say; +all-partitions-weak('abbaba').grep({ + all-palindrome($_ ) and at-least-two($_); +}).say; + +say 'Strong palindromes (len > 1):'; +all-partitions-strong('aabaab').grep({ + all-palindrome($_ ) and at-least-two($_); +}).say; +all-partitions-strong('abbaba').grep({ + all-palindrome($_ ) and at-least-two($_); +}).say; |
