diff options
| author | dcw <d.white@imperial.ac.uk> | 2022-11-20 20:38:56 +0000 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2022-11-20 20:38:56 +0000 |
| commit | 3956a95b61015a8abb5aab29015ea85e594877e0 (patch) | |
| tree | 699b2a0c2181d7c50510c33954598e640187e54d /challenge-190 | |
| parent | e2232acaf7deca62a2551afe032cdef96055cae5 (diff) | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.tar.gz perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.tar.bz2 perlweeklychallenge-club-3956a95b61015a8abb5aab29015ea85e594877e0.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-190')
67 files changed, 3346 insertions, 2 deletions
diff --git a/challenge-190/0rir/raku/ch-1.raku b/challenge-190/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..e9e8c344b6 --- /dev/null +++ b/challenge-190/0rir/raku/ch-1.raku @@ -0,0 +1,118 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +190-1: Capital Detection Submitted by: Mohammad S Anwar + +Given a string with alphabetic characters only: A..Z and a..z. + +Determine if the usage of capitals is legal by complying with one +of the following rules, and return 0 or the rule number satisfied: + +1) Only first letter is capital and all others are small. +2) Every letter is small. +3) Every letter is capital. + +Example 1 +Input: $s = 'Perl' +Output: 1 +Example 2 +Input: $s = 'TPF' +Output: 1 +Example 3 +Input: $s = 'PyThon' +Output: 0 +Example 4 +Input: $s = 'raku' +Output: 1 +=end comment + +=begin comment +Often when doing a weekly challenge, I choose some scenario of usage to +satisfy rather than just fulfilling the bare challenge reqs. + +This time, I have just imagined this requirement is of great import, has +an undisclosed purpose, and should play well with NIH code. So there is +some effort to resolve the upper-case vs. title-case ambiguity, and to +keep the configuration from polluting other namespaces. +=end comment + + +# %*ENV<ITUC-prefer> = Int; # Set to 3 for UPPER, 1 for TITLE, + + +# NOTE: ?! Keeping enum scope narrow for Int namespace collision avoidance. +enum Rule < FAIL-CASE TITLE-CASE LOWER-CASE UPPER-CASE>; + +sub is-title-or-a-uniform-case( + Any:D(Str) $s, + Int $ITUC-prefer = %*ENV<ITUC-prefer>; + --> Int +) { + unless $ITUC-prefer == (UPPER-CASE, TITLE-CASE).any { + warn + "WARNING: Default for title- vs upper-case preference has not been set\n" +~ " locally. Using value for Timbuktu, Mali. It is recommended to\n" +~ " set \%*ENV<ITUC-prefer> or parameter \$ITUC-prefer." ; + $ITUC-prefer = UPPER-CASE; + } + given $s { + when / ^ <:Lu> $ / { return $ITUC-prefer } + when / ^ <:Ll>+ $ / { return LOWER-CASE } + when / ^ <:Lu>+ $ / { return UPPER-CASE } + when / ^ <:Lu> <:Ll>+ $ / { return TITLE-CASE } + when / ^ <:L>+ $ / { return FAIL-CASE } + default { + die "I could handle this if my programmer had been smarter: '$s'"; + } + } +} + +multi MAIN ( ) { MAIN('T' ) } + +multi MAIN ( $t where * = < T t>.any ) { + + my @Test = + { s => 'U' , upper => UPPER-CASE, title => TITLE-CASE }, + { s => 'Raku' , upper => TITLE-CASE, title => TITLE-CASE }, + { s => 'RAKU' , upper => UPPER-CASE, title => UPPER-CASE }, + { s => 'raku' , upper => LOWER-CASE, title => LOWER-CASE }, + { s => 'Ul' , upper => TITLE-CASE, title => TITLE-CASE }, + { s => 'UU' , upper => UPPER-CASE, title => UPPER-CASE }, + { s => 'l' , upper => LOWER-CASE, title => LOWER-CASE }, + { s => 'll' , upper => LOWER-CASE, title => LOWER-CASE }, + { s => 'Perl' , upper => TITLE-CASE, title => TITLE-CASE }, + { s => 'lUlU' , upper => FAIL-CASE, title => FAIL-CASE }, + { s => 'TPF' , upper => UPPER-CASE, title => UPPER-CASE }, + { s => 'PyThon', upper => FAIL-CASE, title => FAIL-CASE }, + { s => 'pY' , upper => FAIL-CASE, title => FAIL-CASE }, +=begin comment +=end comment + ; + + my @Dead = + { s => 'Ra ku', why => 'non-Letters'}, + { s => 'Ra0ku', why => 'non-Letters'}, + { s => '' , why => 'empty Str'}, + { s => (Str) , why => 'undefined' }, + ; + + plan @Dead + +@Test × 2; + for @Dead -> %t { + %*ENV<TUC-prefer> = UPPER-CASE; + dies-ok { is-title-or-a-uniform-case( %t<s>) }, + "dies: %t<why>, " + ~ ( %t<s>.defined ?? "'%t<s>'" !! %t<s>.raku); + } + for @Test -> %t { + is is-title-or-a-uniform-case( %t<s>, TITLE-CASE), %t<title>, + "%t<s> --> %t<title>"; + is is-title-or-a-uniform-case( %t<s>, UPPER-CASE), %t<upper>, + "%t<s> --> %t<upper>"; + } + done-testing; +} + diff --git a/challenge-190/0rir/raku/ch-2.raku b/challenge-190/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..718825c4af --- /dev/null +++ b/challenge-190/0rir/raku/ch-2.raku @@ -0,0 +1,108 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; +use experimental :macros; + + +=begin comment +190-2: Decoded List Submitted by: Mohammad S Anwar + +Given a string, $s, consisting of a sequence of the decimal chars 1 to 9. +Find the all valid different decodings for the given order. +DEcoding is done by mapping A,B,C,D,… to 1,2,3,4,… etc. + +Example 2 +Input: $s = 1115 +Output: AAAE, AAO, AKE, KAE, KO + +Possible decoded data are: +(1 1 1 5) => (AAAE) +(1 1 15) => (AAO) +(1 11 5) => (AKE) +(11 1 5) => (KAE) +(11 15) => (KO) +=end comment + +macro n64chr( $n2chr) { quasi { ( {{{ $n2chr }}} + 64).chr} }; + +macro two2one( $a, $b ) { + quasi { ( {{{$a}}} ~ {{{$b}}}).Int < 27 + ?? ( {{{$a}}} ~ {{{$b}}}) + !! Str } +} + +# Return an array of decoded values, if $justify is set return an +# array contain the numeric values as shown above in the example, +# otherwise return the complete decoding. +sub decoder-str-NtoChar( Str:D $in, Bool:D :$justify = False --> Array ) { + my @in = $in.comb; + my @num; + @num[0].push: @in.shift; + for @in -> $n { + for 0 .. @num.end -> $i { + when not @num[$i][*-1].defined { + @num[$i].push: $n; # suffix it + } + if my $tabby = two2one( @num[$i][*-1], $n ) { + my @a = | @num[$i], Str; + @a[*-2] = $tabby; + @num.push: @a; + } + @num[$i].push: $n; + } + } + @num = sort do for @num -> @a { @a.=grep: *.defined; } + + return @num if $justify; + + my @ret = gather for @num -> @a { + take @a.map( { n64chr( $_) } ).join; + } + + return @ret; +} + +multi MAIN ( Str $t where * = < T t>.any ) { + + my @Test = + { in => '11' , exp => [ < AA K >], }, + { in => '1115' , exp => [ < AAAE AAO AKE KAE KO >], }, + { in => '127' , exp => [ < ABG LG >], }, + { in => '34567893' , exp => [ < CDEFGHIC > ] }, + { in => '111111' , exp => [< + AAAAAA AAAAK AAAKA AAKAA AAKK AKAAA AKAK AKKA + KAAAA KAAK KAKA KKAA KKK >], }, + { in => '1324152617' , exp => [ < + ACBDAEBFAG ACBDAEBFQ ACBDAEZAG ACBDAEZQ + ACBDOBFAG ACBDOBFQ ACBDOZAG ACBDOZQ + ACXAEBFAG ACXAEBFQ ACXAEZAG ACXAEZQ + ACXOBFAG ACXOBFQ ACXOZAG ACXOZQ + MBDAEBFAG MBDAEBFQ MBDAEZAG MBDAEZQ + MBDOBFAG MBDOBFQ MBDOZAG MBDOZQ + MXAEBFAG MXAEBFQ MXAEZAG MXAEZQ + MXOBFAG MXOBFQ MXOZAG MXOZQ > ] }, + { in => '223162' , exp => + [< BBCAFB BBCPB BWAFB BWPB VCAFB VCPB >], }, + ; + + plan +@Test; + for @Test -> %t { + is-deeply decoder-str-NtoChar( %t<in>), @(%t<exp>), + "%t<in> -> @(%t<exp>)"; + } + done-testing; +} + +multi MAIN( $d = '223162' ) { + my @answer = decoder-str-NtoChar( $d); + my @num = decoder-str-NtoChar( $d, :justify); + die "Wrong! @answer" + unless @answer ~~ [< BBCAFB BBCPB BWAFB BWPB VCAFB VCPB >]; + say "Input: \$s = $d\nOutput: @answer[].join(', ')\n\nThe decoded data:"; + for ^@answer.elems -> $i { + printf "(%-11s) => %-7s\n", @num[$i], @answer[$i]; + } +} + + diff --git a/challenge-190/LoneWolfiNTj/perl/ch-1.pl b/challenge-190/LoneWolfiNTj/perl/ch-1.pl new file mode 100755 index 0000000000..2c065c5a0d --- /dev/null +++ b/challenge-190/LoneWolfiNTj/perl/ch-1.pl @@ -0,0 +1,42 @@ +#! /usr/bin/env perl + +# PWCC_190_P1_Capital-Correctness_Robbie-Hatley.pl + +=pod + +Task 1: Capital Detection +Submitted by: Mohammad S Anwar +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 is appropriate. +It's 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. + +=cut + +# NOTE: Input is via built-in-array (default) or CLI. If using CLI, +# input should be space-separated sequence of unquoted clusters +# of English letters (/[a-zA-Z]+/). + +# NOTE: Output will be to stdout and will consist of a 0 or 1 for +# each letter cluster, 0 meaning "inappropriate capital use" +# and 1 meaning "appropriate capital use". + +use v5.36; + +sub appropriate ($string) +{ + $string =~ m/(^[A-Z][a-z]*$)|(^[a-z]+$)|(^[A-Z]+$)/ and return 1 + or return 0; +} + +my @array = qw( Perl TPF PyThon raku ); +if (scalar(@ARGV)>0) {@array = @ARGV} + +for (@array) +{ + my $a = appropriate($_); + $a and say "$a - capitalization of $_ is correct." + or say "$a - capitalization of $_ is incorrect."; +} diff --git a/challenge-190/LoneWolfiNTj/perl/ch-2.pl b/challenge-190/LoneWolfiNTj/perl/ch-2.pl new file mode 100755 index 0000000000..4c48ebf09c --- /dev/null +++ b/challenge-190/LoneWolfiNTj/perl/ch-2.pl @@ -0,0 +1,97 @@ +#! /usr/bin/env perl + +# PWCC_190_P2_Decoded-List_Robbie-Hatley.pl + +=pod + +You are given an encoded string consisting of a sequence of +numeric characters (/[0-9]+/). Encoding is simply done by mapping +A,B,C,D,... to 1,2,3,4,... etc. Write a script to find the all +valid different decodings in sorted order. + +=cut + +# NOTE: Input is via built-in-array (default) or CLI. If using CLI, +# input should be space-separated sequence of unquoted positive +# integers (/^[1-9][0-9]*$/). + +# NOTE: Output will be to stdout and will consist of a sorted List +# of all possible decodings using partitions of each +# digit cluster into numbers. + +use v5.36; + +sub is_positive_integer ($s) +{ + $s =~ m/^[1-9][0-9]*$/ and return 1 + or return 0; +} + +sub are_all_positive_integers_1_26 (@a) +{ + for (@a) + { + return 0 unless is_positive_integer $_ && $_ >= 1 && $_ <= 26; + } + return 1; +} + +# Return an array of refs to arrays of all possible partitionings +# of a string into substrings: +sub string_partitions ($string) +{ + my @partitions; + my $size = length($string); + if ( 0 == $size ) + { + @partitions = ([]); + } + else + { + my ($first, $second); + for ( my $part = 1 ; $part <= $size ; ++$part ) + { + $first = substr($string, 0, $part ); + $second = substr($string, $part, $size - $part); + my @partials = string_partitions($second); + for (@partials) {unshift @$_, $first;} + push @partitions, @partials; + } + } + return @partitions; +} + +# Decode an array of positives integers, 1-26, into a text string: +sub decode (@a) +{ + return 'DecodeError' unless are_all_positive_integers_1_26(@a); + my $s = ''; # output string + foreach my $o (@a){$s.=chr(64+$o);} + return $s; +} + +# Default input: +my @array = qw( 11 1115 127 ); + +# Non-Default input: +if (scalar(@ARGV)>0) {@array = @ARGV} + +# Set-up output to be ', '-separated: +$,=', '; + +foreach my $s (@array) +{ + not is_positive_integer $s # If an input is invalid, + and say "$s is not a positive integer" # alert user + and next; # and skip that input. + my @d = string_partitions($s); # Array of refs to arrays of all possible partitionings of $s (HARD!!!). + my $e; # One possible decoding of $s + my @e; # Array of all decodings of $s + for my $ar (@d) + { # For each partitioning of $s, + $e = decode(@$ar); # get the corresponding decoding, + push(@e,$e) # and push it onto @e, + unless $e eq 'DecodeError'; # unless a decode error occurred. + } + say sort @e; # Print sorted list of valid decodings. +} diff --git a/challenge-190/adam-russell/blog.txt b/challenge-190/adam-russell/blog.txt new file mode 100644 index 0000000000..c17ccf2493 --- /dev/null +++ b/challenge-190/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/11/13
\ No newline at end of file diff --git a/challenge-190/adam-russell/blog1.txt b/challenge-190/adam-russell/blog1.txt new file mode 100644 index 0000000000..5d701ee6ea --- /dev/null +++ b/challenge-190/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/11/13
\ No newline at end of file diff --git a/challenge-190/adam-russell/perl/ch-1.pl b/challenge-190/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..25a37bd50f --- /dev/null +++ b/challenge-190/adam-russell/perl/ch-1.pl @@ -0,0 +1,33 @@ +use v5.36; +use strict; +use warnings; +## +# 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 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. +## +use boolean; + +sub capital_detection{ + {my($s) = @_; return true if length($s) == $s =~ tr/A-Z//d;} + {my($s) = @_; return true if length($s) == $s =~ tr/a-z//d;} + { + my($s) = @_; + $s =~ m/(^.{1})(.*)$/; + my $first_letter = $1; + my $rest_letters = $2; + return true if $first_letter =~ tr/A-Z//d == 1 && + length($rest_letters) == $rest_letters =~ tr/a-z//d; + } + return false; +} + +MAIN:{ + say capital_detection(q/Perl/); + say capital_detection(q/TPF/); + say capital_detection(q/PyThon/); + say capital_detection(q/raku/); +}
\ No newline at end of file diff --git a/challenge-190/adam-russell/perl/ch-2.pl b/challenge-190/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..1d9cd58cc4 --- /dev/null +++ b/challenge-190/adam-russell/perl/ch-2.pl @@ -0,0 +1,69 @@ +use v5.36; +use strict; +use warnings; +## +# You are given an encoded string consisting of a sequence $s of numeric characters: 0..9. +# Write a script to find the all valid different decodings in sorted order. +## +use AI::Prolog; +use Hash::MultiKey; + +my $prolog_code; +sub init_prolog{ + $prolog_code = do{ + local $/; + <DATA>; + }; +} + +sub decoded_list{ + my($s) = @_; + my $prolog = $prolog_code; + my @alphabet = qw/A B C D E F G H I J K L M N O P Q R S T U V W X Y Z/; + my @encoded; + my @decoded; + my $length = length($s); + $prolog =~ s/_LENGTH_/$length/g; + $prolog = AI::Prolog->new($prolog); + $prolog->query("sum(Digits)."); + my %h; + tie %h, "Hash::MultiKey"; + while(my $result = $prolog->results){ + $h{$result->[1]} = undef; + } + for my $pattern (keys %h){ |
