Created
December 1, 2019 17:56
-
-
Save adamcrussell/f3fcbe885a1fb9f61eabb5f5e64c7424 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 036
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Write a program to validate Vehicle Identification Numbers (VINs). | |
## | |
use boolean; | |
use Readonly; | |
Readonly::Hash my %CHAR_VALUES => ( | |
A => 1, | |
B => 2, | |
C => 3, | |
D => 4, | |
E => 5, | |
F => 6, | |
G => 7, | |
H => 8, | |
J => 1, | |
K => 2, | |
L => 3, | |
M => 4, | |
N => 5, | |
P => 7, | |
R => 9, | |
S => 2, | |
T => 3, | |
U => 4, | |
V => 5, | |
W => 6, | |
X => 7, | |
Z => 9, | |
1 => 1, | |
2 => 2, | |
3 => 3, | |
4 => 4, | |
5 => 5, | |
6 => 6, | |
7 => 7, | |
8 => 8, | |
9 => 9, | |
0 => 0, | |
); | |
Readonly::Array my @WEIGHTS => (8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2); | |
Readonly::Array my @CHECK_DIGITS => (0 .. 9, "X"); | |
sub is_valid{ | |
my ($vin) = @_; | |
$vin = uc($vin); | |
unless($vin =~ m/^[A-H J-N P R-Z 0-9]{9}[A-H J-N P R-T V-Y 1-9]{1}[A-H J-N P R-Z 0-9]{7}$/){ | |
return false; | |
} | |
else{ | |
my @vin = split(//, $vin); | |
my $checksum = 0; | |
my @values = map { $CHAR_VALUES{$_} } @vin; | |
for(my $i = 0; $i < @values; $i++){ | |
$checksum = ($checksum + ($values[$i] * $WEIGHTS[$i])); | |
} | |
my $check_digit = $checksum % 11; | |
return true if $vin[8] eq $CHECK_DIGITS[$check_digit]; | |
} | |
return false; | |
} | |
MAIN:{ | |
print is_valid($ARGV[0]); | |
print "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Write a program to solve the Knapsack Problem. | |
## | |
use Readonly; | |
Readonly::Array my @weights => (1, 1, 2, 12, 4); | |
Readonly::Array my @values => (1, 2, 2, 4, 10); | |
sub knapsack{ | |
my($capacity, $n) = @_; | |
return 0 if($n == 0 || $capacity == 0); | |
return knapsack($capacity, $n - 1) if ($weights[$n - 1] > $capacity); | |
my $a = knapsack($capacity - $weights[$n - 1], $n - 1) + $values[$n - 1]; | |
my $b = knapsack($capacity, $n - 1); | |
return $a > $b ? $a : $b; | |
} | |
MAIN:{ | |
my $max_capacity = 15; | |
print knapsack($max_capacity, scalar(@weights)); | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment