diff options
| author | Adam Russell <ac.russell@live.com> | 2021-10-24 15:26:45 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2021-10-24 15:26:45 -0400 |
| commit | d653a46dd0cc61706f7c1c455aa4a21ddb3535f0 (patch) | |
| tree | 2f2e794828a384cd84e245e1799f622416e513c9 | |
| parent | 402866100f60f1c742cb1151fbbe1aeb5f6cac6b (diff) | |
| download | perlweeklychallenge-club-d653a46dd0cc61706f7c1c455aa4a21ddb3535f0.tar.gz perlweeklychallenge-club-d653a46dd0cc61706f7c1c455aa4a21ddb3535f0.tar.bz2 perlweeklychallenge-club-d653a46dd0cc61706f7c1c455aa4a21ddb3535f0.zip | |
initial commit
| -rw-r--r-- | challenge-135/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-135/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-135/adam-russell/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-135/adam-russell/perl/ch-2.pl | 55 | ||||
| -rw-r--r-- | challenge-135/adam-russell/prolog/ch-1.p | 25 | ||||
| -rw-r--r-- | challenge-135/adam-russell/prolog/ch-2.p | 73 |
6 files changed, 179 insertions, 0 deletions
diff --git a/challenge-135/adam-russell/blog.txt b/challenge-135/adam-russell/blog.txt new file mode 100644 index 0000000000..9f6c523296 --- /dev/null +++ b/challenge-135/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/24/perl diff --git a/challenge-135/adam-russell/blog1.txt b/challenge-135/adam-russell/blog1.txt new file mode 100644 index 0000000000..ab4d53b90b --- /dev/null +++ b/challenge-135/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/2021/10/24/prolog diff --git a/challenge-135/adam-russell/perl/ch-1.pl b/challenge-135/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..c4778b44a2 --- /dev/null +++ b/challenge-135/adam-russell/perl/ch-1.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +## +# You are given an integer. +# Write a script find out the middle 3-digits of the given +# integer, if possible, otherwise throw sensible error. +## +use POSIX; +sub middle_3{ + my($i) = @_; + $i = abs($i); + my $length = length($i); + return "even number of digits" if $length % 2 == 0; + return "too short" if $length < 3; + my $middle = ceil($length / 2); + return substr($i, $middle - 2, 3); +} + +MAIN:{ + print middle_3(1234567) . "\n"; + print middle_3(-123) . "\n"; + print middle_3(1) . "\n"; + print middle_3(10) . "\n"; +}
\ No newline at end of file diff --git a/challenge-135/adam-russell/perl/ch-2.pl b/challenge-135/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..9d4e5c2161 --- /dev/null +++ b/challenge-135/adam-russell/perl/ch-2.pl @@ -0,0 +1,55 @@ +use strict; +use warnings; +## +# You are given 7-characters alphanumeric SEDOL. +# Write a script to validate the given SEDOL. +# Print 1 if it is a valid SEDOL otherwise 0. +## +use boolean; + +sub is_sedol{ + my($sedol) = @_; + my $base = substr($sedol, 0, 6); + my $check_digit = substr($sedol, 6, 1); + ## + # check length + ## + return false if length($sedol) != 7; + ## + # check for alphanumerics only + ## + my $test_base = $base; + $test_base =~ tr/[0-9][B-Z]//d; + return false if $test_base; + ## + # confirm the check_digit + ## + return false if $check_digit != compute_check_digit($base); + ## + # all tests passed! + ## + return true; +} + +sub compute_check_digit{ + my($base) = @_; + my @chars = split(//, $base); + my @weights = (1, 3, 1, 7, 3, 9), + my $sum = 0; + do{ + my $c = ord(shift @chars); + if($c >= 66 && $c <= 90){ + $sum += (($c - 64 + 9) * shift @weights); + } + if($c >= 48 && $c <= 57){ + $sum += (($c - 48) * shift @weights); + } + }while(@chars); + return (10 - ($sum % 10)) % 10 +} + +MAIN:{ + print is_sedol(2936921) . "\n"; + print is_sedol(1234567) . "\n"; + print is_sedol("B0YBKL9") . "\n"; +}
\ No newline at end of file diff --git a/challenge-135/adam-russell/prolog/ch-1.p b/challenge-135/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..47dea728e3 --- /dev/null +++ b/challenge-135/adam-russell/prolog/ch-1.p @@ -0,0 +1,25 @@ +:-initialization(main). + +middle_three(N, Middle3):- + number_chars(N, Chars), + N > 0, + length(Chars, Length), + Length > 2, + IsOdd is Length mod 2, IsOdd == 1, + length(M3, 3), + PrefixLength is ceiling(Length / 2) - 2, + length(Prefix, PrefixLength), + append(Prefix, Middle, Chars), + append(M3, _, Middle), + number_chars(Middle3, M3). + +middle_three(N, Middle3):- + (N < 0, N0 is abs(N), middle_three(N0, Middle3)); + (number_chars(N, Chars), length(Chars, Length), Length < 3, format("too short~n", _)); + (number_chars(N, Chars), length(Chars, Length), IsOdd is Length mod 2, IsOdd == 0, format("even number of digits~n", _)); + middle_three(N, Middle3). + +main:- + middle_three(1234567, Middle3), + ((nonvar(Middle3), format("~d~n", [Middle3]), halt); + halt).
\ No newline at end of file diff --git a/challenge-135/adam-russell/prolog/ch-2.p b/challenge-135/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..c5361fc78f --- /dev/null +++ b/challenge-135/adam-russell/prolog/ch-2.p @@ -0,0 +1,73 @@ +:-initialization(main). + +weight(1, 1). +weight(2, 3). +weight(3, 1). +weight(4, 7). +weight(5, 3). +weight(6, 9). + +base --> alphanumeric, alphanumeric, alphanumeric, alphanumeric, alphanumeric, alphanumeric. +alphanumeric --> [AlphaNumeric], {letter_or_digit(AlphaNumeric)}. + +sedol(Sedol):- + var(Sedol), + sedol(_, Sedol). +sedol(Sedol):- + nonvar(Sedol), + length(Base, 6), + append(Base, [CheckDigit], Sedol), + phrase(base, Base), + compute_check(Base, ComputedCheckDigit), + CheckDigit == ComputedCheckDigit. + +sedol(Base, Sedol):- + phrase(base, Base), + check_digit(Base, Sedol). + +letter_or_digit(A):- + nonvar(A), + atom_codes(A, C), + ((C >= 66, C =< 90); + (C >= 48, C =< 57)). + +letter_or_digit(A):- + var(A), + ((between(66, 90, C)); %B through Z + (between(48, 57, C))), %0-9 + atom_codes(A, [C]). + +compute_check(Base, CheckSum):- + compute_check(Base, 1, CheckSum, 0). +compute_check([], _, CheckSum, PartialCheckSum):- + CheckSum is mod(10 - mod(PartialCheckSum, 10), 10). +compute_check([H|T], Index, CheckSum, PartialCheckSum):- + atom_codes(H, [C]), + between(66, 90, C), + weight(Index, Weight), + LetterValue is C - 64 + 9, + Partial is PartialCheckSum + (LetterValue * Weight), + succ(Index, I), + compute_check(T, I, CheckSum, Partial). +compute_check([H|T], Index, CheckSum, PartialCheckSum):- + atom_codes(H, [C]), + between(48, 57, C), + weight(Index, Weight), + NumeralValue is C - 48, + Partial is PartialCheckSum + (NumeralValue * Weight), + succ(Index, I), + compute_check(T, I, CheckSum, Partial). + +check_digit(Base, BaseCheckDigit):- + compute_check(Base, CheckDigit), + append(Base, [CheckDigit], BaseCheckDigit). + + +main:- + (sedol(['2','9','3','6','9','2',1]), format("1~n", _); + format("0~n", _)), + (sedol(['1','2','3','4','5','6',7]), format("1~n", _); + format("0~n", _)), + (sedol(['B','0','Y','B','K','L',9]), format("1~n", _); + format("0~n", _)), + halt.
\ No newline at end of file |
