diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-17 23:04:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-17 23:04:54 +0100 |
| commit | 9a468afbab2cd00362e7d5c726fbf1565a6d75ba (patch) | |
| tree | b434bd2c7f38de07946b5d4a8b062753ff2a0434 /challenge-278 | |
| parent | 7fb6116d76583e09c32b53f319ed71d2d33b8183 (diff) | |
| parent | 304755f79fc5578a5ecd191b099d00da018acd03 (diff) | |
| download | perlweeklychallenge-club-9a468afbab2cd00362e7d5c726fbf1565a6d75ba.tar.gz perlweeklychallenge-club-9a468afbab2cd00362e7d5c726fbf1565a6d75ba.tar.bz2 perlweeklychallenge-club-9a468afbab2cd00362e7d5c726fbf1565a6d75ba.zip | |
Merge pull request #10451 from lancew/wk-278-lw
Perl solution by Lance
Diffstat (limited to 'challenge-278')
| -rw-r--r-- | challenge-278/lance-wicks/perl/ch-1.pl | 11 | ||||
| -rw-r--r-- | challenge-278/lance-wicks/perl/lib/String.pm | 29 | ||||
| -rw-r--r-- | challenge-278/lance-wicks/perl/t/ch-1.t | 24 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-278/lance-wicks/perl/ch-1.pl b/challenge-278/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..231ac32f3c --- /dev/null +++ b/challenge-278/lance-wicks/perl/ch-1.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; + +use lib './lib'; +use String; + +my $string = join ' ', @ARGV; + +my $output = String->num_sort($string); + +print "\n$output\n";
\ No newline at end of file diff --git a/challenge-278/lance-wicks/perl/lib/String.pm b/challenge-278/lance-wicks/perl/lib/String.pm new file mode 100644 index 0000000000..b47f98b20e --- /dev/null +++ b/challenge-278/lance-wicks/perl/lib/String.pm @@ -0,0 +1,29 @@ +package String; + +use strict; +use warnings; + + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +use Data::Dumper; + +sub num_sort ( $class, $string ) { + my @words = split " ", $string; + + my %word_order; + for my $item (@words) { + $item =~ m/^(\w+)(\d+)$/; + $word_order{$2} = $1; + } + + my @words_in_order; + for my $item ( sort keys %word_order ) { + push @words_in_order, $word_order{$item}; + } + + return join ' ', @words_in_order; +} + +1;
\ No newline at end of file diff --git a/challenge-278/lance-wicks/perl/t/ch-1.t b/challenge-278/lance-wicks/perl/t/ch-1.t new file mode 100644 index 0000000000..2eeccb7593 --- /dev/null +++ b/challenge-278/lance-wicks/perl/t/ch-1.t @@ -0,0 +1,24 @@ +use Test2::V0 -target => 'String'; + +subtest "Eaxample 1" => sub { + my $str = "and2 Raku3 cousins5 Perl1 are4"; + my $expected = "Perl and Raku are cousins"; + + is $CLASS->num_sort($str), $expected; +}; + +subtest "Example 2" => sub { + my $str = "guest6 Python1 most4 the3 popular5 is2 language7"; + my $expected = "Python is the most popular guest language"; + + is $CLASS->num_sort($str), $expected; +}; + +subtest "Example 3" => sub { + my $str = "Challenge3 The1 Weekly2"; + my $expected = "The Weekly Challenge"; + + is $CLASS->num_sort($str), $expected; +}; + +done_testing();
\ No newline at end of file |
