diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-18 16:50:02 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-18 16:50:02 +0100 |
| commit | 5ff346ea9cf02b42cb749cd01f8cd87f0df2282a (patch) | |
| tree | a809e9bf343324bbae3b699481ecac3f96f30eb1 /challenge-065 | |
| parent | 11f29bf81fc01598cae4f753f34a7812b89a5cae (diff) | |
| download | perlweeklychallenge-club-5ff346ea9cf02b42cb749cd01f8cd87f0df2282a.tar.gz perlweeklychallenge-club-5ff346ea9cf02b42cb749cd01f8cd87f0df2282a.tar.bz2 perlweeklychallenge-club-5ff346ea9cf02b42cb749cd01f8cd87f0df2282a.zip | |
Add Perl solution to challenge 065
Diffstat (limited to 'challenge-065')
| -rw-r--r-- | challenge-065/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/perl/ch-2.pl | 65 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-065/paulo-custodio/t/test-2.yaml | 20 |
6 files changed, 131 insertions, 0 deletions
diff --git a/challenge-065/paulo-custodio/Makefile b/challenge-065/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-065/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-065/paulo-custodio/README b/challenge-065/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-065/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-065/paulo-custodio/perl/ch-1.pl b/challenge-065/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..67d2eb12e6 --- /dev/null +++ b/challenge-065/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# Challenge 065 +# +# TASK #1 › Digits Sum +# Submitted by: Mohammad S Anwar +# Reviewed by: Ryan Thompson +# +# 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 + +use Modern::Perl; +use List::Util 'sum'; + +my($N, $S) = @ARGV; +my @out = digits_sum($N, $S); +say join(", ", @out); + + +sub digits_sum { + my($N, $S) = @_; + my @out; + for my $n (10 ** ($N-1) .. (10 ** $N)-1) { + my @d = split //, $n; + push @out, $n if sum(@d)==$S; + } + return @out; +} diff --git a/challenge-065/paulo-custodio/perl/ch-2.pl b/challenge-065/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1adaf4eb34 --- /dev/null +++ b/challenge-065/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +# Challenge 065 +# +# TASK #2 › Palindrome Partition +# Submitted by: Mohammad S Anwar +# Reviewed by: Ryan Thompson +# +# 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' + +use Modern::Perl; + +partitions(shift||""); + + +sub is_palindrome { + my($s) = @_; + return length($s) >= 2 && $s eq reverse($s); +} + +sub partitions { + my($s) = @_; + my %seen; + my $len = length($s); + for my $lead (0 .. $len) { + for my $p1 (2 .. $len) { + for my $p2 (0, 2 .. $len) { + for my $tail (0 .. $len) { + next unless $lead+$p1+$p2+$tail==$len; + my $s1 = substr($s, $lead, $p1); + my $s2 = substr($s, $lead+$p1, $p2); + if (is_palindrome($s1) && is_palindrome($s2)) { + if (!$seen{"$s1, $s2"}++) { + say "$s1, $s2"; + } + } + elsif (is_palindrome($s1)) { + if (!$seen{$s1}++) { + say $s1; + } + } + } + } + } + } +} diff --git a/challenge-065/paulo-custodio/t/test-1.yaml b/challenge-065/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4328e2726c --- /dev/null +++ b/challenge-065/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2 4 + input: + output: 13, 22, 31, 40 diff --git a/challenge-065/paulo-custodio/t/test-2.yaml b/challenge-065/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..74b3b5dcc6 --- /dev/null +++ b/challenge-065/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: aabaab + input: + output: | + |aa + |aa, baab + |aabaa + |aba + |baab +- setup: + cleanup: + args: abbaba + input: + output: | + |abba + |bb + |bb, aba + |bab + |aba |
