diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-04 12:12:29 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-04 12:12:29 +0000 |
| commit | 6619f2571264d4c6c73fadbdc972e76dd5836bdb (patch) | |
| tree | 8e73e6c6f95fba11598322fa7cb68888504f7adb /challenge-113 | |
| parent | 9cfe9430aee48716f1e4f6da28f5a4763118a373 (diff) | |
| download | perlweeklychallenge-club-6619f2571264d4c6c73fadbdc972e76dd5836bdb.tar.gz perlweeklychallenge-club-6619f2571264d4c6c73fadbdc972e76dd5836bdb.tar.bz2 perlweeklychallenge-club-6619f2571264d4c6c73fadbdc972e76dd5836bdb.zip | |
Fix task 1 of challenge 113: accept any combinations of numbers containing D
Diffstat (limited to 'challenge-113')
| -rw-r--r-- | challenge-113/paulo-custodio/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-113/paulo-custodio/python/ch-1.py | 16 | ||||
| -rw-r--r-- | challenge-113/paulo-custodio/t/test-1.yaml | 5 |
3 files changed, 37 insertions, 11 deletions
diff --git a/challenge-113/paulo-custodio/perl/ch-1.pl b/challenge-113/paulo-custodio/perl/ch-1.pl index b9badbba36..5cee958f39 100644 --- a/challenge-113/paulo-custodio/perl/ch-1.pl +++ b/challenge-113/paulo-custodio/perl/ch-1.pl @@ -18,14 +18,29 @@ # Output: 1 use Modern::Perl; -my($N, $D) = @ARGV; -say represent($N||0, $D||0) ? 1 : 0; +use Math::Combinatorics; +use List::Util 'sum'; -sub represent { +sub nums_containing { my($n, $d) = @_; - my $sum = 0; + my @nums; for (1..$n) { - $sum += $_ if /$d/; + push @nums, $_ if /$d/; + } + return @nums; +} + +sub represent { + my($n, $d) = @_; + my @nums = nums_containing($n, $d); + for my $k (1 .. @nums) { + for my $combin (combine($k, @nums)) { + return 1 if sum(@$combin) == $n; + } } - return $sum==$n; + return 0; } + + +my($N, $D) = @ARGV or die "Usage: ch-1.pl N D\n"; +say represent($N, $D) ? 1 : 0; diff --git a/challenge-113/paulo-custodio/python/ch-1.py b/challenge-113/paulo-custodio/python/ch-1.py index e544a5ccd8..5c1e781f0c 100644 --- a/challenge-113/paulo-custodio/python/ch-1.py +++ b/challenge-113/paulo-custodio/python/ch-1.py @@ -19,13 +19,19 @@ import sys import re +from itertools import combinations + +def nums_containing(n, d): + nums = list(filter(lambda x: re.search(str(d), str(x)), range(n+1))) + return nums def represent(n, d): - sum = 0 - for i in range(n+1): - if re.search(str(d), str(i)): - sum += i - return sum==n + nums = nums_containing(n, d) + for k in range(1, len(nums)+1): + for combin in combinations(nums, k): + if sum(combin) == n: + return True + return False if represent(int(sys.argv[1]), int(sys.argv[2])): print(1) diff --git a/challenge-113/paulo-custodio/t/test-1.yaml b/challenge-113/paulo-custodio/t/test-1.yaml index f3baa5852a..3c31c6f1e8 100644 --- a/challenge-113/paulo-custodio/t/test-1.yaml +++ b/challenge-113/paulo-custodio/t/test-1.yaml @@ -8,3 +8,8 @@ args: 24 7 input: output: 1 +- setup: + cleanup: + args: 54 7 + input: + output: 1 |
