diff options
| -rw-r--r-- | challenge-075/bob-lied/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-075/bob-lied/perl/lib/CoinSum.pm | 59 | ||||
| -rw-r--r-- | challenge-075/bob-lied/perl/t/CoinSum.t | 24 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-075/bob-lied/perl/ch-1.pl b/challenge-075/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..42b51a19f3 --- /dev/null +++ b/challenge-075/bob-lied/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu syntax=perl: +# +# Copyright (c) 2020 Bob Lied +# The copyright notice above does not evidence any actual +# or intended publication of such source code. + +#=============================================================================== +# ch-1.pl +# +# Description: +# Perl Weekly Challenge 075 Task #1 > Coins Sum +#=============================================================================== +# You are given a set of coins @C, assuming you have infinite amount of each coin in the set. +# Write a script to find how many ways you make sum $S using the coins from the set @C. +# +# Example: +# Input: +# @C = (1, 2, 4) +# $S = 6 +# +# Output: 6 +# There are 6 possible ways to make sum 6. +# a) (1, 1, 1, 1, 1, 1) +# b) (1, 1, 1, 1, 2) +# c) (1, 1, 2, 2) +# d) (1, 1, 4) +# e) (2, 2, 2) +# f) (2, 4) + +use strict; +use warnings; +use feature qw(say); + +use lib "lib"; +use CoinSum qw(coinSum); + +sub Usage { "Usage: $0 SUM coin1 [coin2..coinN]" }; + +my $S = shift; +my @C = @ARGV; + +die Usage() unless $S; +die Usage() unless @C; + +# Sort denominations so largest is first. +@C = sort { $a < $b } @C; + +coinSum($S, @C); diff --git a/challenge-075/bob-lied/perl/lib/CoinSum.pm b/challenge-075/bob-lied/perl/lib/CoinSum.pm new file mode 100644 index 0000000000..a5a7f818a5 --- /dev/null +++ b/challenge-075/bob-lied/perl/lib/CoinSum.pm @@ -0,0 +1,59 @@ +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu syntax=perl: +# +# Copyright (c) 2020 Bob Lied +# The copyright notice above does not evidence any actual +# or intended publication of such source code. + +#=============================================================================== +# CoinSum.pm +# +# Description: +# +#=============================================================================== + +package CoinSum; + +use strict; +use warnings; +use 5.010; +use Carp; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(coinSum); +our @EXPORT_OK = qw(); + + +my @Combo; + +sub _coinSum +{ + my ($target, $denomList, $currentSum, $currentDenom, $comboNum = @_; + + return 0 if ( $currentSum > $target ); + + return 1 if ( $currentSum == $target ); + + my $count = 0; + for my $denom ( @$denomList ) + { + push @{$Combo[$comboNum]] $denom; + if ( $denom >= $currentDenom ) + { + $comboNum += _coinSum($target, $denomList, $currentSum + $currentDenom, $denom, $comboNum); + } + } + return $0; +} + +sub coinSum +{ + my ($sum, @coins) = @_; + + _coinSum($sum, \@coins, 0, $coins[0], 0); + + return 0; +} + +1; + diff --git a/challenge-075/bob-lied/perl/t/CoinSum.t b/challenge-075/bob-lied/perl/t/CoinSum.t new file mode 100644 index 0000000000..2157772195 --- /dev/null +++ b/challenge-075/bob-lied/perl/t/CoinSum.t @@ -0,0 +1,24 @@ +#=============================================================================== +# +# FILE: CoinSum.t +# +# DESCRIPTION: +# +# FILES: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Bob Lied (RL), bob.lied@nokia.com +# ORGANIZATION: PNM +# VERSION: 1.0 +# CREATED: 2020-08-24 10:10:03 AM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use 5.010; + +use Test2::V0; + +done_testing(); + |
