diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 22:21:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 22:21:52 +0100 |
| commit | cf22933c7cf9fc65aea34cd7c73d15051dffd404 (patch) | |
| tree | e93688d4c21702339d8812a28f798dc0c6d4dfb7 | |
| parent | d1daa3d60187aa86b6e6d877d2acb65341e263ff (diff) | |
| parent | ef8987dbde7ed84e63318b1ca43a895b4588dc0c (diff) | |
| download | perlweeklychallenge-club-cf22933c7cf9fc65aea34cd7c73d15051dffd404.tar.gz perlweeklychallenge-club-cf22933c7cf9fc65aea34cd7c73d15051dffd404.tar.bz2 perlweeklychallenge-club-cf22933c7cf9fc65aea34cd7c73d15051dffd404.zip | |
Merge pull request #10311 from jeanluc2020/jeanluc-275
Add solution 275
| -rw-r--r-- | challenge-275/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-275/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-275/jeanluc2020/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-275/jeanluc2020/perl/ch-2.pl | 76 |
4 files changed, 150 insertions, 0 deletions
diff --git a/challenge-275/jeanluc2020/blog-1.txt b/challenge-275/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..d3164fc98c --- /dev/null +++ b/challenge-275/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-275-1.html diff --git a/challenge-275/jeanluc2020/blog-2.txt b/challenge-275/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..77a6c93758 --- /dev/null +++ b/challenge-275/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-275-2.html diff --git a/challenge-275/jeanluc2020/perl/ch-1.pl b/challenge-275/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..1e78eea602 --- /dev/null +++ b/challenge-275/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/#TASK1 +# +# Task 1: Broken Keys +# =================== +# +# You are given a sentence, $sentence and list of broken keys @keys. +# +# Write a script to find out how many words can be typed fully. +# +## Example 1 +## +## Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +## Output: 0 +# +## Example 2 +## +## Input: $sentence = "Perl and Raku", @keys = ('a') +## Output: 1 +## +## Only Perl since the other word two words contain 'a' and can't be typed fully. +# +## Example 3 +## +## Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +## Output: 2 +# +## Example 4 +## +## Input: $sentence = "The joys of polyglottism", @keys = ('T') +## Output: 2 +# +############################################################ +## +## discussion +## +############################################################ +# +# We split the sentence into its words. Then we create an empty +# temporary list, and walk through the list of broken keys. If a +# word doesn't match the broken key, it will go into the temporary +# list, and at the end of each loop over the broken keys, the new +# list will be the list of words from the sentence that didn't +# match any broken keys so far, while the temporary list will be +# emptied again. This way, at the end of the loop over the broken +# keys, we only have the words that don't match any of the broken +# keys in the final list, of which we return the number of elements. + +use strict; +use warnings; + +broken_keys( "Perl Weekly Challenge", "l", "a" ); +broken_keys( "Perl and Raku", "a" ); +broken_keys( "Well done Team PWC", "l", "o" ); +broken_keys( "The joys of polyglottism", "T" ); + +sub broken_keys { + my ($sentence, @bk ) = @_; + print "Input: \$sentence = '$sentence', ('", join("', '", @bk), "')\n"; + my @words = split /\s+/, lc($sentence); + my @tmp = (); + foreach my $broken (@bk) { + $broken = lc($broken); + foreach my $word (@words) { + push @tmp, $word unless $word =~ m/$broken/; + } + @words = @tmp; + @tmp = (); + } + print "Output: ", scalar(@words), "\n"; +} + diff --git a/challenge-275/jeanluc2020/perl/ch-2.pl b/challenge-275/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..9b9d384d05 --- /dev/null +++ b/challenge-275/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/#TASK2 +# +# Task 2: Replace Digits +# ====================== +# +# You are given an alphanumeric string, $str, where each character is either a +# letter or a digit. +# +# Write a script to replace each digit in the given string with the value of +# the previous letter plus (digit) places. +# +## Example 1 +## +## Input: $str = 'a1c1e1' +## Ouput: 'abcdef' +## +## shift('a', 1) => 'b' +## shift('c', 1) => 'd' +## shift('e', 1) => 'f' +# +## Example 2 +## +## Input: $str = 'a1b2c3d4' +## Output: 'abbdcfdh' +## +## shift('a', 1) => 'b' +## shift('b', 2) => 'd' +## shift('c', 3) => 'f' +## shift('d', 4) => 'h' +# +## Example 3 +## +## Input: $str = 'b2b' +## Output: 'bdb' +# +## Example 4 +## +## Input: $str = 'a16z' +## Output: 'abgz' +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk the $str character by character. If it is a digits, +# calculate the corresponding new character for the result by +# using the previous character and the digit, otherwise just +# append the current character and take note of the character +# for the next round. + +use strict; +use warnings; + +replace_digits('a1c1e1'); +replace_digits('a1b2c3d4'); +replace_digits('b2b'); +replace_digits('a16z'); + +sub replace_digits { + my $str = shift; + print "Input: '$str'\n"; + my $previous_char = "a"; + my $result = ""; + foreach my $char (split //, $str) { + if($char =~ m/\d/) { + $result .= chr(ord($previous_char) + $char); + } else { + $result .= $char; + $previous_char = $char; + } + } + print "Output: '$result'\n"; +} |
