diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-10-03 12:24:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-03 12:24:12 +0200 |
| commit | 44fa7a0afffff7d028e0dc7523044ce9f40647fe (patch) | |
| tree | bb37de6f85606fc0ab5441eaf318948767260be3 | |
| parent | f22989ddff23d97b7d29a26a846164a8675e67bb (diff) | |
| download | perlweeklychallenge-club-44fa7a0afffff7d028e0dc7523044ce9f40647fe.tar.gz perlweeklychallenge-club-44fa7a0afffff7d028e0dc7523044ce9f40647fe.tar.bz2 perlweeklychallenge-club-44fa7a0afffff7d028e0dc7523044ce9f40647fe.zip | |
Create ch-2.pl
| -rw-r--r-- | challenge-341/wanderdoc/perl/ch-2.pl | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-341/wanderdoc/perl/ch-2.pl b/challenge-341/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..245dfb5c22 --- /dev/null +++ b/challenge-341/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string, $str and a character in the given string, $char. +Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + +=cut + + + + +use Test2::V0 -no_srand => 1; + +is(reverse_prefix("programming", "g"), "gorpramming", "Example 1"); +is(reverse_prefix("hello", "h"), "hello", "Example 2"); +is(reverse_prefix("abcdefghij", "h"), "hgfedcbaij", "Example 3"); +is(reverse_prefix("reverse", "s"), "srevere", "Example 4"); +is(reverse_prefix("perl", "r"), "repl", "Example 5"); +done_testing(); + +sub reverse_prefix +{ + my ($str, $char) = @_; + my $idx = index($str, $char, 0); + return reverse(substr($str, 0, $idx + 1)) . substr($str, $idx + 1); +} |
