diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-02 21:12:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-02 21:12:02 +0100 |
| commit | 7763e20084f24f8f8e8e06b2092ed7718a699c4a (patch) | |
| tree | 527d0e73a3fb9e0b66e6bc1cc62740abfde4360e | |
| parent | 17a71dc867ddfc3e4c71bbb6ba1c3538d226d217 (diff) | |
| parent | bd64094307effea6d1bd5b465b9632e9185dcfd8 (diff) | |
| download | perlweeklychallenge-club-7763e20084f24f8f8e8e06b2092ed7718a699c4a.tar.gz perlweeklychallenge-club-7763e20084f24f8f8e8e06b2092ed7718a699c4a.tar.bz2 perlweeklychallenge-club-7763e20084f24f8f8e8e06b2092ed7718a699c4a.zip | |
Merge pull request #10948 from choroba/ech289
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'; |
