diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-08-23 08:09:31 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-08-23 08:09:31 +0100 |
| commit | 07859bae61bbc1cef7e10789b33d4ba30baf842e (patch) | |
| tree | f6a5889954a07f79aaa7eed202711a4ef5af03ec /challenge-127/james-smith | |
| parent | 8dc9584bd975421f7428575befab2a28c16aef7b (diff) | |
| download | perlweeklychallenge-club-07859bae61bbc1cef7e10789b33d4ba30baf842e.tar.gz perlweeklychallenge-club-07859bae61bbc1cef7e10789b33d4ba30baf842e.tar.bz2 perlweeklychallenge-club-07859bae61bbc1cef7e10789b33d4ba30baf842e.zip | |
add compact version
Diffstat (limited to 'challenge-127/james-smith')
| -rw-r--r-- | challenge-127/james-smith/perl/ch-2.pl | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/challenge-127/james-smith/perl/ch-2.pl b/challenge-127/james-smith/perl/ch-2.pl index 279dfc2cf4..50080b352b 100644 --- a/challenge-127/james-smith/perl/ch-2.pl +++ b/challenge-127/james-smith/perl/ch-2.pl @@ -17,6 +17,7 @@ my @TESTS = ( #done_testing(); print_res( conflict_intervals( $_ ) ) foreach @TESTS; +print_res( conflict_intervals_compact( $_ ) ) foreach @TESTS; sub print_res { printf "[ %s ]\n", join ', ', map { "($_->[0],$_->[1])" } @{$_[0]}; @@ -25,8 +26,7 @@ sub print_res { sub conflict_intervals { my @in = @{ $_[0] }; my @conf; - while(@in) { - my $int = pop @in; + while( my $int = pop @in ) { foreach(@in) { next unless $int->[1] < $_->[0] || $int->[0] < $_->[1]; unshift @conf, $int; @@ -36,3 +36,11 @@ sub conflict_intervals { return \@conf; } +sub conflict_intervals_compact { + my(@in,@conf) = @{$_[0]}; + while(my $int = pop @in) { + ($int->[1] < $_->[0] || $int->[0] < $_->[1]) && (unshift @conf, $int) && last foreach @in; + } + return \@conf; +} + |
