diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-07 19:06:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-07 19:06:15 +0000 |
| commit | 0644da64ce091c8677c9c1ee3bb3719d4cc8b324 (patch) | |
| tree | 128ac81a49578573468920540440ad32291aba32 | |
| parent | b6fb6bae5c4604f789cf0f38b40607dd8eb20fb5 (diff) | |
| parent | 57b1ca83cb7e4fc9d4500d09486e8ce6c37fa162 (diff) | |
| download | perlweeklychallenge-club-0644da64ce091c8677c9c1ee3bb3719d4cc8b324.tar.gz perlweeklychallenge-club-0644da64ce091c8677c9c1ee3bb3719d4cc8b324.tar.bz2 perlweeklychallenge-club-0644da64ce091c8677c9c1ee3bb3719d4cc8b324.zip | |
Merge pull request #7050 from pjcs00/wk190
Week 190 tasks completed
| -rw-r--r-- | challenge-190/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-190/peter-campbell-smith/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-190/peter-campbell-smith/perl/ch-2.pl | 93 |
3 files changed, 120 insertions, 0 deletions
diff --git a/challenge-190/peter-campbell-smith/blog.txt b/challenge-190/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..0aa7e6346d --- /dev/null +++ b/challenge-190/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/11/capital-test-and-ambiguous-encoding.html diff --git a/challenge-190/peter-campbell-smith/perl/ch-1.pl b/challenge-190/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..a4b4a01566 --- /dev/null +++ b/challenge-190/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-11-07 +# PWC 190 task 1 + +use v5.28; +use utf8; +use warnings; +binmode(STDOUT, ':utf8'); + +# You are given a string with alphabetic characters only: A..Z and a..z. Write a script to find out if the usage of +# capital letters is appropriate if it satisfies at least one of the following rules: +# 1) Only first letter is capital and all others are small. +# 2) Every letter is small. +# 3) Every letter is capital. + +# Blog: https://pjcs-pwc.blogspot.com/2022/11/capital-test-and-ambiguous-encoding.html + +my (@tests, $test); + +@tests = qw[Perl PWC PyThon raku Byron ShakesSpeare miltoN KEATS 123 6-fold Hello! a A]; + +# loop over tests + while ($test = shift @tests) { + say qq[\nInput: $test\nOutput: ] . ($test =~ m/^[A-Z]?([a-z]*|[A-Z]*)$/ ? 1 : 0); +} diff --git a/challenge-190/peter-campbell-smith/perl/ch-2.pl b/challenge-190/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..542854322e --- /dev/null +++ b/challenge-190/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-11-07 +# PWC 190 task 1 + +use v5.28; +use utf8; +use warnings; +binmode(STDOUT, ':utf8'); + +# You are given a string consisting of a sequence of numeric characters: 0..9. The string is a concatenation +# of encoded letters A-Z, where A = 1, B = 2 ... Z = 26. Write a script to find the all valid different decodings +# in sorted order. Example: Input: $s = 11; Ouput: AA, K + +# Blog: https://pjcs-pwc.blogspot.com/2022/11/capital-test-and-ambiguous-encoding.html + +my (@tests, $base, $string, $answer, %answers, $test); + +@tests = qw[11 1115 127 16518122051920]; +$base = ord('A') - 1; # so that we can convert a number to a character as $char = chr($base + $number) + +# loop over tests +while ($test = shift @tests) { + + # analyse the input string + say qq[\nInput: $test]; + %answers = (); + analyse('', $test); + + # format and output the answers + $string = ''; + for $answer (sort keys %answers) { + $string .= $answer . ', '; + } + say qq[Output: ] . substr($string, 0, -2); +} + +sub analyse { + + my ($so_far, $test, $test_length, $first_two); + + # recursively tries taking the first 1 or 2 digits off test and adding them as a character to $so_far + + $so_far = $_[0]; # the character string so far + $test = $_[1]; # the remaining digit string + $test_length = length($test); + return if substr($test, 0, 1) eq '0'; # won't happen for a valid $test + + # take the first digit of $test and add the corresponding character to so_far + $so_far .= chr($base + substr($test, 0, 1)); + + # if anything remains in $test, analyse(the new $so_far, the rest of $test) + if ($test_length > 1) { + analyse($so_far, substr($test, 1)) if length($test) > 1; + + # else we've exhausted $test and found an answer + } else { + $answers{$so_far} = 1; + return; + } + + # if $test is >= 2 digits and they are 10-26 then add them as a character to $so_far + $so_far = $_[0]; + $first_two = substr($test, 0, 2); + if ($test_length >= 2 and $first_two ge '10' and $first_two le '26') { + $so_far .= chr($base + $first_two); + + # if anything remains in $test analyse(the new $so_far, the rest of $test) + if (length($test) > 2) { + analyse($so_far, substr($test, 2)); + + # else we've exhausted $test and found an answer + } else { + $answers{$so_far} = 1; + return; + } + } +} + + + + + + + + + + + + + + + |
