diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-09-22 19:02:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-22 19:02:43 +0200 |
| commit | 5650f5f8ecfbd388441323510fafae9f8f4726f6 (patch) | |
| tree | 91a192d90ae93af6c35b850f9b03547c32e527a9 | |
| parent | c4e70544812c339e0344ad3127de18a5dbf98c34 (diff) | |
| download | perlweeklychallenge-club-5650f5f8ecfbd388441323510fafae9f8f4726f6.tar.gz perlweeklychallenge-club-5650f5f8ecfbd388441323510fafae9f8f4726f6.tar.bz2 perlweeklychallenge-club-5650f5f8ecfbd388441323510fafae9f8f4726f6.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-340/wanderdoc/perl/ch-1.pl | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/challenge-340/wanderdoc/perl/ch-1.pl b/challenge-340/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..faf4abc5a9 --- /dev/null +++ b/challenge-340/wanderdoc/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string, $str, consisting of lowercase English letters. + +Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can. + + A duplicate removal consists of choosing two adjacent and equal letters and removing them. + + +Example 1 + +Input: $str = 'abbaca' +Output: 'ca' + +Step 1: Remove 'bb' => 'aaca' +Step 2: Remove 'aa' => 'ca' + + +Example 2 + +Input: $str = 'azxxzy' +Output: 'ay' + +Step 1: Remove 'xx' => 'azzy' +Step 2: Remove 'zz' => 'ay' + + +Example 3 + +Input: $str = 'aaaaaaaa' +Output: '' + +Step 1: Remove 'aa' => 'aaaaaa' +Step 2: Remove 'aa' => 'aaaa' +Step 3: Remove 'aa' => 'aa' +Step 4: Remove 'aa' => '' + + +Example 4 + +Input: $str = 'aabccba' +Output: 'a' + +Step 1: Remove 'aa' => 'bccba' +Step 2: Remove 'cc' => 'bba' +Step 3: Remove 'bb' => 'a' + + +Example 5 + +Input: $str = 'abcddcba' +Output: '' + +Step 1: Remove 'dd' => 'abccba' +Step 2: Remove 'cc' => 'abba' +Step 3: Remove 'bb' => 'aa' +Step 4: Remove 'aa' => '' +=cut + +use Test2::V0 -no_srand => 1; +is(duplicate_removal('abbaca'), 'ca', 'Example 1'); +is(duplicate_removal('azxxzy'), 'ay', 'Example 2'); +is(duplicate_removal('aaaaaaaa'), '', 'Example 3'); +is(duplicate_removal('aabccba'), 'a', 'Example 4'); +is(duplicate_removal('abcddcba'), '', 'Example 5'); +done_testing(); + +sub duplicate_removal +{ + my $str = $_[0]; + while ( $str =~ /(.)\g{1}/ ) + { + $str =~ s/(.)\g{1}//; + } + return $str; +} |
