diff options
| author | drbaggy <js5@sanger.ac.uk> | 2020-10-05 13:29:28 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2020-10-05 13:29:28 +0100 |
| commit | 1248dcbf7f41dffca0fd8438791749f556101a12 (patch) | |
| tree | 35976f8682d3612548599fa0d97785d1de8ed8f2 /challenge-081/james-smith/perl | |
| parent | cef248ba491398a30061ba49fbc2a824116ae996 (diff) | |
| download | perlweeklychallenge-club-1248dcbf7f41dffca0fd8438791749f556101a12.tar.gz perlweeklychallenge-club-1248dcbf7f41dffca0fd8438791749f556101a12.tar.bz2 perlweeklychallenge-club-1248dcbf7f41dffca0fd8438791749f556101a12.zip | |
solution
Diffstat (limited to 'challenge-081/james-smith/perl')
| -rw-r--r-- | challenge-081/james-smith/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-081/james-smith/perl/ch-2.pl | 36 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-081/james-smith/perl/ch-1.pl b/challenge-081/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..27fdb24971 --- /dev/null +++ b/challenge-081/james-smith/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use feature qw(say); + +use Test::More; + +## Run tests.... + +is( "@{common_base_string( qw(abcdabcd abcdabcdabcdabcd) )}", 'abcd abcdabcd' ); +is( "@{common_base_string( qw(aaa aa) ) }", 'a' ); +is( "@{common_base_string( 'abcd'x30, 'abcd'x12 ) }", 'abcd abcdabcd abcdabcdabcd abcdabcdabcdabcdabcdabcd' ); +is( "@{common_base_string( qw(abcd ef) ) }", '' ); + +done_testing; + +sub common_base_string { + my( $s, $t ) = @_; + my $ls = length $s; ## need lengths many times so we get them + my $lt = length $t; + return [ map { substr $s,0,$_ } + grep { my $x = substr $s,0,$_; + !($ls % $_) && ## false unless length of + !($lt % $_) && ## both strings a multiple of $_ + $s eq ($x x ($ls/$_)) && ## check to see if both + $t eq ($x x ($lt/$_)) ## strings fit requirement! + } + 1 .. ($ls<$lt?$ls:$lt) ]; +} + + + + diff --git a/challenge-081/james-smith/perl/ch-2.pl b/challenge-081/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..bb1d5f40bc --- /dev/null +++ b/challenge-081/james-smith/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +my $string = q( +The award-winning adaptation of the classic romantic tragedy "Romeo and Juliet". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff's best friend (and former Jet) Tony and Bernardo's younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what's happened, tragedy strikes and doesn't stop until the climactic and heartbreaking ending. +); + +print_words( get_words( $string )); + +sub get_words { + my $string = shift; + my $words = {}; + foreach ( grep {$_ }map { s{([."\(\),]|--|'s$)}{}msgr } $string =~ m{(\S+)}mxg ) { + $words->{lc $_} ||= [ $_, 0 ]; + $words->{lc $_}[1]++; + } + return $words; +} + +sub print_words { + my $struct = shift; + my @words; + ## Use array rather than hash as avoids one of the two sorts and + ## shouldn't be too sparse + push @{ $words[$_->[1]] }, $_->[0] foreach (values %{$struct}); + foreach ( 0..(@words-1) ) { + next unless $words[$_]; + print "$_ @{[ sort { lc $a cmp lc $b } @{$words[$_]} ]}\n"; + } +} + + + + |
