aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-18 22:31:44 +0000
committerGitHub <noreply@github.com>2023-11-18 22:31:44 +0000
commit0bfb7c2aca9e2767aa710f4d826ac01df3934889 (patch)
tree020c7d4ea75992145138cc88116020378e7a0766
parent36b392f45e2309a6ce511fc0e76b17c14032f9b5 (diff)
parent60eaa303bfff74985a2e69485167841e71540152 (diff)
downloadperlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.tar.gz
perlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.tar.bz2
perlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.zip
Merge pull request #9086 from ntovar/branch-243
Branch 243 By Nelo Tovar
-rw-r--r--challenge-241/nelo-tovar/README.md1
-rw-r--r--challenge-241/nelo-tovar/perl/ch-1.pl59
-rw-r--r--challenge-241/nelo-tovar/perl/ch-2.pl28
-rwxr-xr-xchallenge-243/nelo-tovar/bash/ch-1.sh33
-rwxr-xr-xchallenge-243/nelo-tovar/bash/ch-2.sh30
-rw-r--r--challenge-243/nelo-tovar/perl/ch-1.pl63
-rw-r--r--challenge-243/nelo-tovar/perl/ch-2.pl55
7 files changed, 269 insertions, 0 deletions
diff --git a/challenge-241/nelo-tovar/README.md b/challenge-241/nelo-tovar/README.md
new file mode 100644
index 0000000000..845b45e758
--- /dev/null
+++ b/challenge-241/nelo-tovar/README.md
@@ -0,0 +1 @@
+Solution by Nelo Tovar
diff --git a/challenge-241/nelo-tovar/perl/ch-1.pl b/challenge-241/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..fe64a21acd
--- /dev/null
+++ b/challenge-241/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+#Task 1: Arithmetic Triplets
+#
+#You are given an array (3 or more members) of integers in increasing order and a positive integer.
+#
+#Write a script to find out the number of unique Arithmetic Triplets satisfying the following rules:
+#
+#a) i < j < k
+#b) nums[j] - nums[i] == diff
+#c) nums[k] - nums[j] == diff
+#
+#Example 1
+#
+#Input: @nums = (0, 1, 4, 6, 7, 10)
+# $diff = 3
+#Output: 2
+#
+#Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
+#Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
+
+use strict;
+use warnings;
+use v5.28;
+
+
+my @examples = (
+
+ { nums => [ 0, 1, 4, 6, 7, 10 ], diff => 3, },
+ { nums => [ 4, 5, 6, 7, 8, 9 ], diff => 2, }
+);
+
+sub aritmetic_triplet {
+ my $diff = shift;
+ my @nums = @_;
+ my $len_nums = scalar @nums;
+ my $count = 0;
+
+ for (my $i = 0; $i < $len_nums - 2; $i++) {
+ for (my $j = $i + 1; $j < $len_nums - 1; $j++) {
+ next unless ( ($nums[$j] - $nums[$i]) == $diff );
+
+ for (my $k = $j + 1; $k < $len_nums; $k++) {
+ next unless ( ($nums[$k] - $nums[$j]) == $diff );
+ $count++;
+ }
+ }
+ }
+
+ return $count
+}
+
+foreach my $element (@examples) {
+ my $at = aritmetic_triplet($element->{diff}, $element->{nums}->@*);
+ say 'Input : @nums = (', join(',', $element->{nums}->@*), ')';
+ say ' $diff = ', $element->{diff};
+ say "Output : $at\n"
+
+}
diff --git a/challenge-241/nelo-tovar/perl/ch-2.pl b/challenge-241/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..4ce36119b7
--- /dev/null
+++ b/challenge-241/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+# Task 2: Prime Order
+#
+# You are given an array of unique positive integers greater than 2.
+#
+# Write a script to sort them in ascending order of the count of their prime factors, tie-breaking by ascending value.
+# Example 1
+#
+# Input: @int = (11, 8, 27, 4)
+# Output: (11, 4, 8, 27))
+#
+# Prime factors of 11 => 11
+# Prime factors of 4 => 2, 2
+# Prime factors of 8 => 2, 2, 2
+# Prime factors of 27 => 3, 3, 3
+
+use strict;
+use warnings;
+use v5.28;
+
+my $x=shift;
+for(my $y=2; $y<=$x; $y++) {
+ next if $x%$y;
+ $x/=$y;
+ say $y;
+ redo
+}
diff --git a/challenge-243/nelo-tovar/bash/ch-1.sh b/challenge-243/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..993946911f
--- /dev/null
+++ b/challenge-243/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,33 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 243 - By Nelo Tovar
+#
+# Task 1 : Reverse Pairs
+
+function get_reverse_pairs() {
+ local nums=("$@")
+ local length=${#nums[@]}
+ local pairs=()
+
+ for (( i = 0; i < $(($length - 1)); i++ )); do
+ for (( j = $(($i + 1)) ; j < $length; j++ )); do
+ if [[ ${nums[$i]} -gt $((2 * ${nums[$j]})) ]]; then
+ pairs+=("($i,$j)")
+ fi
+ done
+ done
+
+ echo ${pairs[@]}
+}
+
+example1='1 3 2 3 1'
+example2='2 4 3 5 1'
+
+for e in "$example1" "$example2"; do
+ array=($e)
+ pairs=($(get_reverse_pairs "${array[@]}"))
+ echo "Input : nums = (${array[@]})"
+ echo "Output : ${#pairs[@]}"
+ echo -e "Pairs : ${pairs[@]}\n"
+done
+
diff --git a/challenge-243/nelo-tovar/bash/ch-2.sh b/challenge-243/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..6772948052
--- /dev/null
+++ b/challenge-243/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 243 - by Nelo Tovar
+#
+# Task 2 : Floor Sum
+#
+
+function sum_of_floor() {
+ local nums=("$@")
+ local sum=0
+
+ for i in ${nums[@]}; do
+ for j in ${nums[@]}; do
+ ((sum+=$(($i/$j))))
+ done
+ done
+
+ echo $sum
+}
+
+example1='2 5 9'
+example2='7 7 7 7 7 7 7'
+
+for e in "$example1" "$example2"; do
+ array=($e)
+ sof=$(sum_of_floor "${array[@]}")
+ echo "Input : nums = (${array[@]})"
+ echo -e "Output : $sof\n"
+done
+
diff --git a/challenge-243/nelo-tovar/perl/ch-1.pl b/challenge-243/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..6bad274812
--- /dev/null
+++ b/challenge-243/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+# Task 1: Reverse Pairs
+# Submitted by: Mohammad S Anwar
+#
+# 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].
+# Example 1
+#
+# Input: @nums = (1, 3, 2, 3, 1)
+# Output: 2
+#
+# (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+# (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+#
+# Example 2
+#
+# Input: @nums = (2, 4, 3, 5, 1)
+# Output: 3
+#
+# (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+# (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+# (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 1, 3, 2, 3, 1 ],
+ [ 2, 4, 3, 5, 1 ],
+);
+
+sub get_reverse_pairs {
+ my $nums = shift;
+ my $length = scalar @$nums;
+ my @pairs;
+ for (my $i = 0; $i < $length - 1; $i++) {
+ for (my $j = $i + 1; $j < $length; $j++) {
+ push(@pairs, [$i,$j]) if ( $nums->[$i] > 2 * $nums->[$j])
+ }
+ }
+
+ return \@pairs;
+}
+
+for my $elements (@examples) {
+ my $reverse_pairs = get_reverse_pairs $elements;
+
+ say 'Input : @nums = ', dump(@$elements);
+ say 'Output : ', scalar @$reverse_pairs;
+ foreach my $pair (@$reverse_pairs) {
+ my $i = $pair->[0];
+ my $j = $pair->[1];
+ say sprintf "%s => nums[%d] = %d, nums[%d] = %d, %d > 2 * %d", dump(@$pair), $i, $elements->[$i], $j, $elements->[$j], $elements->[$i], $elements->[$j]
+ }
+ say ' ';
+}
diff --git a/challenge-243/nelo-tovar/perl/ch-2.pl b/challenge-243/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..f37c9c3945
--- /dev/null
+++ b/challenge-243/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+
+# Task 2: Floor Sum
+#
+# 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. The floor() function returns the integer part of the division.
+#
+# Example 1
+#
+# Input: @nums = (2, 5, 9)
+# Output: 10
+#
+# floor(2 / 5) = 0
+# floor(2 / 9) = 0
+# floor(5 / 9) = 0
+# floor(2 / 2) = 1
+# floor(5 / 5) = 1
+# floor(9 / 9) = 1
+# floor(5 / 2) = 2
+# floor(9 / 2) = 4
+# floor(9 / 5) = 1
+#
+# Example 2
+#
+# Input: @nums = (7, 7, 7, 7, 7, 7, 7)
+# Output: 49
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 2, 5, 9 ],
+ [ 7, 7, 7, 7, 7, 7, 7 ],
+);
+
+sub sum_of_floor {
+ my $nums = shift;
+ my $sum = 0;
+
+ foreach my $i (@$nums) {
+ $sum += int($i / $_) foreach (@$nums)
+ }
+
+ return $sum
+}
+
+for my $elements (@examples) {
+ my $sof = sum_of_floor($elements);
+
+ say 'Input : @nums = ', dump(@$elements);
+ say 'Output : ', $sof;
+}