aboutsummaryrefslogtreecommitdiff
path: root/challenge-087
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2020-11-25 09:31:47 +0000
committerdrbaggy <js5@sanger.ac.uk>2020-11-25 09:31:47 +0000
commitee4ca50a9ae8434166c8de2eea5121e58ea7d7db (patch)
tree9cc2d364899dcf419bbcabf9a1272160aae08de5 /challenge-087
parent783f4bed20188b8713c558e0e501d037a90f58af (diff)
downloadperlweeklychallenge-club-ee4ca50a9ae8434166c8de2eea5121e58ea7d7db.tar.gz
perlweeklychallenge-club-ee4ca50a9ae8434166c8de2eea5121e58ea7d7db.tar.bz2
perlweeklychallenge-club-ee4ca50a9ae8434166c8de2eea5121e58ea7d7db.zip
adding files
Diffstat (limited to 'challenge-087')
-rw-r--r--challenge-087/james-smith/perl/ch-1.pl45
-rw-r--r--challenge-087/james-smith/perl/ch-2.pl31
2 files changed, 69 insertions, 7 deletions
diff --git a/challenge-087/james-smith/perl/ch-1.pl b/challenge-087/james-smith/perl/ch-1.pl
index 930d91cc65..06d1180f84 100644
--- a/challenge-087/james-smith/perl/ch-1.pl
+++ b/challenge-087/james-smith/perl/ch-1.pl
@@ -5,12 +5,51 @@ use strict;
use warnings;
use feature qw(say);
use Test::More;
+use Data::Dumper qw(Dumper);
-is( my_function(), 1 );
+is( display_lcf( lcf(qw(100 4 50 3 2)) ), '(2, 3, 4)' );
+is( display_lcf( lcf(qw(20 30 10 40 50)) ), '0' );
+is( display_lcf( lcf(qw(20 19 9 11 10)) ), '(9, 10, 11)' );
+is( display_lcf( lcf(reverse 1..100) ), display_lcf(1..100) );
done_testing();
-sub my_function {
- return 1;
+sub display_lcf {
+ return @_ ? sprintf '(%s)', join q(, ), @_ : 0;
+}
+
+sub lcf {
+ my @n = @_;
+ my %seq;
+ ## Start by collecting together those numbers which
+ ## match the criteria of both value and value+1 are
+ ## in array...
+ foreach my $a (@n) {
+ $seq{$a} = $a+1 if grep {$_==$a+1} @n;
+ }
+ ## Now we are going to collapse the structure;
+ ## $seq{$_} exists in %seq we remove it and update
+ ## the value of $seq{$_} to that value.
+ ## (delete removes element from hash - return value
+ ## is element removed)
+ my $flag = 1;
+ while($flag) {
+ $flag = 0;
+ foreach (keys %seq) {
+ next unless exists $seq{$_} && exists $seq{$seq{$_}};
+ $seq{$_} = delete $seq{$seq{$_}};
+ $flag = 1;
+ }
+ }
+ ## Now we look for the longest sequence
+ ## (note we only return the first sequence of a given
+ ## length we find)
+ my $k = undef;
+ foreach ( keys %seq ) {
+ $k = $_ if !defined $k || $seq{$_}-$_ > $seq{$k}-$k;
+ }
+ ## Return it if there is a longest sequence.
+ return unless defined $k;
+ return $k..$seq{$k};
}
diff --git a/challenge-087/james-smith/perl/ch-2.pl b/challenge-087/james-smith/perl/ch-2.pl
index 930d91cc65..b6dac95cef 100644
--- a/challenge-087/james-smith/perl/ch-2.pl
+++ b/challenge-087/james-smith/perl/ch-2.pl
@@ -3,14 +3,37 @@
use strict;
use warnings;
-use feature qw(say);
+use featureqw(say);
use Test::More;
-is( my_function(), 1 );
+is( display_rect( largest_rect( [qw(0 0 0 1 0 0)], [qw(1 1 1 0 0 0)], [qw(0 0 1 0 0 1)], [qw(1 1 1 1 1 0)], [qw(1 1 1 1 1 0)] ) ),
+ display_rect( [qw(1 1 1 1 1)], [qw(1 1 1 1 1)] ) );
+is( display_rect( largest_rect( [qw(1 0 1 0 1 0)], [qw(0 1 0 1 0 1)], [qw(1 0 1 0 1 0)], [qw(0 1 0 1 0 1)] ) ), 0 );
+is( display_rect( largest_Rect( [qw(0 0 0 1 1 1)], [qw(1 1 1 1 1 1)], [qw(0 0 1 0 0 1)], [qw(0 0 1 1 1 1)], [qw(0 0 1 1 1 1)] )),
+ display_rect( [qw(1 1 1 1)], [qw(1 1 1 1)] ) );
done_testing();
-sub my_function {
- return 1;
+sub display_rect {
+ my @h;
+ return 0 unless @h;
+ return join "\n", map { "[ @{$_} ]" } @h;
+}
+sub largest_rect {
+ my @grid = @_;
+ my $h = @grid;
+ my $w = @{$grid[0]});
+ my ($m_h,$m_w) = (0,0);
+ foreach my $l (0..($w-1)) {
+ OUTER: foreach my $t (0..($h-1)) {
+ my $flag = 1;
+ foreach my $r ($l+1..($w-1)) {
+ foreach my $b ($l+1..($w-1)) {
+ next OUTER unless $grid[$r][$b];
+ }
+ }
+ if( $
+ }
+ }
}