diff options
| -rw-r--r-- | challenge-190/steve-g-lynn/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-190/steve-g-lynn/julia/ch-1.jl | 12 | ||||
| -rwxr-xr-x | challenge-190/steve-g-lynn/perl/ch-1.pl | 12 | ||||
| -rwxr-xr-x | challenge-190/steve-g-lynn/perl/ch-2.pl | 119 | ||||
| -rwxr-xr-x | challenge-190/steve-g-lynn/raku/ch-1.p6 | 16 | ||||
| -rwxr-xr-x | challenge-190/steve-g-lynn/raku/ch-2.p6 | 79 |
6 files changed, 239 insertions, 0 deletions
diff --git a/challenge-190/steve-g-lynn/blog.txt b/challenge-190/steve-g-lynn/blog.txt new file mode 100644 index 0000000000..70577d03ae --- /dev/null +++ b/challenge-190/steve-g-lynn/blog.txt @@ -0,0 +1 @@ +https://thiujiac.blogspot.com/2022/11/pwc-190.html diff --git a/challenge-190/steve-g-lynn/julia/ch-1.jl b/challenge-190/steve-g-lynn/julia/ch-1.jl new file mode 100755 index 0000000000..9dab9ddc50 --- /dev/null +++ b/challenge-190/steve-g-lynn/julia/ch-1.jl @@ -0,0 +1,12 @@ +#!/usr/bin/env julia + +function capital_detection( s::String ) ::Bool + myregex=r"^[A-Z]?[a-z]+$|^[A-Z]+$|^[a-z]+$" + return occursin( myregex, s) +end + +println("Perl: ", capital_detection("Perl")) +println("TPF: ", capital_detection("TPF")) +println("PyThon: ", capital_detection("PyThon")); +println("raku:", capital_detection("raku")); + diff --git a/challenge-190/steve-g-lynn/perl/ch-1.pl b/challenge-190/steve-g-lynn/perl/ch-1.pl new file mode 100755 index 0000000000..a5f49dc236 --- /dev/null +++ b/challenge-190/steve-g-lynn/perl/ch-1.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl + +print 'Perl:', &capital_detection('Perl'),"\n"; +print 'TPF:', &capital_detection('TPF'),"\n"; +print 'PyThon:', &capital_detection('PyThon'),"\n"; +print 'raku:', &capital_detection('raku'),"\n"; + +sub capital_detection { + my ($s)=@_; + ($s =~ /^[A-Z]?[a-z]+$|^[A-Z]+$|^[a-z]+$/) ? 1 : 0; +} + diff --git a/challenge-190/steve-g-lynn/perl/ch-2.pl b/challenge-190/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..fbfb4d727b --- /dev/null +++ b/challenge-190/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,119 @@ +#!/usr/bin/env perl + +#-- uses only perl 4 syntax +#-- ref: https://www.rexswain.com/perl4.html +{ + +local (%a2i,%i2a); #-- hashes to store codes +local $ctr; #-- counter for loops + +#-- create a lookup table to store the codes +#-- %i2a: 1 .. 26 => 'A' .. 'Z' +#-- %a2i: 'A' .. 'Z' => 1 .. 26 + +for $ctr (1 .. 26) { + $i2a{$ctr}=('A' .. 'Z')[$ctr-1]; + $a2i{$i2a{$ctr}}=$ctr; +} + +local *decoded_list = sub { + local ($s) = @_; + + #-- subsubs and lookup tables + + local *chk = sub { + #-- verify that a particular code matches $s + local (@code) = @_; + local ($ctr,$retval,@retval); + for $ctr (@code) { + push @retval, $a2i{$ctr}; + } + $retval = join //, @retval; + return ($retval == $s) + }; + + local %iterator; + #-- specifies the order of iterations for $s length 2,3 and 4 + #-- as a string to be eval'd in a loop + + $iterator{2}=q<('@ones[0..1]','@twos[0]')>; + $iterator{3}=q<('@ones[0..2]', + '($ones[0],$twos[1])', + '($twos[0],$ones[2])')>; + $iterator{4}=q<('@ones[0..3]', + '(@ones[0..1],$twos[2])', + '($twos[0],@ones[2..3])', + '($ones[0],$twos[1],$ones[3])', + '@twos[0,2]' + )>; + + #-- back to root sub decoded_list + + $s=~/^[1-9]+$/ || die "Only [1-9] allowed.\n"; + + (length($s) > 4) && die "For strings > 4 chars, use &slide_decoded_list\n"; + + (length($s)==1) && (return $i2a{$s}); + + local (@ones,@twos,@retval,$ctr); + + for $ctr (1 .. length($s)) { + push @ones, $i2a{substr($s,$ctr-1,1)}; + push @twos, $i2a{substr($s,$ctr-1,2)}; + } + pop @twos; + #@ones stores the codes for the digits 1 at a time + #@twos stores the codes for the digits 2 at a time + #with a null element for digit-pairs > 26 + + #iterate thru' the possibilities + + for $ctr (eval $iterator{length($s)}) { + local @s = eval $ctr; + ( &chk(@s) ) && ( push @retval, join //, @s); + } + + return sort {$a cmp $b} @retval; +}; + +local *slide_decoded_list = sub{ + #-- for longer input, return a list of + #-- stringified decode_list output for + #-- substrings taken four at a time, + #-- as substr($s,0,4),substr($s,3,4),substr($s,6,4)... + + #-- it makes more sense to slide through a long text + #-- decoding small overlapping chunks at a time + #-- rather than searching for all the combinations + #-- in the entire text viewed as a single string + + local ($s)=@_; + local ($ctr,@retval); + for ($ctr=0; $ctr < length($s); $ctr+=3 ) { + local @s=&decoded_list(substr($s,$ctr,4)); + push @retval, "(@s)"; + } + return @retval; +}; + +{ +local @test=&decoded_list('11'); +print "@test\n"; #AA K +} + +{ +local @test=&decoded_list('1115'); +print "@test\n"; #AAAE AAO AKE KAE KO +} + +{ +local @test=&decoded_list('127'); +print "@test\n"; #ABG LG +} + +{ +local @test=&slide_decoded_list(1115127); +print "@test\n"; +#(AAAE AAO AKE KAE KO) (EABG ELG) (G) +} +} diff --git a/challenge-190/steve-g-lynn/raku/ch-1.p6 b/challenge-190/steve-g-lynn/raku/ch-1.p6 new file mode 100755 index 0000000000..33a2ab34cc --- /dev/null +++ b/challenge-190/steve-g-lynn/raku/ch-1.p6 @@ -0,0 +1,16 @@ +#!/usr/bin/env perl6 + +say 'Perl:', &capital-detection('Perl'); +say 'TPF:', &capital-detection('TPF'); +say 'PyThon:', &capital-detection('PyThon'); +say 'raku:', &capital-detection('raku'); + +sub capital-detection( Str $s) { + ($s ~~ + /^ <[A .. Z]> ? <[ a .. z]> + $ || + ^ <[A .. Z]> + $ || + ^ <[a .. z]> +/) + ?? 1 + !! 0; +} + diff --git a/challenge-190/steve-g-lynn/raku/ch-2.p6 b/challenge-190/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..3e9f6a2036 --- /dev/null +++ b/challenge-190/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,79 @@ +#!/usr/bin/env perl6 + + +say &decode-list('8'); #H +say &decode-list('11'); #(AA K) +say &decode-list('127'); #(ABG LG) +say &decode-list('1115'); #(AAAE AAO AKE KAE KO) +say &decode-list('1115127'); +#[(AAAE AAO AKE KAE KO) (EABG ELG) G] + +multi sub decode-list( Str $s where + $s ~~ /^<[1..9]>+$/ && $s.chars==1) { + + return $s.trans([1 .. 9] => ['A' .. 'I']); + +} + +multi sub decode-list( Str $s where + $s ~~ /^<[1..9]>+$/ && $s.chars==2) { + + my @retval; + + @retval.append($s.trans([1 .. 9] => ['A' .. 'I'])); + + ($s <= 26) && @retval.append($s.trans([1..26] => ['A' .. 'Z'])); + + return @retval.sort; +} + +multi sub decode-list( Str $s where + $s ~~ /^<[1..9]>+$/ && $s.chars==3) { + + my @retval; + + @retval.append($s.trans([1 .. 9] => ['A' .. 'I'])); + + ($s.substr(1) <= 26) && @retval.append($s.substr(0,1).trans([1..9]=>['A'..'I']) ~ + $s.substr(1).trans([1..26] => ['A' .. 'Z'])); + + ($s.substr(0,2) <= 26) && @retval.append($s.substr(0,2).trans([1..26]=>['A'..'Z']) ~ + $s.substr(2).trans([1..9] => ['A' .. 'I'])); + + return @retval.sort; +} + +multi sub decode-list( Str $s where + $s ~~ /^<[1..9]>+$/ && $s.chars==4 ) { + + my @retval; + + @retval.append($s.trans([1 .. 9] => ['A' .. 'I'])); + + ($s.substr(2) <= 26) && @retval.append($s.substr(0,2).trans([1..9]=>['A'..'I']) ~ + $s.substr(2).trans([1..26] => ['A' .. 'Z'])); + + ($s.substr(0,2) <= 26) && @retval.append($s.substr(0,2).trans([1..26]=>['A'..'Z']) ~ + $s.substr(2).trans([1..9] => ['A' .. 'I'])); + + ($s.substr(1,2) <= 26) && @retval.append($s.substr(0,1).trans([1..9]=>['A'..'I']) ~ + $s.substr(1,2).trans([10..26]=>['J'..'Z']) ~ + $s.substr(3).trans([1..9] => ['A' .. 'I'])); + + (($s.substr(0,2) <= 26) && ($s.substr(2) <= 26)) && + @retval.append($s.trans([10..26]=>['J'..'Z'])); + + return @retval.sort; +} + +multi sub decode-list( Str $s where + $s ~~ /^<[1..9]>+$/ && $s.chars > 4 ) { + + my @retval; + + loop (my $ctr=0; $ctr <= $s.chars; $ctr+=3) { + push @retval, (decode-list($s.substr($ctr,4))); + } + + return @retval; +} |
