diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-09-23 18:55:17 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-09-23 18:55:17 -0700 |
| commit | bf4f8694c1b78736a85c39d28102a7d0b2fa532d (patch) | |
| tree | 970ef28e0ea1f9fe5df38356f12f7857ca5e053f /challenge-340 | |
| parent | e4cf6979506ce0b97f4168edfe69e3c4607d4c30 (diff) | |
| download | perlweeklychallenge-club-bf4f8694c1b78736a85c39d28102a7d0b2fa532d.tar.gz perlweeklychallenge-club-bf4f8694c1b78736a85c39d28102a7d0b2fa532d.tar.bz2 perlweeklychallenge-club-bf4f8694c1b78736a85c39d28102a7d0b2fa532d.zip | |
Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #340.
Diffstat (limited to 'challenge-340')
| -rw-r--r-- | challenge-340/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-340/robbie-hatley/perl/ch-1.pl | 99 | ||||
| -rwxr-xr-x | challenge-340/robbie-hatley/perl/ch-2.pl | 107 |
3 files changed, 207 insertions, 0 deletions
diff --git a/challenge-340/robbie-hatley/blog.txt b/challenge-340/robbie-hatley/blog.txt new file mode 100644 index 0000000000..4a6b9189ee --- /dev/null +++ b/challenge-340/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/09/robbie-hatleys-solutions-in-perl-for_23.html diff --git a/challenge-340/robbie-hatley/perl/ch-1.pl b/challenge-340/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..55cb2193dc --- /dev/null +++ b/challenge-340/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 340-1, +written by Robbie Hatley on Tue Sep 23, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 340-1: Duplicate Removals +Submitted by: Mohammad Sajid Anwar +You are given a string 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: 'abbaca' +Output: 'ca' +Step 1: Remove 'bb' => 'aaca' +Step 2: Remove 'aa' => 'ca' + +Example #2: +Input: 'azxxzy' +Output: 'ay' +Step 1: Remove 'xx' => 'azzy' +Step 2: Remove 'zz' => 'ay' + +Example #3: +Input: 'aaaaaaaa' +Output: '' +Step 1: Remove 'aa' => 'aaaaaa' +Step 2: Remove 'aa' => 'aaaa' +Step 3: Remove 'aa' => 'aa' +Step 4: Remove 'aa' => '' + +Example #4: +Input: 'aabccba' +Output: 'a' +Step 1: Remove 'aa' => 'bccba' +Step 2: Remove 'cc' => 'bba' +Step 3: Remove 'bb' => 'a' + +Example #5: +Input: 'abcddcba' +Output: '' +Step 1: Remove 'dd' => 'abccba' +Step 2: Remove 'cc' => 'abba' +Step 3: Remove 'bb' => 'aa' +Step 4: Remove 'aa' => '' + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem, I'll use "substr" in a 3-part loop, double-decrementing the index after each deletion +in order to account for possible new "duplicate" relationship between previous character and new current +character. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of double-quoted strings, in proper Perl syntax, like so: + +./ch-1.pl '("看的星星", "rat", "booth", "Mississippi", "lollollol")' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Nix all duplicates from a string: + sub nix_dup ($s) { + for ( my $idx = 0 ; $idx <= length($s)-2 ; ++$idx ) { + if ( substr($s, $idx, 1) eq substr($s, $idx+1, 1) ) { + substr $s, $idx, 2, ''; + $idx -= 2; + $idx = -1 if $idx < -1}} + $s} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @strings = @ARGV ? eval($ARGV[0]) : ("abbaca", "azxxzy", "aaaaaaaa", "aabccba", "abcddcba"); +# Expected outputs : "ca" "ay" "" "a" "" + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $s (@strings) { + say ''; + my $t = nix_dup($s); + say "Original string = \"$s\""; + say "Deduped string = \"$t\""} diff --git a/challenge-340/robbie-hatley/perl/ch-2.pl b/challenge-340/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..94ebf023b7 --- /dev/null +++ b/challenge-340/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,107 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 340-2, +written by Robbie Hatley on Tue Sep 23, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 340-2: Ascending Numbers +Submitted by: Mohammad Sajid Anwar + +You are given a string which is a list of tokens separated by +single spaces. Every token is either a positive number consisting +of digits 0-9 with no leading zeros, or a word consisting of +lowercase English letters. Write a script to check if all the +numbers in the given string are strictly increasing from left +to right. + +Example #1: +Input: "The cat has 3 kittens 7 toys 10 beds" +Output: true +Numbers 3, 7, 10 - strictly increasing. + +Example #2: +Input: 'Alice bought 5 apples 2 oranges 9 bananas' +Output: false + +Example #3: +Input: 'I ran 1 mile 2 days 3 weeks 4 months' +Output: true + +Example #4: +Input: 'Bob has 10 cars 10 bikes' +Output: false + +Example #5: +Input: 'Zero is 0 one is 1 two is 2' +Output: true + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To solve this problem, for each token $t which consists solely of digits, I'll push (0+$t) to an array @a, +then return true if-and-only-if @a is strictly-increasing. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of double-quoted strings, in proper Perl syntax, like so: + +./ch-2.pl '("Jim ate 7 cats and 3 rats!", "Jim ate 3 cats and 7 rats!", "I ate 5 bats and 5 gnats.")' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Are the numbers in a string strictly-increasing? + sub strictly_increasing ($s) { + my @t = split /\s+/, $s; + my @n; + for (@t) { + push @n, 0+$_ if /^\d+$/} + for (0..$#n-1) { + return 'false' if $n[$_] >= $n[$_+1]} + return 'true'} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @strings = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + "The cat has 3 kittens 7 toys 10 beds", + # Expected output: true + + # Example 2 input: + "Alice bought 5 apples 2 oranges 9 bananas", + # Expected output: false + + # Example 3 input: + "I ran 1 mile 2 days 3 weeks 4 months", + # Expected output: true + + # Example 4 input: + "Bob has 10 cars 10 bikes", + # Expected output: false + + # Example 5 input: + "Zero is 0 one is 1 two is 2", + # Expected output: true +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $s (@strings) { + say ''; + my $i = strictly_increasing($s); + say "String = \"$s\""; + say "Numbers are strictly-increasing? $i"} |
