From 54f5e9b0b7956c918358d7113480049d85724ec1 Mon Sep 17 00:00:00 2001 From: Solathian Date: Sun, 19 Nov 2023 12:13:49 +0100 Subject: Added files for 243 --- challenge-243/solathian/perl/ch-1.pl | 30 ++++++++++++++++++++++++++++++ challenge-243/solathian/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 challenge-243/solathian/perl/ch-1.pl create mode 100644 challenge-243/solathian/perl/ch-2.pl diff --git a/challenge-243/solathian/perl/ch-1.pl b/challenge-243/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..e577f69bd8 --- /dev/null +++ b/challenge-243/solathian/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!usr/bin/perl +use v5.38; + +use builtin "indexed"; +no warnings "experimental"; + + +# Challenge 243 - 1 - Reverse Pairs +# 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]. + +revPair((1, 3, 2, 3, 1)); +revPair((2, 4, 3, 5, 1)); + +sub revPair(@arr) +{ + my @pairs; + + foreach my ($i, $iVal) (indexed @arr) + { + foreach my ($j, $jVal) (indexed @arr) + { + # just going with the debug approach, a simple counter would also suffice + push(@pairs, [$i, $j]) if( $i < $j and $iVal > 2*$jVal); + } + } + + say "Number of pairs: ", scalar @pairs; +} \ No newline at end of file diff --git a/challenge-243/solathian/perl/ch-2.pl b/challenge-243/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..90b8d13b56 --- /dev/null +++ b/challenge-243/solathian/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!usr/bin/perl +use v5.38; +use builtin "floor"; +no warnings "experimental"; + +# Challenge 243 - 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. + + +floorSum(2, 5, 9); +floorSum(7, 7, 7, 7, 7, 7, 7); + +sub floorSum(@list) +{ + my $sum; + + foreach my ($iVal) (@list) + { + foreach my ($jVal) (@list) + { + $sum += floor($iVal / $jVal) + } + } + + say $sum; +} \ No newline at end of file -- cgit