diff options
| -rw-r--r-- | challenge-190/adam-russell/blog.txt | 0 | ||||
| -rw-r--r-- | challenge-190/adam-russell/blog1.txt | 0 | ||||
| -rw-r--r-- | challenge-190/adam-russell/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-190/adam-russell/perl/ch-2.pl | 69 | ||||
| -rw-r--r-- | challenge-190/adam-russell/prolog/ch-1.p | 25 | ||||
| -rw-r--r-- | challenge-190/adam-russell/prolog/ch-2.p | 40 |
6 files changed, 167 insertions, 0 deletions
diff --git a/challenge-190/adam-russell/blog.txt b/challenge-190/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-190/adam-russell/blog.txt diff --git a/challenge-190/adam-russell/blog1.txt b/challenge-190/adam-russell/blog1.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-190/adam-russell/blog1.txt 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){ + my $index = 0; + my $encoded = []; + for my $i (@{$pattern}){ + push @{$encoded}, substr($s, $index, $i); + $index += $i; + } + push @encoded, $encoded if 0 == grep { $_ > 26 } @{$encoded}; + } + @decoded = sort { $a cmp $b } map { join("", map { $alphabet[$_ - 1] } @{$_}) } @encoded; +} + +MAIN:{ + init_prolog; + say join(", ", decoded_list(11)); + say join(", ", decoded_list(1115)); + say join(", ", decoded_list(127)); +} + +__DATA__ +member(X,[X|_]). +member(X,[_|T]) :- member(X,T). + +digits([1, 2]). + +sum(Digits):- + sum([], Digits, 0). + +sum(Digits, Digits, _LENGTH_). + +sum(Partial, Digits, Sum):- + Sum < _LENGTH_, + digits(L), + member(X,L), + S is Sum + X, + sum([X | Partial], Digits, S).
\ No newline at end of file diff --git a/challenge-190/adam-russell/prolog/ch-1.p b/challenge-190/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..5bfe30ce57 --- /dev/null +++ b/challenge-190/adam-russell/prolog/ch-1.p @@ -0,0 +1,25 @@ +all_small([]). +all_small([H|T]):- + H >= 97, + H =< 122, + all_small(T). + +all_capitals([]). +all_capitals([H|T]):- + H >= 65, + H =< 90, + all_capitals(T). + +capital_detection([]). +capital_detection([H|T]):- + H >= 65, + H =< 90, + all_capitals(T). +capital_detection([H|T]):- + H >= 65, + H =< 90, + all_small(T). +capital_detection([H|T]):- + H >= 97, + H =< 122, + all_small(T).
\ No newline at end of file diff --git a/challenge-190/adam-russell/prolog/ch-2.p b/challenge-190/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..b9c846fb26 --- /dev/null +++ b/challenge-190/adam-russell/prolog/ch-2.p @@ -0,0 +1,40 @@ +alphabet(1, 'A'). +alphabet(2, 'B'). +alphabet(3, 'C'). +alphabet(4, 'D'). +alphabet(5, 'E'). +alphabet(6, 'F'). +alphabet(7, 'G'). +alphabet(8, 'H'). +alphabet(9, 'I'). +alphabet(10,'J'). +alphabet(11, 'K'). +alphabet(12, 'L'). +alphabet(13, 'M'). +alphabet(14, 'N'). +alphabet(15, 'O'). +alphabet(16, 'P'). +alphabet(17, 'Q'). +alphabet(18, 'R'). +alphabet(19, 'S'). +alphabet(20, 'T'). +alphabet(21, 'U'). +alphabet(22, 'V'). +alphabet(23, 'W'). +alphabet(24, 'X'). +alphabet(25, 'Y'). +alphabet(26, 'Z'). + +digits([1, 2]). + +sum(Digits):- + sum([], Digits, 0). + +sum(Digits, Digits, _LENGTH_). + +sum(Partial, Digits, Sum):- + Sum < _LENGTH_, + digits(L), + member(X,L), + S is Sum + X, + sum([X | Partial], Digits, S).
\ No newline at end of file |
