diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-08-27 20:56:32 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-08-27 20:56:32 +0200 |
| commit | 5648b83768e8ab5cf00d18c3e551cbb98d019970 (patch) | |
| tree | 3ec48056728e747a658d311c96a0cb1195b997f7 | |
| parent | a33ba82d4bf61cc6e6f028c5bac893de78c161a6 (diff) | |
| download | perlweeklychallenge-club-5648b83768e8ab5cf00d18c3e551cbb98d019970.tar.gz perlweeklychallenge-club-5648b83768e8ab5cf00d18c3e551cbb98d019970.tar.bz2 perlweeklychallenge-club-5648b83768e8ab5cf00d18c3e551cbb98d019970.zip | |
Solutions Chal 075 Task 1 Perl Python LK
| -rw-r--r-- | challenge-075/lubos-kolouch/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-075/lubos-kolouch/python/ch_1.py | 21 |
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-075/lubos-kolouch/perl/ch-1.pl b/challenge-075/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..756cba3ebf --- /dev/null +++ b/challenge-075/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use strict; +use warnings; +# Perl weekly challenge 075 Task 1 - Coins sum + + +sub count { + + my ($coins, $target_sum) = @_; + + my @ways = (0) x $target_sum; + $ways[0] = 1; + + for my $coin_pos (0..scalar @$coins - 1) { + for my $j (0..$target_sum) { + $ways[$j] += $ways[$j - $coins->[$coin_pos]] if $coins->[$coin_pos] <= $j; + } + } + + return $ways[$target_sum]; +} + +use Test::More; + +is(count([1, 2, 4], 6), 6, 'Test case 1, 2, 4 and 6'); +is(count([1, 5, 10], 12), 4, 'Test case 1, 5, 10 and 12'); +done_testing; diff --git a/challenge-075/lubos-kolouch/python/ch_1.py b/challenge-075/lubos-kolouch/python/ch_1.py new file mode 100644 index 0000000000..c706f7f5a0 --- /dev/null +++ b/challenge-075/lubos-kolouch/python/ch_1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +""" Perl weekly challenge 075 Task 1 - Coins sum """ + + +def count(coins, target_sum): + """ count the ways we can sum coins """ + + ways = [0] * (target_sum + 1) + + ways[0] = 1 + + for value in coins: + for j, _ in enumerate(ways): + if value <= j: + ways[j] += ways[j - value] + + return ways[target_sum] + + +assert count([1, 2, 4], 6) == 6 +assert count([1, 5, 10], 12) == 4 |
