diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-10-01 23:02:02 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-10-01 23:02:02 +0200 |
| commit | bd64094307effea6d1bd5b465b9632e9185dcfd8 (patch) | |
| tree | 659bb5917b3595523a386a8f5e6b34a5ca304dbd | |
| parent | 3260fe0e07221d6637b73740228a1b29678cea51 (diff) | |
| download | perlweeklychallenge-club-bd64094307effea6d1bd5b465b9632e9185dcfd8.tar.gz perlweeklychallenge-club-bd64094307effea6d1bd5b465b9632e9185dcfd8.tar.bz2 perlweeklychallenge-club-bd64094307effea6d1bd5b465b9632e9185dcfd8.zip | |
Add solutions to 289: Third Maximum & Jumbled Letters by E. Choroba
| -rwxr-xr-x | challenge-289/e-choroba/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-289/e-choroba/perl/ch-2.pl | 29 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-289/e-choroba/perl/ch-1.pl b/challenge-289/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..0992b840b4 --- /dev/null +++ b/challenge-289/e-choroba/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub third_maximum(@ints) { + my %uniq; + @uniq{@ints} = (); + my @max3; + + for my $n (keys %uniq) { + next if 3 == @max3 && $n <= $max3[2]; + + push @max3, $n; + @max3 = sort { $b <=> $a } @max3; + pop @max3 if @max3 > 3; + } + return $max3[ 3 == @max3 ? 2 : 0 ] +} + +use Test::More tests => 3 + 1; + +is third_maximum(5, 6, 4, 1), 4, 'Example 1'; +is third_maximum(4, 5), 5, 'Example 2'; +is third_maximum (1, 2, 2, 3), 1, 'Example 3'; + +is third_maximum(1 .. 1000, 1 .. 1000), 998, 'Long'; diff --git a/challenge-289/e-choroba/perl/ch-2.pl b/challenge-289/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..26a649f940 --- /dev/null +++ b/challenge-289/e-choroba/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ shuffle }; + +sub jumbled_letters($text) { + $text =~ s/(\w+)/jumble_word($1)/ger +} + +sub jumble_word($word) { + my $shuffle_length = length($word) - 2; + substr $word, 1, $shuffle_length, join "", + shuffle(split //, substr $word, 1, $shuffle_length); + return $word +} + +use Test2::V0; +plan(4); + +is jumbled_letters('Perl'), in_set('Perl', 'Prel'), 'Perl'; + +my $j = jumbled_letters("It doesn't matter."); +like $j, qr/^It d...n't m....r\.$/, "Characters that don't move"; +is substr($j, 4, 3), in_set(qw( oes ose eos eso seo soe )), 'length 3'; +is substr($j, 12, 4), in_set(qw( atte atet aett etta etat eatt + ttae ttea taet teat tate teta )), + 'length 4'; |
