diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-23 14:13:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-23 14:13:39 +0100 |
| commit | d926c207ce8f539825cee5f55f9ddfeef6af71a6 (patch) | |
| tree | f5ba1c8505bb9e63314d52b702cbb9261c68f418 | |
| parent | 8c8b12e5cc0941bff65c25bbe6328e965c8a4bd8 (diff) | |
| parent | 71118e242f42216c97d237d124c2c2094aba9011 (diff) | |
| download | perlweeklychallenge-club-d926c207ce8f539825cee5f55f9ddfeef6af71a6.tar.gz perlweeklychallenge-club-d926c207ce8f539825cee5f55f9ddfeef6af71a6.tar.bz2 perlweeklychallenge-club-d926c207ce8f539825cee5f55f9ddfeef6af71a6.zip | |
Merge pull request #5082 from simongreen-net/swg-135
sgreen solutions to challenge 135
| -rw-r--r-- | challenge-134/sgreen/README.md | 4 | ||||
| -rwxr-xr-x | challenge-134/sgreen/perl/ch-2.pl | 34 | ||||
| -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 |
6 files changed, 104 insertions, 4 deletions
diff --git a/challenge-134/sgreen/README.md b/challenge-134/sgreen/README.md index 298af3387e..223b623bcc 100644 --- a/challenge-134/sgreen/README.md +++ b/challenge-134/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 133 +# The Weekly Challenge 134 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-133-7gi) +Solution by Simon Green. diff --git a/challenge-134/sgreen/perl/ch-2.pl b/challenge-134/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..e23cb45068 --- /dev/null +++ b/challenge-134/sgreen/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my ($m, $n) = @_; + + # Sanity check + die "You must provide two values\n" unless defined $m and defined $n; + die "The first value is not a positive integer" unless $m =~ /^[1-9][0-9]*$/; + die "The second value is not a positive integer" unless $n =~ /^[1-9][0-9]*$/; + + # Show the multiplication table + my %seen = (); + my $length = length($m * $n); + my $format = "%${length}s |" . ( " %${length}s" x $n) ."\n"; + printf $format, 'x', 1 .. $n; + say '-' x ($length+1), '+', '-' x (($length+1)*$n); + foreach my $o (1 .. $m) { + my @row = map { $_ * $o } 1..$n; + $seen{$_} = 1 foreach @row; + printf $format, $o, @row; + } + + my @numbers = sort { $a <=> $b } keys %seen; + say ''; + say 'Distinct Terms: ', join ', ', @numbers; + say "Count: ", scalar(@numbers); + +} + +main(@ARGV); 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); |
