diff options
| author | ntovar <tovar.nelo@gmail.com> | 2023-11-17 18:54:54 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2023-11-17 18:54:54 -0500 |
| commit | 60eaa303bfff74985a2e69485167841e71540152 (patch) | |
| tree | 35d5a1c33eee10620d4227ac8ef061c582dd6ec5 | |
| parent | 4333c415ec1503cffa0618876e2ded4f6eb2ce8d (diff) | |
| download | perlweeklychallenge-club-60eaa303bfff74985a2e69485167841e71540152.tar.gz perlweeklychallenge-club-60eaa303bfff74985a2e69485167841e71540152.tar.bz2 perlweeklychallenge-club-60eaa303bfff74985a2e69485167841e71540152.zip | |
Adding solutions by Nelo Tovar (Perl, Bash)
| -rwxr-xr-x | challenge-243/nelo-tovar/bash/ch-1.sh | 33 | ||||
| -rwxr-xr-x | challenge-243/nelo-tovar/bash/ch-2.sh | 30 | ||||
| -rw-r--r-- | challenge-243/nelo-tovar/perl/ch-1.pl | 63 | ||||
| -rw-r--r-- | challenge-243/nelo-tovar/perl/ch-2.pl | 55 |
4 files changed, 181 insertions, 0 deletions
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; +} |
