aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-19 22:44:13 +0000
committerGitHub <noreply@github.com>2023-11-19 22:44:13 +0000
commit368f67627faf48d64c487471220dba586834cbc3 (patch)
tree0b898b41356a324baa124485b37bcbf41263e626
parentbbc9d0eba96994dbe80ceb3a69df3816cc60b09a (diff)
parent0095202048c74c12cda0ed73532cffe5bd0ff403 (diff)
downloadperlweeklychallenge-club-368f67627faf48d64c487471220dba586834cbc3.tar.gz
perlweeklychallenge-club-368f67627faf48d64c487471220dba586834cbc3.tar.bz2
perlweeklychallenge-club-368f67627faf48d64c487471220dba586834cbc3.zip
Merge pull request #9096 from adamcrussell/challenge-243
initial commit
-rw-r--r--challenge-243/adam-russell/.gitignore3
-rw-r--r--challenge-243/adam-russell/blog.txt1
-rw-r--r--challenge-243/adam-russell/blog1.txt1
-rw-r--r--challenge-243/adam-russell/javascript/ch-1.js19
-rw-r--r--challenge-243/adam-russell/javascript/ch-2.js18
-rw-r--r--challenge-243/adam-russell/perl/ch-1.pl27
-rw-r--r--challenge-243/adam-russell/perl/ch-2.pl28
-rw-r--r--challenge-243/adam-russell/prolog/ch-1.p8
-rw-r--r--challenge-243/adam-russell/prolog/ch-2.p11
-rw-r--r--challenge-243/adam-russell/r/ch-1.r25
-rw-r--r--challenge-243/adam-russell/r/ch-2.r23
11 files changed, 164 insertions, 0 deletions
diff --git a/challenge-243/adam-russell/.gitignore b/challenge-243/adam-russell/.gitignore
new file mode 100644
index 0000000000..d4e9a94d5e
--- /dev/null
+++ b/challenge-243/adam-russell/.gitignore
@@ -0,0 +1,3 @@
+*.bbprojectd
+.RData
+.Rhistory
diff --git a/challenge-243/adam-russell/blog.txt b/challenge-243/adam-russell/blog.txt
new file mode 100644
index 0000000000..b0e8e4e7a8
--- /dev/null
+++ b/challenge-243/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/perl/2023/11/19 \ No newline at end of file
diff --git a/challenge-243/adam-russell/blog1.txt b/challenge-243/adam-russell/blog1.txt
new file mode 100644
index 0000000000..dd30862401
--- /dev/null
+++ b/challenge-243/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/prolog/2023/11/19 \ No newline at end of file
diff --git a/challenge-243/adam-russell/javascript/ch-1.js b/challenge-243/adam-russell/javascript/ch-1.js
new file mode 100644
index 0000000000..929338a160
--- /dev/null
+++ b/challenge-243/adam-russell/javascript/ch-1.js
@@ -0,0 +1,19 @@
+class Ch1{
+ reversePairs(integers){
+ let r = [];
+ for(let i = 0; i < integers.length; i++){
+ for(let j = i + 1; j < integers.length; j++){
+ if(integers[i] > integers[j] + integers[j])
+ r.push([i, j]);
+ }
+ }
+ return r.length;
+ }
+}
+let ch1 = new Ch1();
+console.log(
+ ch1.reversePairs([1, 3, 2, 3, 1])
+);
+console.log(
+ ch1.reversePairs([2, 4, 3, 5, 1])
+); \ No newline at end of file
diff --git a/challenge-243/adam-russell/javascript/ch-2.js b/challenge-243/adam-russell/javascript/ch-2.js
new file mode 100644
index 0000000000..ef2fdcd56f
--- /dev/null
+++ b/challenge-243/adam-russell/javascript/ch-2.js
@@ -0,0 +1,18 @@
+class Ch2{
+ floorSum(integers){
+ let floorSum = 0;
+ integers.forEach(function(x){
+ integers.forEach(function(y){
+ floorSum += Math.floor(x / y)
+ });
+ });
+ return floorSum;
+ }
+}
+let ch2 = new Ch2();
+console.log(
+ ch2.floorSum([2, 5, 9])
+);
+console.log(
+ ch2.floorSum([7, 7, 7, 7, 7, 7, 7])
+); \ No newline at end of file
diff --git a/challenge-243/adam-russell/perl/ch-1.pl b/challenge-243/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..9f2093359a
--- /dev/null
+++ b/challenge-243/adam-russell/perl/ch-1.pl
@@ -0,0 +1,27 @@
+use v5.38;
+##
+# You are given an array of integers. Write a script to return the
+# number of reverse pairs in the given array. A reverse pair is:
+# a pair (i, j)
+# where:
+# a) 0 <= i < j < nums.length
+# and
+# b) nums[i] > 2 * nums[j].
+##
+sub reverse_pairs{
+ my @integers = @_;
+ my @reverse_pairs;
+ do{
+ my $i = $_;
+ do{
+ my $j = $_;
+ push @reverse_pairs, [$i, $j] if $integers[$i] > $integers[$j] + $integers[$j];
+ } for $i + 1 .. @integers - 1;
+ } for 0 .. @integers - 1;
+ return 0 + @reverse_pairs;
+}
+
+MAIN:{
+ say reverse_pairs 1, 3, 2, 3, 1;
+ say reverse_pairs 2, 4, 3, 5, 1;
+} \ No newline at end of file
diff --git a/challenge-243/adam-russell/perl/ch-2.pl b/challenge-243/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..797eb150c3
--- /dev/null
+++ b/challenge-243/adam-russell/perl/ch-2.pl
@@ -0,0 +1,28 @@
+use v5.38;
+##
+# You are given an array of positive integers (>=1). Write a script to
+# return the sum of
+# floor(nums[i] / nums[j])
+# where
+# 0 <= i,j < nums.length.
+# We define the floor() function as returning the integer part of the
+# division.
+##
+use POSIX;
+sub floor_sum{
+ my @integers = @_;
+ my $floor_sum;
+ do{
+ my $i = $_;
+ do{
+ my $j = $_;
+ $floor_sum += floor($integers[$i] / $integers[$j]);
+ } for 0 .. @integers - 1;
+ } for 0 .. @integers - 1;
+ return $floor_sum;
+}
+
+MAIN:{
+ say floor_sum 2, 5, 9;
+ say floor_sum 7, 7, 7, 7, 7, 7, 7;
+}
diff --git a/challenge-243/adam-russell/prolog/ch-1.p b/challenge-243/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..fce375eb9b
--- /dev/null
+++ b/challenge-243/adam-russell/prolog/ch-1.p
@@ -0,0 +1,8 @@
+reverse_pair(X, Y, Z):-
+ (X =\= Y, X > Y + Y, Z = 1, !); Z = 0.
+reverse_pairs([], 0).
+reverse_pairs([H|T], ReversePairs):-
+ reverse_pairs(T, R),
+ maplist(reverse_pair(H), T, RP),
+ sum_list(RP, Sum),
+ ReversePairs is R + Sum. \ No newline at end of file
diff --git a/challenge-243/adam-russell/prolog/ch-2.p b/challenge-243/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..502d0239de
--- /dev/null
+++ b/challenge-243/adam-russell/prolog/ch-2.p
@@ -0,0 +1,11 @@
+floor_sum_pair(X, Y, Z):-
+ Z is floor(X / Y).
+
+floor_sum(Integers, FloorSum):-
+ floor_sum(Integers, Integers, FloorSum).
+floor_sum([], _, 0).
+floor_sum([H|T], L, FloorSum):-
+ floor_sum(T, L, F),
+ maplist(floor_sum_pair(H), L, FS),
+ sum_list(FS, Sum),
+ FloorSum is F + Sum. \ No newline at end of file
diff --git a/challenge-243/adam-russell/r/ch-1.r b/challenge-243/adam-russell/r/ch-1.r
new file mode 100644
index 0000000000..f89c96d0da
--- /dev/null
+++ b/challenge-243/adam-russell/r/ch-1.r
@@ -0,0 +1,25 @@
+ch_1 <- function(){
+ structure(list(), class = "ch_1")
+}
+
+reverse_pairs <- function(self, l){
+ UseMethod("reverse_pairs", self)
+}
+
+reverse_pairs.ch_1 <- function(self, integers) {
+ r <- list()
+ for (i in 1:(length(integers) - 1)) {
+ for (j in (i + 1):length(integers)) {
+ if (integers[i] > integers[j] + integers[j]) {
+ r <- c(r, list(c(i, j)))
+ }
+ }
+ }
+ return(length(r))
+}
+
+ch_1 <- ch_1()
+number_reverse_pairs <- reverse_pairs(ch_1, c(1, 3, 2, 3, 1))
+print(number_reverse_pairs)
+number_reverse_pairs <- reverse_pairs(ch_1, c(2, 4, 3, 5, 1))
+print(number_reverse_pairs) \ No newline at end of file
diff --git a/challenge-243/adam-russell/r/ch-2.r b/challenge-243/adam-russell/r/ch-2.r
new file mode 100644
index 0000000000..bffca25287
--- /dev/null
+++ b/challenge-243/adam-russell/r/ch-2.r
@@ -0,0 +1,23 @@
+ch_2 <- function(){
+ structure(list(), class = "ch_2")
+}
+
+floor_sum <- function(self, l){
+ UseMethod("floor_sum", self)
+}
+
+floor_sum <- function(self, integers) {
+ floor_sum <- 0
+ for (x in integers) {
+ for (y in integers) {
+ floor_sum <- floor_sum + floor(x / y)
+ }
+ }
+ return(floor_sum)
+}
+
+ch_2 <- ch_2()
+floor_summed <- floor_sum(ch_1, c(2, 5, 9))
+print(floor_summed)
+floor_summed <- floor_sum(ch_1, c(7, 7, 7, 7, 7, 7, 7))
+print(floor_summed) \ No newline at end of file