diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-22 06:56:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-22 06:56:04 +0000 |
| commit | be3c5cd158fd2942c22aaae29f3497f1aa1fa829 (patch) | |
| tree | 682f722d91c1f58be3986578bcf179fb10b2f87d | |
| parent | 91d268386983bd95083b0874856463d187369a61 (diff) | |
| parent | f385d2b3e145123490f304a197b741d4ee20830a (diff) | |
| download | perlweeklychallenge-club-be3c5cd158fd2942c22aaae29f3497f1aa1fa829.tar.gz perlweeklychallenge-club-be3c5cd158fd2942c22aaae29f3497f1aa1fa829.tar.bz2 perlweeklychallenge-club-be3c5cd158fd2942c22aaae29f3497f1aa1fa829.zip | |
Merge pull request #3033 from drbaggy/master
first commit
| -rw-r--r-- | challenge-092/james-smith/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-092/james-smith/perl/ch-2.pl | 44 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-092/james-smith/perl/ch-1.pl b/challenge-092/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..b4fc12b5e3 --- /dev/null +++ b/challenge-092/james-smith/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( iso('abc','xyz'), 1 ); +is( iso('abb','xyy'), 1 ); +is( iso('sum','add'), 0 ); +is( iso('add','sum'), 0 ); + +done_testing( ); + +sub iso { + my ($a,$b) = @_; + my %x; + my %y; + return 0 unless length $a == length $b; + my @b = split m{}, $b; + foreach ( split m{}, $a ) { + my $t = shift @b; + if( exists $x{$_} ) { ## Have we already got a map from 1st letter? + return 0 unless $x{$_} eq $t; ## Yes but it doesn't match return... + } else { + $x{$_} = $t; + return 0 if exists $y{$t} && $y{$t} ne $_; + } + $y{$t} = $_; + } + return 1; +} + diff --git a/challenge-092/james-smith/perl/ch-2.pl b/challenge-092/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..af77886e72 --- /dev/null +++ b/challenge-092/james-smith/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is_deeply( int_insert( [2,6], [1,4], [8,10] ), [[1,6],[8,10]] ); ## Simple overlap +is_deeply( int_insert( [2,6], [1,7], [8,10] ), [[1,7],[8,10]] ); ## New entry entirely in one region +is_deeply( int_insert( [1,7], [2,6], [8,10] ), [[1,7],[8,10]] ); ## New entry engulfs one region +is_deeply( int_insert( [1,7], [2,3], [4,5], [8,10] ), [[1,7],[8,10]] ); ## New entry engulfs 2 regions +is_deeply( int_insert( [2,8], [1,2], [3,4], [5,6], [7,9] ), [[1,9]] ); ## Merges multiple regions +is_deeply( int_insert( [5,8], [1,2], [3,7], [8,10] ), [[1,2],[3,10]] ); ## Merges two groups +is_deeply( int_insert( [10,11], [1,5],[7,9] ), [[1,5],[7,9],[10,11]] ); ## New entry at end +is_deeply( int_insert( [5,6], [1,2],[7,9] ), [[1,2],[5,6],[7,9]] ); ## New entry in middle +is_deeply( int_insert( [1,2], [5,6],[7,9] ), [[1,2],[5,6],[7,9]] ); ## New entry at start +is_deeply( int_insert( [1,1e7], map { [2*$_,2*$_+1] } 1..4_999_999 ), [[1,1e7]] ); ## Check it isn't really slow! + +done_testing( ); + +sub int_insert { + my( $new, @list ) = @_; + my @new_list; + while(my $e = shift @list) { + return [ @new_list, $new, $e, @list ] if $e->[0] > $new->[1]; + ## The start of the next element is after the end of the new element - so we can safely + ## push the new element and the rest of the list (and return it) + if( $e->[1] < $new->[0] ) { ## Next element is to the left of the new element so push + push @new_list,$e; ## and continue + next; + } + $new->[0] = $e->[0] if $e->[0] < $new->[0]; + $new->[1] = $e->[1] if $e->[1] > $new->[1]; ## Get start/end of first overlap... + while( @list && $new->[1] >= $list[0][0] ) { ## this also overlaps the next list element + $new->[1] = $list[0][1] if $list[0][1] > $new->[1]; ## If goes beyond next list element we extend new + shift @list; ## Remove element from list; + } + return [ @new_list, $new, @list ]; ## The rest of the list will be after the "new" element now + } ## So we can safely push and return it.... + return [ @new_list, $new ]; ## The new element must be after the list so we just + ## return it on the end of the list... +} + |
