diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2019-11-30 13:09:32 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2019-11-30 13:09:32 +0100 |
| commit | b0b7d523f9c49f536cd7f2da7807c33378c1db44 (patch) | |
| tree | fb08e01aec3867bac9e5d508a444734973396070 /challenge-036 | |
| parent | 072afe25773826b58c34300dfad482957a41b958 (diff) | |
| download | perlweeklychallenge-club-b0b7d523f9c49f536cd7f2da7807c33378c1db44.tar.gz perlweeklychallenge-club-b0b7d523f9c49f536cd7f2da7807c33378c1db44.tar.bz2 perlweeklychallenge-club-b0b7d523f9c49f536cd7f2da7807c33378c1db44.zip | |
Solution 36 LK
Diffstat (limited to 'challenge-036')
| -rw-r--r-- | challenge-036/lubos-kolouch/perl5/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-036/lubos-kolouch/perl5/ch-2.pl | 95 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-036/lubos-kolouch/perl5/ch-1.pl b/challenge-036/lubos-kolouch/perl5/ch-1.pl new file mode 100644 index 0000000000..6a13a059bf --- /dev/null +++ b/challenge-036/lubos-kolouch/perl5/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-036/ +# +# TASK #1 +# Write a program to validate given Vehicle Identification Number (VIN). For more information, please checkout wikipedia. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: YOUR NAME (), +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 11/30/2019 12:22:41 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; +use Data::Validate::VIN; + + +my $vin = $ARGV[0] // die 'Usage: ch-1.pl vin_code'; + +my $vv = Data::Validate::VIN->new($vin); + +if ( $vv->valid() ) { + say 'valid'; +} +else { + say "ERROR: $_" for @{ $vv->errors() }; +} + + diff --git a/challenge-036/lubos-kolouch/perl5/ch-2.pl b/challenge-036/lubos-kolouch/perl5/ch-2.pl new file mode 100644 index 0000000000..34d1104d5f --- /dev/null +++ b/challenge-036/lubos-kolouch/perl5/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-036/ +# +# Write a program to solve Knapsack Problem. +# +# There are 5 color coded boxes with varying weights and amounts in GBP. Which boxes should be choosen to maximize the amount of money while still keeping the overall weight under or equal to 15 kgs? +# R: (weight = 1 kg, amount = £1) +#B: (weight = 1 kg, amount = £2) +#G : (weight = 2 kg, amount = £2) +#Y: (weight = 12 kg, amount = £4) +#P: (weight = 4 kg, amount = £10) +# +#Bonus task, what if you were allowed to pick only 2 boxes or 3 boxes or 4 boxes? Find out which combination of boxes is the most optimal? +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: YOUR NAME (), +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 11/30/2019 12:30:15 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; +use Math::Combinatorics; + +my $weight_limit = 15; +my %weight = ( + "R" => 1, + "B" => 1, + "G" => 2, + "Y" => 12, + "P" => 4,); + +my %value = ( + "R" => 1, + "B" => 2, + "G" => 2, + "Y" => 4, + "P" => 10,); + +sub calculate_value { + my $combo = shift; + my $total_we; + my $total_va; + + for (@$combo) { + $total_we += $weight{$_}; + $total_va += $value{$_}; + } + + return $total_va if $total_we <= $weight_limit; + + return 0; +} + + +# --------- MAIN --------- +my @list = qw/R G B Y P/; + +for my $count ( 1 .. scalar @list ) { + my $max_value = 0; + my $max_boxes; + + my $combinat = Math::Combinatorics->new( + count => $count, + data => [@list], + ); + + while ( my @combo = $combinat->next_combination ) { + my $boxes = join( ' ', @combo ); + my $val = calculate_value(\@combo); + if ($val > $max_value) { + $max_value = $val; + $max_boxes = $boxes; + } + } + + if ($max_value) { + say "Best combination for $count boxes is $max_boxes with value $max_value"; + } else { + say "There is no possible combination for $count boxes under or equal value $max_value"; + } +} + |
