diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-10 18:14:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-10 18:14:48 +0000 |
| commit | 384f32f3ba7dac6e242ea873611e75cba2fa894b (patch) | |
| tree | 2426f0a1f6609d6b2478ea702649cee19d30cdaa /challenge-051 | |
| parent | d14da95baaed0aa7bda799c4de32128aeba2cd59 (diff) | |
| parent | f905bb2278adc5102dea12e5601337234c2a3f60 (diff) | |
| download | perlweeklychallenge-club-384f32f3ba7dac6e242ea873611e75cba2fa894b.tar.gz perlweeklychallenge-club-384f32f3ba7dac6e242ea873611e75cba2fa894b.tar.bz2 perlweeklychallenge-club-384f32f3ba7dac6e242ea873611e75cba2fa894b.zip | |
Merge pull request #1394 from adamcrussell/challenge-051
solution for challenge 051
Diffstat (limited to 'challenge-051')
| -rw-r--r-- | challenge-051/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-051/adam-russell/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-051/adam-russell/perl/ch-2.pl | 45 |
3 files changed, 81 insertions, 0 deletions
diff --git a/challenge-051/adam-russell/blog.txt b/challenge-051/adam-russell/blog.txt new file mode 100644 index 0000000000..8a76fcc5bf --- /dev/null +++ b/challenge-051/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/15423.html diff --git a/challenge-051/adam-russell/perl/ch-1.pl b/challenge-051/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..12405baddf --- /dev/null +++ b/challenge-051/adam-russell/perl/ch-1.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +## +# Given an array @L of integers. Write a script +# to find all unique triplets such that a + b + c is +# the same as the given target T. Also make sure a <= b <= c. +## +use AI::Prolog; +use constant TARGET => 0; +use constant NUMBERS => "-25, -10, -7, -3, 2, 4, 8, 10"; +my $prolog = do{ + local $/; + <DATA>; +}; +my $numbers = NUMBERS; +$prolog =~ s/NUMBER_LIST/$numbers/; +$prolog = new AI::Prolog($prolog); +$prolog->query("unique_triplets(X, Y, Z, " . TARGET . ")."); +my $result = $prolog->results; +my($x, $y, $z) = @{$result}[1 .. @{$result} - 1]; +print "X: $x\nY: $y\nZ: $z\n"; + +__DATA__ +member(X,[X|T]). +member(X,[H|T]) :- member(X,T). +numbers([NUMBER_LIST]). +unique_triplets(X, Y, Z, T) :- + numbers(L), + member(X, L), + member(Y, L), + member(Z, L), + X <= Y, + Y <= Z, + X <= Z, + T is X + Y + Z. diff --git a/challenge-051/adam-russell/perl/ch-2.pl b/challenge-051/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..29a4f8258f --- /dev/null +++ b/challenge-051/adam-russell/perl/ch-2.pl @@ -0,0 +1,45 @@ +use strict; +use warnings; +## +# Write a script to display all Colorful Numbers with 3 digits. +# A number can be declared a "Colorful Number" when +# all the products of consecutive subsets of the digits +# are different. +## +use AI::Prolog; +MAIN:{ + my $prolog = do{ + local $/; + <DATA>; + }; + $prolog = new AI::Prolog($prolog); + for my $n (100..999){ + my($x, $y, $z) = split(//, $n); + $prolog->query("colorful($x, $y, $z)."); + my $result = $prolog->results; + if($result){ + print "$n: colorful number\n"; + } + else{ + print "$n: not a colorful number\n"; + } + } +} + +__DATA__ +colorful(X, Y, Z) :- + A is X * Y, + B is Y * Z, + C is X * Y * Z, + X \= Y, + Y \= Z, + X \= Z, + X \= A, + X \= B, + X \= C, + Y \= A, + Y \= B, + Y \= C, + Z \= A, + Z \= B, + Z \= C. |
