blob: fd72cb47e8a1e37c88768614a4e65717fb94b25a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#!/usr/bin/perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-135/
# Task #2 > Validate SEDOL
#
# You are given 7-characters alphanumeric SEDOL.
#
# Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL otherwise 0.
#
# For more information about SEDOL, please checkout the wikipedia page (https://en.wikipedia.org/wiki/SEDOL)
#
# Example 1
# Input: $SEDOL = '2936921'
# Output: 1
# Example 2
# Input: $SEDOL = '1234567'
# Output: 0
# Example 3
# Input: $SEDOL = 'B0YBKL9'
# Output: 1
use strict;
use warnings;
use feature 'say';
my $sedol = shift;
say is_valid($sedol);
exit 0;
sub is_valid {
my $sedol = shift;
return 0 if !$sedol || $sedol !~ /^([0-9B-DF-HJ-NP-TV-Z]{6})([0-9])$/;
my $code = $1;
my $check = $2;
my $sum = 0;
for (0 .. 5) {
my $v = substr $code, $_, 1; # value for numbers
$v = ord($v) - 55 if $v !~ /\d/; # value for letters (alphabet position)
$sum += $v * (1,3,1,7,3,9)[$_]; # weighted sum of values
}
return $check == (10 - $sum % 10) % 10 ? 1 : 0;
}
__END__
./ch-2.pl 2936921
1
./ch-2.pl 1234567
0
./ch-2.pl B0YBKL9
1
|