aboutsummaryrefslogtreecommitdiff
path: root/challenge-278
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-21 21:52:17 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2024-07-21 21:52:17 +0100
commit4ed82ae475e3554cff5d9796e043f34a94ce5ccf (patch)
treeaead41ab624994e82d1d9ed8d39bb6758d8fd195 /challenge-278
parent31c9ce7e6019dfc2564c7f9f10075c767b9ef27f (diff)
downloadperlweeklychallenge-club-4ed82ae475e3554cff5d9796e043f34a94ce5ccf.tar.gz
perlweeklychallenge-club-4ed82ae475e3554cff5d9796e043f34a94ce5ccf.tar.bz2
perlweeklychallenge-club-4ed82ae475e3554cff5d9796e043f34a94ce5ccf.zip
- Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-278')
-rwxr-xr-xchallenge-278/wanderdoc/perl/ch-1.pl56
-rwxr-xr-xchallenge-278/wanderdoc/perl/ch-2.pl50
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-278/wanderdoc/perl/ch-1.pl b/challenge-278/wanderdoc/perl/ch-1.pl
new file mode 100755
index 0000000000..fcb4a32796
--- /dev/null
+++ b/challenge-278/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a shuffle string, $str.
+
+Write a script to return the sorted string.
+
+ A string is Shuffled by appending word position to each word.
+
+Example 1
+
+Input: $str = "and2 Raku3 cousins5 Perl1 are4"
+Output: "Perl and Raku are cousins"
+
+Example 2
+
+Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7"
+Output: "Python is the most popular guest language"
+
+Example 3
+
+Input: $str = "Challenge3 The1 Weekly2"
+Output: "The Weekly Challenge"
+=cut
+
+# use feature qw(state);
+# using constant instead:
+use constant ALPHANUM => qr/(\p{IsAlpha}+)(\p{IsDigit}+)/;
+
+use Test2::V0;
+
+is(sort_string(q(and2 Raku3 cousins5 Perl1 are4)), q(Perl and Raku are cousins), 'Example 1');
+is(sort_string(q(guest6 Python1 most4 the3 popular5 is2 language7)), q(Python is the most popular guest language), 'Example 2');
+is(sort_string(q(Challenge3 The1 Weekly2)), q(The Weekly Challenge), 'Example 3');
+done_testing();
+
+sub sort_string
+{
+ my $str = $_[0];
+ my @arr = split(/\s/, $str);
+ @arr =
+ map { $_->[0] }
+ sort { $a->[1] <=> $b->[1] }
+ map { [split_element($_)] }
+ @arr;
+ return join(' ', @arr);
+}
+
+sub split_element
+{
+ my $word = $_[0];
+ my($alpha, $num) = $word =~ ALPHANUM;
+ return ($alpha, $num);
+} \ No newline at end of file
diff --git a/challenge-278/wanderdoc/perl/ch-2.pl b/challenge-278/wanderdoc/perl/ch-2.pl
new file mode 100755
index 0000000000..8593b155cf
--- /dev/null
+++ b/challenge-278/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a word, $word and a character, $char.
+
+Write a script to replace the substring up to and including $char with its characters sorted alphabetically. If the $char doesn't exist then DON'T do anything.
+Example 1
+
+Input: $str = "challenge", $char = "e"
+Output: "acehllnge"
+
+Example 2
+
+Input: $str = "programming", $char = "a"
+Output: "agoprrmming"
+
+Example 3
+
+Input: $str = "champion", $char = "b"
+Output: "champion"
+=cut
+
+
+
+use List::MoreUtils qw(first_index);
+use Test2::V0;
+
+is(reverse_word("challenge", "e"), "acehllnge", 'Example 1');
+is(reverse_word("programming", "a"), "agoprrmming", 'Example 2');
+is(reverse_word("champion", "b"), "champion", 'Example 3');
+done_testing();
+
+sub reverse_word
+{
+ my $word = $_[0];
+ my $char = $_[1];
+ my @arr = split(//, $word);
+ my $idx = first_index { $_ eq $char } @arr;
+ if ( $idx == -1 )
+ {
+ return $word;
+ }
+ else
+ {
+ @arr[0 .. $idx] = sort {$a cmp $b} @arr[0 .. $idx];
+ return join('', @arr);
+ }
+} \ No newline at end of file