From bd64094307effea6d1bd5b465b9632e9185dcfd8 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 1 Oct 2024 23:02:02 +0200 Subject: Add solutions to 289: Third Maximum & Jumbled Letters by E. Choroba --- challenge-289/e-choroba/perl/ch-1.pl | 27 +++++++++++++++++++++++++++ challenge-289/e-choroba/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100755 challenge-289/e-choroba/perl/ch-1.pl create mode 100755 challenge-289/e-choroba/perl/ch-2.pl 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'; -- cgit