diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-02 23:27:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-02 23:27:40 +0100 |
| commit | bb7c5037520118913b30668db812a54217819bde (patch) | |
| tree | 7429fa09b6f2c15415abd9f128e8d709095558e5 | |
| parent | 041c3343d699fe070492cab9025455020a010e1e (diff) | |
| parent | 69fa88d63378e716036aa1eedbb676303f67e821 (diff) | |
| download | perlweeklychallenge-club-bb7c5037520118913b30668db812a54217819bde.tar.gz perlweeklychallenge-club-bb7c5037520118913b30668db812a54217819bde.tar.bz2 perlweeklychallenge-club-bb7c5037520118913b30668db812a54217819bde.zip | |
Merge pull request #12266 from jeanluc2020/jeanluc2020-328
Add solution 328.
| -rw-r--r-- | challenge-328/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-328/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-328/jeanluc2020/perl/ch-1.pl | 80 | ||||
| -rwxr-xr-x | challenge-328/jeanluc2020/perl/ch-2.pl | 70 |
4 files changed, 152 insertions, 0 deletions
diff --git a/challenge-328/jeanluc2020/blog-1.txt b/challenge-328/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..8e7ea7251d --- /dev/null +++ b/challenge-328/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-328-1.html diff --git a/challenge-328/jeanluc2020/blog-2.txt b/challenge-328/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..b6808cfe7a --- /dev/null +++ b/challenge-328/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-328-2.html diff --git a/challenge-328/jeanluc2020/perl/ch-1.pl b/challenge-328/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..83e67792c8 --- /dev/null +++ b/challenge-328/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-328/#TASK1 +# +# Task 1: Replace all ? +# ===================== +# +# You are given a string containing only lower case English letters and ?. +# +# Write a script to replace all ? in the given string so that the string +# doesn’t contain consecutive repeating characters. +# +## Example 1 +## +## Input: $str = "a?z" +## Output: "abz" +## +## There can be many strings, one of them is "abz". +## The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'. +# +# +## Example 2 +## +## Input: $str = "pe?k" +## Output: "peak" +# +# +## Example 3 +## +## Input: $str = "gra?te" +## Output: "grabte" +# +############################################################ +## +## discsussion +## +############################################################ +# +# Starting from left to right, we pick each "?" and try to replace it. +# For that, we walk the characters from a to z, and if we find one that +# works, we use it right away. + +use v5.36; + +replace_all("a?z"); +replace_all("pe?k"); +replace_all("gra?te"); +replace_all("gra?te"); +replace_all("?bc"); +replace_all("ab?"); +replace_all("ab?a"); +replace_all("ab???"); +replace_all("???a"); + +sub replace_all($str) { + say "Input: $str"; + my @chars = split //, $str; + foreach my $i (0..$#chars) { + if($chars[$i] eq "?") { + foreach my $c ("a".."z") { + my $ok = 1; + if($i != 0) { + if($chars[$i-1] eq $c) { + $ok = 0; + } + } + if($i != $#chars) { + if($chars[$i+1] eq $c) { + $ok = 0; + } + } + if($ok) { + $chars[$i] = $c; + last; + } + } + } + } + my $output = join("",@chars); + say "Output: $output"; +} diff --git a/challenge-328/jeanluc2020/perl/ch-2.pl b/challenge-328/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..c4215ff96c --- /dev/null +++ b/challenge-328/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-328/#TASK2 +# +# Task 2: Good String +# =================== +# +# You are given a string made up of lower and upper case English letters only. +# +# Write a script to return the good string of the given string. A string is +# called good string if it doesn’t have two adjacent same characters, one in +# upper case and other is lower case. +# +## Example 1 +## +## Input: $str = "WeEeekly" +## Output: "Weekly" +## +## We can remove either, "eE" or "Ee" to make it good. +# +# +## Example 2 +## +## Input: $str = "abBAdD" +## Output: "" +## +## We remove "bB" first: "aAdD" +## Then we remove "aA": "dD" +## Finally remove "dD". +# +# +## Example 3 +## +## Input: $str = "abc" +## Output: "abc" +# +############################################################ +## +## discussion +## +############################################################ +# +# As long as we have two adjacent same characters, one in upper and +# one in lower case, we remove that part from the string. What remains +# in the end is the good string. + +use v5.36; + +good_string("WeEeekly"); +good_string("abBAdD"); +good_string("abc"); + +sub good_string($str) { + say "Input: $str"; + my $found = 1; + while($found) { + $found = 0; + foreach my $c ("a".."z") { + my $C = uc($c); + if($str =~ m/$c$C/) { + $str =~ s/$c$C//; + $found = 1; + } + if($str =~ m/$C$c/) { + $str =~ s/$C$c//; + $found = 1; + } + } + } + say "Output: $str"; +} |
