aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-29 23:39:56 +0000
committerGitHub <noreply@github.com>2023-01-29 23:39:56 +0000
commit71da4ee7b534b323f26b57f9f6d6467ecaa1cd92 (patch)
treee0e447939ce25b5ca76e22c876ac3f274525aac1
parent27ca0c94120618398fb577aad2d3e1a5adc9a888 (diff)
parent181cff4a2145e1f7a251b3e0bc971de0fc35f7f0 (diff)
downloadperlweeklychallenge-club-71da4ee7b534b323f26b57f9f6d6467ecaa1cd92.tar.gz
perlweeklychallenge-club-71da4ee7b534b323f26b57f9f6d6467ecaa1cd92.tar.bz2
perlweeklychallenge-club-71da4ee7b534b323f26b57f9f6d6467ecaa1cd92.zip
Merge pull request #7494 from adamcrussell/challenge-201
initial commit
-rw-r--r--challenge-201/adam-russell/blog.txt1
-rw-r--r--challenge-201/adam-russell/blog1.txt1
-rw-r--r--challenge-201/adam-russell/perl/ch-1.pl20
-rw-r--r--challenge-201/adam-russell/perl/ch-2.pl51
-rw-r--r--challenge-201/adam-russell/prolog/ch-1.p4
-rw-r--r--challenge-201/adam-russell/prolog/ch-2.p17
6 files changed, 94 insertions, 0 deletions
diff --git a/challenge-201/adam-russell/blog.txt b/challenge-201/adam-russell/blog.txt
new file mode 100644
index 0000000000..dfba13fc2f
--- /dev/null
+++ b/challenge-201/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2023/01/29 \ No newline at end of file
diff --git a/challenge-201/adam-russell/blog1.txt b/challenge-201/adam-russell/blog1.txt
new file mode 100644
index 0000000000..2329b72962
--- /dev/null
+++ b/challenge-201/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2023/01/29 \ No newline at end of file
diff --git a/challenge-201/adam-russell/perl/ch-1.pl b/challenge-201/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..62bd4d5970
--- /dev/null
+++ b/challenge-201/adam-russell/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use v5.36;
+##
+# You are given an array of unique numbers.
+# Write a script to find out all missing numbers
+# in the range 0..$n where $n is the array size.
+##
+use boolean;
+sub missing_numbers{
+ my @numbers = @_;
+ my %h;
+ do { $h{$_} = undef } for @numbers;
+ my @missing = grep { !exists($h{$_}) } 0 .. @numbers;
+ return @missing;
+}
+
+MAIN:{
+ say q/(/ . join(q/, /, missing_numbers(0, 1, 3)) . q/)/;
+ say q/(/ . join(q/, /, missing_numbers(0, 1)) . q/)/;
+ say q/(/ . join(q/, /, missing_numbers(0, 1, 2, 2)) . q/)/;
+} \ No newline at end of file
diff --git a/challenge-201/adam-russell/perl/ch-2.pl b/challenge-201/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..5775270fbb
--- /dev/null
+++ b/challenge-201/adam-russell/perl/ch-2.pl
@@ -0,0 +1,51 @@
+use v5.36;
+##
+# You are given an integer, $n > 0.
+# Write a script to determine the number of ways of
+# putting $n pennies in a row of piles of ascending
+# heights from left to right.
+##
+use AI::Prolog;
+use Hash::MultiKey;
+
+MAIN:{
+ my $S = $ARGV[0];
+ my $C = "[" . $ARGV[1] . "]";
+
+ my $prolog = do{
+ local $/;
+ <DATA>;
+ };
+ $prolog =~ s/_COINS_/$C/g;
+ $prolog =~ s/_SUM_/$S/g;
+ $prolog = AI::Prolog->new($prolog);
+ $prolog->query("sum(Coins).");
+ my %h;
+ tie %h, "Hash::MultiKey";
+ while(my $result = $prolog->results){
+ my @s = sort @{$result->[1]};
+ $h{\@s} = undef;
+ }
+ for my $k ( sort { @{$b} <=> @{$a} } keys %h){
+ print "(" . join(",", @{$k}) . ")";
+ print "\n";
+ }
+}
+
+__DATA__
+member(X,[X|_]).
+member(X,[_|T]) :- member(X,T).
+
+coins(_COINS_).
+
+sum(Coins):-
+ sum([], Coins, 0).
+
+sum(Coins, Coins, _SUM_).
+
+sum(Partial, Coins, Sum):-
+ Sum < _SUM_,
+ coins(L),
+ member(X,L),
+ S is Sum + X,
+ sum([X | Partial], Coins, S). \ No newline at end of file
diff --git a/challenge-201/adam-russell/prolog/ch-1.p b/challenge-201/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..8d16c15a6b
--- /dev/null
+++ b/challenge-201/adam-russell/prolog/ch-1.p
@@ -0,0 +1,4 @@
+missing_number(Numbers, Missing):-
+ length(Numbers, NumbersLength),
+ between(0, NumbersLength, Missing),
+ \+ member(Missing, Numbers). \ No newline at end of file
diff --git a/challenge-201/adam-russell/prolog/ch-2.p b/challenge-201/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..2e20c312d3
--- /dev/null
+++ b/challenge-201/adam-russell/prolog/ch-2.p
@@ -0,0 +1,17 @@
+sum(Coins):-
+ sum([], Coins, 0).
+
+sum(Coins, Coins, 5).
+
+sum(Partial, Coins, Sum):-
+ Sum < 5,
+ between(1, 5, X),
+ S is Sum + X,
+ sum([X | Partial], Coins, S).
+
+main:-
+ findall(Coins, sum(Coins), C),
+ maplist(msort, C, CS),
+ sort(CS, CoinsSorted),
+ write(CoinsSorted), nl,
+ halt. \ No newline at end of file