From c7cd0c401f1ebef9b09e1a4e64cfc5b0b2adaf2c Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 27 Aug 2020 01:29:13 +0100 Subject: - Tidied up contributions by James Smith. --- challenge-075/james-smith/ch-1.pl | 29 ----------------------------- challenge-075/james-smith/ch-2.pl | 34 ---------------------------------- challenge-075/james-smith/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-075/james-smith/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 challenge-075/james-smith/ch-1.pl delete mode 100644 challenge-075/james-smith/ch-2.pl create mode 100644 challenge-075/james-smith/perl/ch-1.pl create mode 100644 challenge-075/james-smith/perl/ch-2.pl (limited to 'challenge-075') diff --git a/challenge-075/james-smith/ch-1.pl b/challenge-075/james-smith/ch-1.pl deleted file mode 100644 index 08d9c19862..0000000000 --- a/challenge-075/james-smith/ch-1.pl +++ /dev/null @@ -1,29 +0,0 @@ -use strict; -use warnings; -use feature qw(say); - -my %mem; ## Used to cache results of CSM to speed things up for large cases... - -## Some test cases - example from challenge itself -say q(); -_dump( csm( 6, qw(1 2 4) ) ); -say q(); - -## All the values up to £2 - using standard UK coins -foreach (1..200) { - _dump( csm( $_, qw(1 2 5 10 20 50 100 200) ) ); - say q(); -} - -# The hardwork - use recursion -sub csm { - my $t = shift; - return @{$mem{"$t @_"}||=[map {my $a=$_; $t==$a?[$a]: - map {[$a,@{$_}]} csm($t-$a,grep {$a<=$_&&$_<=$t} @_)} @_] }; -} - -## Support function to dump values; -sub _dump { - say " @{$_}" foreach @_; -} - diff --git a/challenge-075/james-smith/ch-2.pl b/challenge-075/james-smith/ch-2.pl deleted file mode 100644 index 16bca58aa0..0000000000 --- a/challenge-075/james-smith/ch-2.pl +++ /dev/null @@ -1,34 +0,0 @@ -use strict; -use warnings; -use feature qw(say); - -say q(); -say lrh(qw(2 1 4 5 3 7)); -say q(); -say lrh(qw(3 2 3 5 7 5)); -say q(); - -## Could import max/min - but these are easy to write ourselves in this case.... - -sub lrh { - my @V = @_; - my ($mx,$y) = 0; - $mx = $mx < $_ ? $_ : $mx foreach @V; -## The following chunk renders the histogram as requested... -## Render output as table.... - say sprintf( ' %2d', $y=$_ ), map { $_ < $y ? q( ) : q( #) } @V foreach reverse 1..$mx; - say q( --), map { q( --) } @V; - say q( ), map { sprintf ' %2d', $_ } @V; - say q(); - -## Now do the calculation of mx area - my $mx_area = 0; - foreach my $s ( 0 .. @V-1 ) { ## Loop through each start of block... - my $mn = $mx; - foreach ( $s .. @V-1 ) { - $mn = $V[$_] if $mn > $V[$_]; ## Loop through ends, keeping track of minimum value - $mx_area = $mn * ($_-$s+1) if $mx_area < $mn * ($_-$s+1); ## And check to see if the area is greater than any other area! - } - } - return $mx_area; -} diff --git a/challenge-075/james-smith/perl/ch-1.pl b/challenge-075/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..08d9c19862 --- /dev/null +++ b/challenge-075/james-smith/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +use feature qw(say); + +my %mem; ## Used to cache results of CSM to speed things up for large cases... + +## Some test cases - example from challenge itself +say q(); +_dump( csm( 6, qw(1 2 4) ) ); +say q(); + +## All the values up to £2 - using standard UK coins +foreach (1..200) { + _dump( csm( $_, qw(1 2 5 10 20 50 100 200) ) ); + say q(); +} + +# The hardwork - use recursion +sub csm { + my $t = shift; + return @{$mem{"$t @_"}||=[map {my $a=$_; $t==$a?[$a]: + map {[$a,@{$_}]} csm($t-$a,grep {$a<=$_&&$_<=$t} @_)} @_] }; +} + +## Support function to dump values; +sub _dump { + say " @{$_}" foreach @_; +} + diff --git a/challenge-075/james-smith/perl/ch-2.pl b/challenge-075/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..16bca58aa0 --- /dev/null +++ b/challenge-075/james-smith/perl/ch-2.pl @@ -0,0 +1,34 @@ +use strict; +use warnings; +use feature qw(say); + +say q(); +say lrh(qw(2 1 4 5 3 7)); +say q(); +say lrh(qw(3 2 3 5 7 5)); +say q(); + +## Could import max/min - but these are easy to write ourselves in this case.... + +sub lrh { + my @V = @_; + my ($mx,$y) = 0; + $mx = $mx < $_ ? $_ : $mx foreach @V; +## The following chunk renders the histogram as requested... +## Render output as table.... + say sprintf( ' %2d', $y=$_ ), map { $_ < $y ? q( ) : q( #) } @V foreach reverse 1..$mx; + say q( --), map { q( --) } @V; + say q( ), map { sprintf ' %2d', $_ } @V; + say q(); + +## Now do the calculation of mx area + my $mx_area = 0; + foreach my $s ( 0 .. @V-1 ) { ## Loop through each start of block... + my $mn = $mx; + foreach ( $s .. @V-1 ) { + $mn = $V[$_] if $mn > $V[$_]; ## Loop through ends, keeping track of minimum value + $mx_area = $mn * ($_-$s+1) if $mx_area < $mn * ($_-$s+1); ## And check to see if the area is greater than any other area! + } + } + return $mx_area; +} -- cgit