diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-07-01 19:37:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-01 19:37:36 +0200 |
| commit | dac508970486851efa92ce7aa602852b1a9dc781 (patch) | |
| tree | 2fc2c09e26f839ae565edf78ce7c4be887cbaa0f | |
| parent | cc09cbf5b720ad6b03ebba85fe6e0e21dc901300 (diff) | |
| download | perlweeklychallenge-club-dac508970486851efa92ce7aa602852b1a9dc781.tar.gz perlweeklychallenge-club-dac508970486851efa92ce7aa602852b1a9dc781.tar.bz2 perlweeklychallenge-club-dac508970486851efa92ce7aa602852b1a9dc781.zip | |
Create ch-2.pl
| -rw-r--r-- | challenge-328/wanderdoc/perl/ch-2.pl | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-328/wanderdoc/perl/ch-2.pl b/challenge-328/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..a3371733b4 --- /dev/null +++ b/challenge-328/wanderdoc/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string made up of lower and upper case English letters only. +Write a script to return the good string of the given string. A string is called good string if it doesn't have two adjacent same characters, one in upper case and other is lower case. + +Example 1 + +Input: $str = "WeEeekly" +Output: "Weekly" + +We can remove either, "eE" or "Ee" to make it good. + + +Example 2 + +Input: $str = "abBAdD" +Output: "" + +We remove "bB" first: "aAdD" +Then we remove "aA": "dD" +Finally remove "dD". + + +Example 3 + +Input: $str = "abc" +Output: "abc" +=cut + +use Test2::V0 -no_srand => 1; + +is(make_good_string("WeEeekly"), "Weekly", "Example 1"); +is(make_good_string("abBAdD"), "", "Example 2"); +is(make_good_string("abc"), "abc", "Example 3"); +done_testing(); + + +sub make_good_string +{ + my $str = $_[0]; + my @arr = split(//, $str); + LOOP: while ( 1 ) + { + return '' unless @arr; + for my $idx ( 0 .. $#arr - 1 ) + { + if ( ord($arr[$idx]) - ord($arr[$idx+1]) == 32 + or + ord($arr[$idx+1]) - ord($arr[$idx]) == 32 + ) + { + splice(@arr, $idx, 2); + next LOOP; + } + } + return join('', @arr); + } +} |
