diff options
| author | Ulrich Reining <spuelrich@posteo.de> | 2025-07-08 21:33:50 +0200 |
|---|---|---|
| committer | Ulrich Reining <spuelrich@posteo.de> | 2025-07-08 21:33:50 +0200 |
| commit | d38b95b1c0ef76ae4ea937cb33339ac721264631 (patch) | |
| tree | 137ed5a2e39fe72d6bf357db6eb5eba429045dfc | |
| parent | 09ded88c0f22d977cc3ce8163855b48bf0aec03b (diff) | |
| download | perlweeklychallenge-club-d38b95b1c0ef76ae4ea937cb33339ac721264631.tar.gz perlweeklychallenge-club-d38b95b1c0ef76ae4ea937cb33339ac721264631.tar.bz2 perlweeklychallenge-club-d38b95b1c0ef76ae4ea937cb33339ac721264631.zip | |
challenge 329 by spuelrich
| -rw-r--r-- | challenge-329/spuelrich/README | 52 | ||||
| -rwxr-xr-x | challenge-329/spuelrich/perl/ch-01.pl | 25 | ||||
| -rwxr-xr-x | challenge-329/spuelrich/perl/ch-02.pl | 54 |
3 files changed, 130 insertions, 1 deletions
diff --git a/challenge-329/spuelrich/README b/challenge-329/spuelrich/README index dbdeadaa09..84be6f2cab 100644 --- a/challenge-329/spuelrich/README +++ b/challenge-329/spuelrich/README @@ -1 +1,51 @@ -Solution by spuelrich
\ No newline at end of file +# Solution by spuelrich + +## Task 1: Counter Integers + +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 + +## Task 2: Nice String + +You are given a string made up of lower and upper case English letters only. + +Write a script to return the longest substring of the give string which is nice. A string is nice if, for every letter of the alphabet that the string contains, it appears both in uppercase and lowercase. + +Example 1 + +Input: $str = "YaaAho" +Output: "aaA" + + +Example 2 + +Input: $str = "cC" +Output: "cC" + + +Example 3 + +Input: $str = "A" +Output: "" + +No nice string found. diff --git a/challenge-329/spuelrich/perl/ch-01.pl b/challenge-329/spuelrich/perl/ch-01.pl new file mode 100755 index 0000000000..b65b1dc8fa --- /dev/null +++ b/challenge-329/spuelrich/perl/ch-01.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use Modern::Perl; +use experimental qw(signatures); +use Test::More; + +sub ch1 ($str) { + # find integers + my @integers = ($str =~ /(\d+)/g); + + # remove duplicates + my %seen; + return ( + grep { !$seen{$_}++ } + @integers + ); +} + + +is_deeply([ch1('the1weekly2challenge2')], [1, 2]); +is_deeply([ch1('the1weekly2challenge2and1')], [1, 2]); +is_deeply([ch1('the1weekly2challenge2more3')], [1, 2, 3]); +is_deeply([ch1('go21od1lu5c7k')], [21, 1, 5, 7]); +is_deeply([ch1('4p3e2r1l')], [4, 3, 2, 1]); + +done_testing(); diff --git a/challenge-329/spuelrich/perl/ch-02.pl b/challenge-329/spuelrich/perl/ch-02.pl new file mode 100755 index 0000000000..6526a5efb4 --- /dev/null +++ b/challenge-329/spuelrich/perl/ch-02.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +use Modern::Perl; +use experimental qw(signatures); +use Test::More; + +sub is_nice_string ($string) { + my %lowers = ( + map {$_ => 1} + $string =~ /([a-z])/g + ); + my %uppers = ( + map {$_ => 1} + $string =~ /([A-Z])/g + ); + + return uc(join(q(), sort keys %lowers)) eq join(q(), sort keys %uppers); +} + +sub find_longest_nice_substring ($string) { + my $length = length($string); + my @substrings; + for my $start (0 .. $length - 2) { + for my $substr_length (2 .. $length - $start) { + push @substrings, substr($string, $start, $substr_length); + } + } + + my $longest_nice_substring = ( + sort {$b->[0] <=> $a->[0]} + map {[length($_), $_]} + grep {is_nice_string($_)} + @substrings + )[0]; + + return $longest_nice_substring ? $longest_nice_substring->[1] : q(); +} + +ok(is_nice_string('aA')); +ok(is_nice_string('abAB')); +ok(is_nice_string('aAbB')); +ok(is_nice_string('aaaAbaB')); +ok(!is_nice_string('a')); +ok(!is_nice_string('A')); +ok(!is_nice_string('aB')); +ok(!is_nice_string('bA')); + +is(find_longest_nice_substring('YaaAho'), 'aaA'); +is(find_longest_nice_substring('cC'), 'cC'); +is(find_longest_nice_substring('guuhUHarto'), 'uuhUH'); +is(find_longest_nice_substring('abababAB'), 'abababAB'); +is(find_longest_nice_substring('A'), ''); +is(find_longest_nice_substring('ABC'), ''); + +done_testing(); |
