diff options
| author | wanderdoc <wanderdoc@users.noreply.github.com> | 2025-07-07 17:13:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-07 17:13:07 +0200 |
| commit | e1aaa879a5be00f2451e92b9cc3b3b9545ca5659 (patch) | |
| tree | 1f3021a417d6e298c7524dc89ed2f3f46767b839 | |
| parent | bd7fce4bd5d085c209a213f2daca1e79799c9e87 (diff) | |
| download | perlweeklychallenge-club-e1aaa879a5be00f2451e92b9cc3b3b9545ca5659.tar.gz perlweeklychallenge-club-e1aaa879a5be00f2451e92b9cc3b3b9545ca5659.tar.bz2 perlweeklychallenge-club-e1aaa879a5be00f2451e92b9cc3b3b9545ca5659.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-329/wanderdoc/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-329/wanderdoc/perl/ch-1.pl b/challenge-329/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..c09350a418 --- /dev/null +++ b/challenge-329/wanderdoc/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string containing only lower case English letters and digits. +Write a script to replace every non-digit character with a space and then return all the distinct integers left. + +Example 1 + +Input: $str = "the1weekly2challenge2" +Output: 1, 2 + +2 is appeared twice, so we count it one only. + + +Example 2 + +Input: $str = "go21od1lu5c7k" +Output: 21, 1, 5, 7 + + +Example 3 + +Input: $str = "4p3e2r1l" +Output: 4, 3, 2, 1 +=cut + +use List::Util qw(uniq); +use Test2::V0 -no_srand => 1; +is(counter_integers('the1weekly2challenge2'), '1, 2', 'Example 1'); +is(counter_integers('go21od1lu5c7k'), '21, 1, 5, 7', 'Example 2'); +is(counter_integers('4p3e2r1l'), '4, 3, 2, 1', 'Example 3'); +done_testing(); + + +sub counter_integers +{ + my $str = $_[0]; + $str =~ s/[[:alpha:]]+/ /g; + $str =~ s/(^ +| +$)//g; + return join(", ", uniq(split(/ /, $str))); # no hash because of sequence order. +} |
