From d7c456153e7bc36721a4fa5ea00d7cf16b7fa807 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sat, 23 Oct 2021 21:28:14 +1100 Subject: Belated commit to challenge 134 --- challenge-134/sgreen/README.md | 4 ++-- challenge-134/sgreen/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100755 challenge-134/sgreen/perl/ch-2.pl 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); -- cgit From 71118e242f42216c97d237d124c2c2094aba9011 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sat, 23 Oct 2021 23:41:25 +1100 Subject: sgreen solutions to challenge 135 --- challenge-135/sgreen/README.md | 4 ++-- challenge-135/sgreen/blog.txt | 1 + challenge-135/sgreen/perl/ch-1.pl | 24 +++++++++++++++++++++++ challenge-135/sgreen/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 challenge-135/sgreen/blog.txt create mode 100755 challenge-135/sgreen/perl/ch-1.pl create mode 100755 challenge-135/sgreen/perl/ch-2.pl 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); -- cgit