aboutsummaryrefslogtreecommitdiff
path: root/challenge-152
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-18 10:39:03 +0000
committerGitHub <noreply@github.com>2022-02-18 10:39:03 +0000
commit3acd29f6195bfb005f78108c56423a018f2d44c4 (patch)
treec50bb8f520847cbb5a8f29097e5341340eaa2221 /challenge-152
parent4e5406f39834b68dbeb125f3ecd16c10f5b77271 (diff)
parent978a125c86a7391ba79621e8ae981f8a87645133 (diff)
downloadperlweeklychallenge-club-3acd29f6195bfb005f78108c56423a018f2d44c4.tar.gz
perlweeklychallenge-club-3acd29f6195bfb005f78108c56423a018f2d44c4.tar.bz2
perlweeklychallenge-club-3acd29f6195bfb005f78108c56423a018f2d44c4.zip
Merge pull request #5669 from drbaggy/master
Updated - added one liner
Diffstat (limited to 'challenge-152')
-rw-r--r--challenge-152/james-smith/README.md19
-rw-r--r--challenge-152/james-smith/perl/ch-1.pl46
2 files changed, 55 insertions, 10 deletions
diff --git a/challenge-152/james-smith/README.md b/challenge-152/james-smith/README.md
index 8c7c813547..9e00afbeab 100644
--- a/challenge-152/james-smith/README.md
+++ b/challenge-152/james-smith/README.md
@@ -81,6 +81,25 @@ sub min_path_anydir_total {
$res;
}
```
+### Solution b - version 2... a 1-liner.
+
+To reduce the number of lines we can replace the minimum with a sort and take lowest value using `[sort {$a<=>$b} @{$_}]->[0]` this gives the following two functions...
+
+```perl
+sub min_path_anydir_sort {
+ my($res,@order)= 0;
+ (push @order, [sort {$a<=>$b} @{$_}]->[0]), $res+=$order[-1] for @{$_[0]};
+ say sprintf 'Minimum value %d: [ %s ]', $res, join ', ', @order;
+ $res;
+}
+
+sub min_path_anydir_stot {
+ my $res=0;
+ $res += [sort {$a<=>$b} @{$_}]->[0] for @{$_[0]};
+ $res;
+}
+
+```
# Challange 2 - Rectangle Area
***You are given coordinates bottom-left and top-right corner of two rectangles in a 2D plane. Write a script to find the total area covered by the two rectangles.***
diff --git a/challenge-152/james-smith/perl/ch-1.pl b/challenge-152/james-smith/perl/ch-1.pl
index 8b013c4ca8..bf931d357f 100644
--- a/challenge-152/james-smith/perl/ch-1.pl
+++ b/challenge-152/james-smith/perl/ch-1.pl
@@ -14,27 +14,32 @@ my @TESTS = (
);
is( min_path( $_->[0] ), $_->[1] ) foreach @TESTS;
-is( min_path_anydir( $_->[0] ), $_->[2] ) foreach @TESTS;
is( min_path_total( $_->[0] ), $_->[1] ) foreach @TESTS;
+
+is( min_path_anydir( $_->[0] ), $_->[2] ) foreach @TESTS;
+is( min_path_anydir_sort( $_->[0] ), $_->[2] ) foreach @TESTS;
is( min_path_anydir_total( $_->[0] ), $_->[2] ) foreach @TESTS;
+is( min_path_anydir_stot( $_->[0] ), $_->[2] ) foreach @TESTS;
done_testing();
+## For each entry - we store tuple [ total, "path" ].
+## We create a "row" below the triangle with "0"s and empty paths...
+## We work back up the tree, and for each cell in the row we
+## add the left or right sub-tree depending on which has the lowest
+## value...
+
sub min_path {
- my @p = map { [0,[]] } 0,my @t = reverse @{$_[0]};
- @p = map {
- $p[0][0] < $p[1][0]
- ? [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ]
- : [ $_+$p[1][0], [ $_, @{$p[1][1]} ] ],
- (shift @p) x 0
- } @{$_} for @t;
+ my @p = ( [0,[]] ) x (1 + @{$_[0]});
+ @p = map { $p[0][0] < $p[1][0] ? [ $_+$p[0][0], [ $_, @{$p[0][1]} ] ] : [ $_+$p[1][0], [ $_, @{$p[1][1]} ] ], (shift @p) x 0 } @{$_} for reverse @{$_[0]};
say sprintf 'Minimum value %d: [ %s ]', $p[0][0], join ', ', @{$p[0][1]};
$p[0][0];
}
+## Without he tuple containing the total/sequence
sub min_path_total {
- my @p = map { 0 } 0, my @t = reverse @{$_[0]};
- @p = map { $_ + $p[ $p[0] < $p[1] ? 0 : 1 ], (shift @p)x 0 } @{$_} for @t;
+ my @p = (0) x (1+@{$_[0]});
+ @p = map { $_ + $p[$p[0]<$p[1]?0:1], (shift @p)x 0 } @{$_} for reverse @{$_[0]};
$p[0];
}
@@ -50,6 +55,17 @@ sub min_path_anydir {
$res;
}
+## Cheeky "one-liner" version - use sort instead of for loop....
+##
+## After we have pushed the value we then update the total {which is the last element of order}
+
+sub min_path_anydir_sort {
+ my($res,@order)= 0;
+ (push @order, [sort {$a<=>$b} @{$_}]->[0]), $res+=$order[-1] for @{$_[0]};
+ say sprintf 'Minimum value %d: [ %s ]', $res, join ', ', @order;
+ $res;
+}
+
sub min_path_anydir_total {
my $res = 0;
foreach(@{$_[0]}) {
@@ -59,3 +75,13 @@ sub min_path_anydir_total {
}
$res;
}
+
+## Simpler version of the sort method above which only does total..
+
+sub min_path_anydir_stot {
+ my $res=0;
+ $res += [sort {$a<=>$b} @{$_}]->[0] for @{$_[0]};
+ $res;
+}
+
+