aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-21 02:03:42 +0000
committerGitHub <noreply@github.com>2022-11-21 02:03:42 +0000
commit77acfbb32c3e5d0f0d2e041961f1249cb121e7f1 (patch)
tree2aa7ce116b62b931e44c1cc06afd3102eb2dfa62
parentb391220b5056a71a4b0e99a0fb99ab7ce9d748ad (diff)
parentb5791fac00294ed02e86dd60f70cd0416f66ebfd (diff)
downloadperlweeklychallenge-club-77acfbb32c3e5d0f0d2e041961f1249cb121e7f1.tar.gz
perlweeklychallenge-club-77acfbb32c3e5d0f0d2e041961f1249cb121e7f1.tar.bz2
perlweeklychallenge-club-77acfbb32c3e5d0f0d2e041961f1249cb121e7f1.zip
Merge pull request #7119 from adamcrussell/challenge-191
Challenge 191
-rw-r--r--challenge-191/adam-russell/blog.txt1
-rw-r--r--challenge-191/adam-russell/blog1.txt1
-rw-r--r--challenge-191/adam-russell/perl/ch-1.pl30
-rw-r--r--challenge-191/adam-russell/perl/ch-2.pl40
-rw-r--r--challenge-191/adam-russell/prolog/ch-1.p17
-rw-r--r--challenge-191/adam-russell/prolog/ch-2.p11
6 files changed, 100 insertions, 0 deletions
diff --git a/challenge-191/adam-russell/blog.txt b/challenge-191/adam-russell/blog.txt
new file mode 100644
index 0000000000..22ccf20284
--- /dev/null
+++ b/challenge-191/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/11/20 \ No newline at end of file
diff --git a/challenge-191/adam-russell/blog1.txt b/challenge-191/adam-russell/blog1.txt
new file mode 100644
index 0000000000..6a46aadfe3
--- /dev/null
+++ b/challenge-191/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/11/20 \ No newline at end of file
diff --git a/challenge-191/adam-russell/perl/ch-1.pl b/challenge-191/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..aa6095c94b
--- /dev/null
+++ b/challenge-191/adam-russell/perl/ch-1.pl
@@ -0,0 +1,30 @@
+use v5.36;
+use strict;
+use warnings;
+##
+# You are given list of integers, @list. Write a script to find out whether the largest
+# item in the list is at least twice as large as each of the other items.
+##
+use boolean;
+
+sub twice_largest{
+ my(@list_integers) = @_;
+ my $twice_max = -1;
+ for my $i (0 .. @list_integers - 1){
+ my $twice_rest = true;
+ for my $j (0 .. @list_integers - 1){
+ unless($i == $j){
+ $twice_rest = $list_integers[$i] >= 2 * $list_integers[$j];
+ }
+ }
+ $twice_max = $list_integers[$i] if $twice_rest;
+ }
+ return $twice_max>-1?1:-1;
+}
+
+MAIN:{
+ say twice_largest(1, 2, 3, 4);
+ say twice_largest(1, 2, 0, 5);
+ say twice_largest(2, 6, 3, 1);
+ say twice_largest(4, 5, 2, 3);
+} \ No newline at end of file
diff --git a/challenge-191/adam-russell/perl/ch-2.pl b/challenge-191/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..5964e231bb
--- /dev/null
+++ b/challenge-191/adam-russell/perl/ch-2.pl
@@ -0,0 +1,40 @@
+use v5.36;
+use strict;
+use warnings;
+##
+# You are given an integer, 0 < $n <= 15.
+# Write a script to find the number of orderings of numbers that form a cute list.
+##
+use Hash::MultiKey;
+
+sub cute_list{
+ my($n) = @_;
+ my %cute;
+ tie %cute, "Hash::MultiKey";
+ for my $i (1 .. $n){
+ $cute{[$i]} = undef;
+ }
+ my $i = 1;
+ {
+ $i++;
+ my %cute_temp;
+ tie %cute_temp, "Hash::MultiKey";
+ for my $j (1 .. $n){
+ for my $cute (keys %cute){
+ if(0 == grep {$j == $_} @{$cute}){
+ if(0 == $j % $i || 0 == $i % $j){
+ $cute_temp{[@{$cute}, $j]} = undef;
+ }
+ }
+ }
+ }
+ %cute = %cute_temp;
+ untie %cute_temp;
+ redo unless $i == $n;
+ }
+ return keys %cute;
+}
+
+MAIN:{
+ say cute_list(15) . q//;
+} \ No newline at end of file
diff --git a/challenge-191/adam-russell/prolog/ch-1.p b/challenge-191/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..e6f1c443e8
--- /dev/null
+++ b/challenge-191/adam-russell/prolog/ch-1.p
@@ -0,0 +1,17 @@
+twice_greater(X, Y, TwiceGreater):-
+ X \== Y,
+ TwiceY is 2 * Y,
+ X >= TwiceY,
+ TwiceGreater = -1.
+twice_greater(X, Y, TwiceGreater):-
+ TwiceY is 2 * Y,
+ X < TwiceY,
+ TwiceGreater = 1.
+
+twice_largest(List):-
+ max_list(List, Max),
+ maplist(twice_greater(Max), List, TwiceGreater),
+ delete(TwiceGreater, -1, TwiceGreaterOneDeleted),
+ length(TwiceGreaterOneDeleted, TwiceGreaterOneDeletedLength),
+ TwiceGreaterOneDeletedLength == 1, !.
+ \ No newline at end of file
diff --git a/challenge-191/adam-russell/prolog/ch-2.p b/challenge-191/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..6711b4d1ff
--- /dev/null
+++ b/challenge-191/adam-russell/prolog/ch-2.p
@@ -0,0 +1,11 @@
+cute(_, _) --> [].
+cute(N, CuteList) --> [X], {between(1, N, X), \+ member(X, CuteList),
+ append(CuteList, [X], CuteListUpdated),
+ nth(I, CuteListUpdated, X),
+ 0 is mod(X, I)},
+ cute(N, CuteListUpdated).
+cute(N, CuteList) --> [X], {between(1, N, X), \+ member(X, CuteList),
+ append(CuteList, [X], CuteListUpdated),
+ nth(I, CuteListUpdated, X),
+ 0 is mod(I, X)},
+ cute(N, CuteListUpdated). \ No newline at end of file