diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-04-22 00:16:51 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-04-22 00:16:51 +0100 |
| commit | 07a34244668223bd9ed9f27cadd23769dab03188 (patch) | |
| tree | 28ff4018c392de389f996b83e7c814b96063e474 /challenge-161 | |
| parent | dd40b9c0b1ef7d5feeab586ddc59aa6b449a45a7 (diff) | |
| download | perlweeklychallenge-club-07a34244668223bd9ed9f27cadd23769dab03188.tar.gz perlweeklychallenge-club-07a34244668223bd9ed9f27cadd23769dab03188.tar.bz2 perlweeklychallenge-club-07a34244668223bd9ed9f27cadd23769dab03188.zip | |
some other random methods
Diffstat (limited to 'challenge-161')
| -rw-r--r-- | challenge-161/james-smith/perl/ch-2.pl | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/challenge-161/james-smith/perl/ch-2.pl b/challenge-161/james-smith/perl/ch-2.pl index a42de25ca6..db30ad599f 100644 --- a/challenge-161/james-smith/perl/ch-2.pl +++ b/challenge-161/james-smith/perl/ch-2.pl @@ -19,6 +19,7 @@ O: for my $w (@words) { push @abcde,$w; } + say "\nML:"; say join ' ', ' * [W]', most_letters( \@words ); say join ' ', ' * [A]', most_letters( \@abcde ); @@ -38,8 +39,9 @@ say join ' ', ' * [W]', generate_a_pangram_alpha_order( \@words ); say "\nOne letter at a time (alpha):"; say join ' ', ' * [W]', generate_one_letter_at_time( \@words ); +say join ' ', ' * [W]', generate_one_letter_at_time_longest( \@words ); + -say "\n"; sub generate_a_pangram_random { my ($list,$c,%letters,@pangram) = (shift,0,map { $_ => 0 } 'a'..'z'); @@ -102,6 +104,21 @@ sub generate_one_letter_at_time { @pangram; } +sub generate_one_letter_at_time_longest { + my ($list,$next,%letters,@pangram) = (shift,'a',map { $_ => 0 } 'a'..'z'); + O: until( 'aa' eq $next ) { + my($best,$best_length,$best_word) = (0,0,''); + W: foreach my $word ( @{$list} ) { + my %t = map { $_=>1 } split //, $word; + my @T = grep { $_ ge $next } keys %t; + ($best_word,$best_length) = ($word,length $word) if @T == 1 && $T[0] eq $next && $best_length < length $word; + } + push @pangram, $best_word; + $next++; + } + @pangram; +} + sub most_letters { my ($list,$c,%letters,@pangram) = (shift,0,map{$_=>1}'a'..'z'); while($c<26) { |
