diff options
| author | Simon Green <mail@simon.green> | 2021-10-23 23:41:25 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2021-10-23 23:41:25 +1100 |
| commit | 71118e242f42216c97d237d124c2c2094aba9011 (patch) | |
| tree | 5255eafc5cd22e126f5e391b2bde892f64ea7e0d | |
| parent | d7c456153e7bc36721a4fa5ea00d7cf16b7fa807 (diff) | |
| download | perlweeklychallenge-club-71118e242f42216c97d237d124c2c2094aba9011.tar.gz perlweeklychallenge-club-71118e242f42216c97d237d124c2c2094aba9011.tar.bz2 perlweeklychallenge-club-71118e242f42216c97d237d124c2c2094aba9011.zip | |
sgreen solutions to challenge 135
| -rw-r--r-- | challenge-135/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-135/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-135/sgreen/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-135/sgreen/perl/ch-2.pl | 41 |
4 files changed, 68 insertions, 2 deletions
diff --git a/challenge-135/sgreen/README.md b/challenge-135/sgreen/README.md index 298af3387e..12939a8bf5 100644 --- a/challenge-135/sgreen/README.md +++ b/challenge-135/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 133 +# The Weekly Challenge 135 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-133-7gi) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-135-g0o) diff --git a/challenge-135/sgreen/blog.txt b/challenge-135/sgreen/blog.txt new file mode 100644 index 0000000000..5f1dbb024e --- /dev/null +++ b/challenge-135/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-135-g0o diff --git a/challenge-135/sgreen/perl/ch-1.pl b/challenge-135/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e667b28205 --- /dev/null +++ b/challenge-135/sgreen/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $n = shift; + + # Check the input is valid + die "You must enter an integer\n" unless $n; + die "The input does not look like an integer\n" unless $n =~ /^-?[1-9][0-9]*$/; + + # If it's a negative number, treat it like a positive from now onwards + $n = abs($n); + my $l = length( $n // '' ); + die "even numbers of digits\n" unless $l % 2 == 1; + die "too short\n" unless $l >= 3; + + # Display the middle three digits + say substr( $n, ( $l - 3 ) / 2, 3 ); +} + +main(@ARGV); diff --git a/challenge-135/sgreen/perl/ch-2.pl b/challenge-135/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1b0e850aeb --- /dev/null +++ b/challenge-135/sgreen/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _is_sedol { + my $input = shift; + my @weight = qw(1 3 1 7 3 9 1); + + # Check that valid characters are used. The first six + # characters are letters (not vowels) or numbers. The last + # character must be a digit. + return 0 unless $input =~ /^[B-DF-HJ-NP-TV-Z0-9]{6}[0-9]$/; + + # Start with the last digit + my $sum = substr( $input, 6 ); + for my $i ( 0 .. 5 ) { + # Get the character, and work out its value. + my $c = substr( $input, $i, 1 ); + my $v = $c =~ /[0-9]/ ? $c : ord($c) - 55; + + # Add the value to the sum multiplied by the weight + $sum += $v * $weight[$i]; + } + + # This is a SEDOL value if it ends in a zero + return $sum % 10 ? 0 : 1; +} + +sub main { + my $input = shift; + + # Sanity check + die "You must enter a value\n" unless defined $input; + die "The value must be seven characters\n" unless length($input) == 7; + + say _is_sedol($input); +} + +main(@ARGV); |
