diff options
| -rw-r--r-- | challenge-329/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-329/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-329/jeanluc2020/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-329/jeanluc2020/perl/ch-2.pl | 70 |
4 files changed, 136 insertions, 0 deletions
diff --git a/challenge-329/jeanluc2020/blog-1.txt b/challenge-329/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..530d2e3341 --- /dev/null +++ b/challenge-329/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-329-1.html diff --git a/challenge-329/jeanluc2020/blog-2.txt b/challenge-329/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0e91a137ff --- /dev/null +++ b/challenge-329/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-329-2.html diff --git a/challenge-329/jeanluc2020/perl/ch-1.pl b/challenge-329/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..b033b7ce80 --- /dev/null +++ b/challenge-329/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/#TASK1 +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# This one is fairly simple: +# - replace all non-digits by whitespace +# - remove leading or trailing whitespace +# - split string at whitespaces into individual integers +# - for each integer, put it into the result array unless it's +# already there + +use v5.36; + +counter_integers("the1weekly2challenge2"); +counter_integers("go21od1lu5c7k"); +counter_integers("4p3e2r1l"); + +sub counter_integers($str) { + say "Input: \"$str\""; + $str =~ s/[^\d]/ /g; + $str =~ s/^\s+//; + $str =~ s/\s+$//; + my @integers = split /\s+/, $str; + my @result = (); + my $seen = {}; + foreach my $integer (@integers) { + push @result, $integer unless $seen->{$integer}; + $seen->{$integer} = 1; + } + say "Output: " . join(", ", @result); +} + diff --git a/challenge-329/jeanluc2020/perl/ch-2.pl b/challenge-329/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..865680766e --- /dev/null +++ b/challenge-329/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/#TASK2 +# +# 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. +# +############################################################ +## +## discussion +## +############################################################ +# +# For each character in $str, we use the lowercase character of it +# as the key in a hash. There we keep track of each character that +# we have seen so far whether it was in lower, upper or both cases. +# In the end, we walk the characters from $str again and just keep +# the ones for which we have seen both the lower and upper case +# variant. + +use v5.36; + +nice_string("YaaAho"); +nice_string("cC"); +nice_string("A"); + +sub nice_string($str) { + say "Input: \"$str\""; + my @chars = split //, $str; + my $seen = {}; + my $result = ""; + foreach my $char (@chars) { + if($char =~ m/[a-z]/) { + $seen->{$char}->{lc} = 1; + } else { + $seen->{lc($char)}->{uc} = 1; + } + } + foreach my $char (@chars) { + if($seen->{lc($char)}->{lc} && $seen->{lc($char)}->{uc}) { + $result .= $char; + } + } + say "Output: \"$result\""; +} + |
