From fd9562531cd99bf082d568e875e50c606c351e5a Mon Sep 17 00:00:00 2001 From: ntovar Date: Wed, 8 Nov 2023 17:51:15 -0500 Subject: Solutions for challenge 241 --- challenge-241/nelo-tovar/README.md | 1 + challenge-241/nelo-tovar/perl/ch-1.pl | 59 +++++++++++++++++++++++++++++++++++ challenge-241/nelo-tovar/perl/ch-2.pl | 28 +++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 challenge-241/nelo-tovar/README.md create mode 100644 challenge-241/nelo-tovar/perl/ch-1.pl create mode 100644 challenge-241/nelo-tovar/perl/ch-2.pl 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 +} -- cgit From 60c9786ce22aaa5bbd9eb83f81226014453c97cb Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Mon, 13 Nov 2023 10:26:05 +0330 Subject: TWC243 --- challenge-243/deadmarshal/blog.txt | 1 + challenge-243/deadmarshal/perl/ch-1.pl | 17 +++++++++++++++++ challenge-243/deadmarshal/perl/ch-2.pl | 17 +++++++++++++++++ challenge-243/deadmarshal/raku/ch-1.raku | 15 +++++++++++++++ challenge-243/deadmarshal/raku/ch-2.raku | 15 +++++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 challenge-243/deadmarshal/blog.txt create mode 100644 challenge-243/deadmarshal/perl/ch-1.pl create mode 100644 challenge-243/deadmarshal/perl/ch-2.pl create mode 100644 challenge-243/deadmarshal/raku/ch-1.raku create mode 100644 challenge-243/deadmarshal/raku/ch-2.raku diff --git a/challenge-243/deadmarshal/blog.txt b/challenge-243/deadmarshal/blog.txt new file mode 100644 index 0000000000..8d0cecec37 --- /dev/null +++ b/challenge-243/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2023/11/twc243.html diff --git a/challenge-243/deadmarshal/perl/ch-1.pl b/challenge-243/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..25080f955f --- /dev/null +++ b/challenge-243/deadmarshal/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Algorithm::Combinatorics qw(combinations); + +sub reverse_pairs{ + my $it = combinations($_[0],2); + my $count = 0; + while(my $c = $it->next){ + $count++ if $c->[0] > ($c->[1] * 2) + } + $count +} + +printf "%d\n",reverse_pairs([1,3,2,3,1]); +printf "%d\n",reverse_pairs([2,4,3,5,1]); + diff --git a/challenge-243/deadmarshal/perl/ch-2.pl b/challenge-243/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..99efb96562 --- /dev/null +++ b/challenge-243/deadmarshal/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Algorithm::Combinatorics qw(variations_with_repetition); + +sub floor_sum{ + my $it = variations_with_repetition($_[0],2); + my $sum = 0; + while(my $c = $it->next){ + $sum += int($c->[0] / $c->[1]); + } + $sum +} + +printf "%d\n",floor_sum([2,5,9]); +printf "%d\n",floor_sum([7,7,7,7,7,7,7]); + diff --git a/challenge-243/deadmarshal/raku/ch-1.raku b/challenge-243/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..c122ffdf46 --- /dev/null +++ b/challenge-243/deadmarshal/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +sub reverse-pairs(@arr) +{ + my $count = 0; + for @arr.combinations(2) + { + $count++ if $_[0] > ($_[1] * 2) + } + $count +} + +say reverse-pairs([1,3,2,3,1]); +say reverse-pairs([2,4,3,5,1]); + diff --git a/challenge-243/deadmarshal/raku/ch-2.raku b/challenge-243/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..59a3a3bd37 --- /dev/null +++ b/challenge-243/deadmarshal/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +sub floor-sum(@arr) +{ + my $sum = 0; + for @arr X @arr + { + $sum += $_[0] div $_[1]; + } + $sum +} + +say floor-sum([2,5,9]); +say floor-sum([7,7,7,7,7,7,7]); + -- cgit From 51490d6cb389bbb3a506b41cb59123669940cbe7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Nov 2023 07:15:09 +0000 Subject: Challenge 243 Solutions (Raku) --- challenge-243/mark-anderson/raku/ch-1.raku | 10 +++ challenge-243/mark-anderson/raku/ch-2.raku | 113 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 challenge-243/mark-anderson/raku/ch-1.raku create mode 100644 challenge-243/mark-anderson/raku/ch-2.raku diff --git a/challenge-243/mark-anderson/raku/ch-1.raku b/challenge-243/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..7f6bd0bee2 --- /dev/null +++ b/challenge-243/mark-anderson/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Test; + +is reverse-pairs(1,3,2,3,1), 2; +is reverse-pairs(2,4,3,5,1), 3; + +sub reverse-pairs(*@a) +{ + + grep { .[0] > .[1] * 2 }, @a.combinations(2) +} diff --git a/challenge-243/mark-anderson/raku/ch-2.raku b/challenge-243/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..3d335682bf --- /dev/null +++ b/challenge-243/mark-anderson/raku/ch-2.raku @@ -0,0 +1,113 @@ +#!/usr/bin/env raku +use Test; +use Benchy; + +is floor-sum(2,5,9), 10; +is floor-sum(7 xx 7), 49; +is floor-sum(5,5,7,15,15,15), 40; + +benchmark(); + +=begin comment + + ***** Explanation of floor-sum() ***** + +There's a sequence for the floor-sum of consecutive numbers where +@a[0] >= @a.elems. That sequence is... + + 0,1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190...* + +and can be generated with... + + 0,1, -> $_ { ++$ + .succ }...* + +For example, if @a is [5,6,7] or [10,11,12] then the floor-sum of +@a is (0,1, -> $_ { ++$ + .succ } ... *)[@a.elems] == 6; + +Unfortunately I couldn't see a pattern for consecutive numbers where +@a[0] < @a.elems so I left out the (possible) optimization of +consecutive streaks in the input 😭 + +So here's what I did... + +This challenge involves summing floor quotients so we can ignore +any pair where the dividend is less than the divisor since those +will always be 0. + +Another optimization is turning the array into a bag + + @a = [5 5 7 15 15 15] + $bag = @a.Bag # Bag(15(3) 5(2) 7) + +and then generating the key combinations + + $bag.keys.sort.combinations(2) # ((5 7) (5 15) (7 15)) + +The keys are sorted so the head is less than the tail so we don't +get any quotients == 0. + +Next, we take the floor(tail / head) of each pair and multiply that with +$bag{ tail } * $bag{ head }. + + map { .[1] div .[0] * $bag{.[1]} * $bag{.[0]} }, + $bag.keys.sort.combinations(2) # (2 18 6) + +For example, the pair (5 15) result would be + + (15 div 5) * $bag{15} * $bag{5} # 18 + +which is equivalent to the un-optimized + + (15 div 5) + (15 div 5) + (15 div 5) + (15 div 5) + (15 div 5) + (15 div 5) + +Now we need the pair results from (15 15 15), (5 5) and (7) which are + + (15 div 15) + (15 div 15) + (15 div 15) + + (15 div 15) + (15 div 15) + (15 div 15) + + (15 div 15) + (15 div 15) + (15 div 15) # 9 + + (5 div 5) + (5 div 5) + (5 div 5) + (5 div 5) # 4 + + (7 div 7) # 1 + +which are the squares of the $bag values + + $bag.values >>**>> 2 # (9 4 1) + +Finally, we just have to sum the two lists + + (2 + 18 + 6) + (9 + 4 + 1) # 40 + +=end comment + +sub floor-sum(*@a where .all > 0) +{ + my $bag = @a.Bag; + + sum flat $bag.values >>**>> 2, + map { .[1] div .[0] * $bag{.[1]} * $bag{.[0]} }, + $bag.keys.sort.combinations(2) +} + +sub floor-sum-slow(*@a where .all > 0) +{ + sum @a Xdiv @a +} + +sub benchmark +{ + my @a = (1..9).roll(2500); + + b 5, + { + say floor-sum-slow(@a), " slow" + }, + + { + say floor-sum(@a), " fast" + } + + # Old: 48.663255609s + # New: 0.172294888s + # NEW version is 282.44x faster +} -- cgit From 03593725948f4adc106777b47d963d4e4f4b31d8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Nov 2023 07:54:40 +0000 Subject: Challenge 243 Solutions (Raku) --- challenge-243/mark-anderson/raku/ch-1.raku | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/challenge-243/mark-anderson/raku/ch-1.raku b/challenge-243/mark-anderson/raku/ch-1.raku index 7f6bd0bee2..a944777d1e 100644 --- a/challenge-243/mark-anderson/raku/ch-1.raku +++ b/challenge-243/mark-anderson/raku/ch-1.raku @@ -1,10 +1,16 @@ #!/usr/bin/env raku use Test; +use experimental :cached; is reverse-pairs(1,3,2,3,1), 2; is reverse-pairs(2,4,3,5,1), 3; sub reverse-pairs(*@a) { - + grep { .[0] > .[1] * 2 }, @a.combinations(2) + sub rp($p) is cached + { + .[0] > .[1] * 2 given |$p + } + + + @a.combinations(2).grep(&rp) } -- cgit From fa15676e0a781d61e4f9592f9ea6713c58419275 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 13 Nov 2023 09:54:25 +0100 Subject: Add solutions to 243: Reverse Pairs & Floor Sum by E. Choroba --- challenge-243/e-choroba/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-243/e-choroba/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 challenge-243/e-choroba/perl/ch-1.pl create mode 100755 challenge-243/e-choroba/perl/ch-2.pl diff --git a/challenge-243/e-choroba/perl/ch-1.pl b/challenge-243/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..10f51ff517 --- /dev/null +++ b/challenge-243/e-choroba/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub reverse_pairs(@nums) { + my $tally = 0; + for my $j (1 .. $#nums) { + for my $i (0 .. $j - 1) { + ++$tally if $nums[$i] > 2 * $nums[$j]; + } + } + return $tally +} + +use Test::More tests => 2; + +is reverse_pairs(1, 3, 2, 3, 1), 2, 'Example 1'; +is reverse_pairs(2, 4, 3, 5, 1), 3, 'Example 2'; diff --git a/challenge-243/e-choroba/perl/ch-2.pl b/challenge-243/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..3fb2af6363 --- /dev/null +++ b/challenge-243/e-choroba/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use POSIX qw{ floor }; + +sub floor_sum(@nums) { + my $sum = 0; + for my $i (@nums) { + for my $j (@nums) { + $sum += floor($i / $j); + } + } + return $sum +} + +use List::Util qw{ sum }; +sub floor_sum_functional(@nums) { + sum(map { my $i = $_; map floor($i / $_), @nums } @nums) +} +# Benchmark shows no significant difference in performance. + +use Test::More tests => 2 + 2; + +is floor_sum(2, 5, 9), 10, 'Example 1'; +is floor_sum(7, 7, 7, 7, 7, 7, 7), 49, 'Example 2'; + +is floor_sum_functional(2, 5, 9), 10, 'Example 1 func'; +is floor_sum_functional(7, 7, 7, 7, 7, 7, 7), 49, 'Example 2 func'; -- cgit From 7dd52fb63dcac5f223b444c76866e53d2a330891 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:17:23 +0000 Subject: Challenge 243 Solutions (Raku) --- challenge-243/mark-anderson/raku/ch-1.raku | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/challenge-243/mark-anderson/raku/ch-1.raku b/challenge-243/mark-anderson/raku/ch-1.raku index a944777d1e..c5e9532de3 100644 --- a/challenge-243/mark-anderson/raku/ch-1.raku +++ b/challenge-243/mark-anderson/raku/ch-1.raku @@ -1,16 +1,13 @@ #!/usr/bin/env raku use Test; -use experimental :cached; is reverse-pairs(1,3,2,3,1), 2; is reverse-pairs(2,4,3,5,1), 3; +# I've tried memoize, race, and promises for big lists and those seem +# to make things slower 🤔 + sub reverse-pairs(*@a) { - sub rp($p) is cached - { - .[0] > .[1] * 2 given |$p - } - - + @a.combinations(2).grep(&rp) + + grep { .[0] > .[1] * 2 }, @a.combinations(2) } -- cgit From b00ba3344b779c7565c0bd485c4577d65d874288 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 13 Nov 2023 11:18:03 +0000 Subject: w243 - Task 1 & 2 --- challenge-243/perlboy1967/perl/ch1.pl | 41 +++++++++++++++++++++++++++++++++++ challenge-243/perlboy1967/perl/ch2.pl | 36 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 challenge-243/perlboy1967/perl/ch1.pl create mode 100755 challenge-243/perlboy1967/perl/ch2.pl diff --git a/challenge-243/perlboy1967/perl/ch1.pl b/challenge-243/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..b0d3bf22dc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch1.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 243 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-243 + +Author: Niels 'PerlBoy' van Dijke + +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]. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +sub nReversePairs (@) { + my $n = 0; + + for my $i (0 .. $#_ - 1) { + for my $j ($i + 1 .. $#_) { + $n++ if ($_[$i] > 2 * $_[$j]); + } + } + + return $n; +} + +is(nReversePairs(1,3,2,3,1),2); +is(nReversePairs(2,4,3,5,1),3); + +done_testing; diff --git a/challenge-243/perlboy1967/perl/ch2.pl b/challenge-243/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..78b89dbddc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch2.pl @@ -0,0 +1,36 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 243 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-243 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Floor Sum +Submitted by: Mohammad S Anwar + +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. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(variations_with_repetition); +use POSIX qw(floor); +use List::Util qw(sum0); + +sub floorSum (@) { + sum0 map { floor($$_[0] / $$_[1]) } variations_with_repetition([@_],2); +} + +is(floorSum(2,5,9),10); +is(floorSum((7) x 7),49); + +done_testing; -- cgit From ebb103d257a18a7e796f121350be487a26de636f Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 13 Nov 2023 23:28:37 +1100 Subject: pwc243 solution in python --- challenge-243/pokgopun/python/ch-1.py | 52 ++++++++++++++++++++++++++++++++ challenge-243/pokgopun/python/ch-2.py | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 challenge-243/pokgopun/python/ch-1.py create mode 100644 challenge-243/pokgopun/python/ch-2.py diff --git a/challenge-243/pokgopun/python/ch-1.py b/challenge-243/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..5caf040c6d --- /dev/null +++ b/challenge-243/pokgopun/python/ch-1.py @@ -0,0 +1,52 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +""" + +Task 1: Reverse Pairs + +Submitted by: [50]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 + +Task 2: Floor Sum +""" +### solution by pokgopun@gmail.com + +def rpCount(nums: tuple): + c, l = 0, len(nums) + for i in range(l-1): + for j in range(i,l): + if nums[i] > 2 * nums[j]: c += 1 + return c + +for inpt,otpt in { + (1, 3, 2, 3, 1):2, + (2, 4, 3, 5, 1):3, + }.items(): + print(rpCount(inpt)==otpt) + + + diff --git a/challenge-243/pokgopun/python/ch-2.py b/challenge-243/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d45f06e663 --- /dev/null +++ b/challenge-243/pokgopun/python/ch-2.py @@ -0,0 +1,57 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +""" + +Task 2: Floor Sum + +Submitted by: [51]Mohammad S Anwar + __________________________________________________________________ + + 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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def floorSum(tup: tuple): + c, l = 0, len(tup) + for i in range(l): + for j in range(l): + c += tup[j]//tup[i] + return c + +for inpt,otpt in { + (2, 5, 9): 10, + (7, 7, 7, 7, 7, 7, 7): 49, + }.items(): + print(floorSum(inpt)==otpt) + + -- cgit From 299bb6dc4c3a77afc72f5a953ab3d4d0e183df0a Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 14 Nov 2023 00:01:33 +1100 Subject: pwc243 solution in go --- challenge-243/pokgopun/go/ch-1.go | 66 ++++++++++++++++++++++++++++++++++++ challenge-243/pokgopun/go/ch-2.go | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 challenge-243/pokgopun/go/ch-1.go create mode 100644 challenge-243/pokgopun/go/ch-2.go diff --git a/challenge-243/pokgopun/go/ch-1.go b/challenge-243/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..3ce659dddc --- /dev/null +++ b/challenge-243/pokgopun/go/ch-1.go @@ -0,0 +1,66 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +/*# + +Task 1: Reverse Pairs + +Submitted by: [50]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 + +Task 2: Floor Sum +#*/ +//# solution by pokgopun@gmail.com + +package main + +import "fmt" + +type nums []int + +func (n nums) rpCount() int { + c, l := 0, len(n) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + if n[i] > 2*n[j] { + c++ + } + } + } + return c +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{1, 3, 2, 3, 1}, 2}, + {nums{2, 4, 3, 5, 1}, 3}, + } { + fmt.Println(data.input.rpCount() == data.output) + } +} diff --git a/challenge-243/pokgopun/go/ch-2.go b/challenge-243/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a9458aec9f --- /dev/null +++ b/challenge-243/pokgopun/go/ch-2.go @@ -0,0 +1,70 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +/*# + +Task 2: Floor Sum + +Submitted by: [51]Mohammad S Anwar + __________________________________________________________________ + + 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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import "fmt" + +type nums []int + +func (n nums) floorSum() int { + c, l := 0, len(n) + for i := 0; i < l; i++ { + for j := 0; j < l; j++ { + c += n[i] / n[j] + } + } + return c +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{2, 5, 9}, 10}, + {nums{7, 7, 7, 7, 7, 7, 7}, 49}, + } { + fmt.Println(data.input.floorSum() == data.output) + } +} -- cgit From 7c6b4dfaf173a92addf28b1a90eb68a978adb2d7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 13 Nov 2023 13:59:43 +0000 Subject: Add Perl, C and C++ solutions --- challenge-243/paulo-custodio/Makefile | 2 + challenge-243/paulo-custodio/c/ch-1.c | 64 ++++++++ challenge-243/paulo-custodio/c/ch-2.c | 66 ++++++++ challenge-243/paulo-custodio/c/utarray.h | 252 +++++++++++++++++++++++++++++ challenge-243/paulo-custodio/cpp/ch-1.cpp | 61 +++++++ challenge-243/paulo-custodio/cpp/ch-2.cpp | 63 ++++++++ challenge-243/paulo-custodio/perl/ch-1.pl | 45 ++++++ challenge-243/paulo-custodio/perl/ch-2.pl | 48 ++++++ challenge-243/paulo-custodio/t/test-1.yaml | 10 ++ challenge-243/paulo-custodio/t/test-2.yaml | 10 ++ 10 files changed, 621 insertions(+) create mode 100644 challenge-243/paulo-custodio/Makefile create mode 100644 challenge-243/paulo-custodio/c/ch-1.c create mode 100644 challenge-243/paulo-custodio/c/ch-2.c create mode 100644 challenge-243/paulo-custodio/c/utarray.h create mode 100644 challenge-243/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-243/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-243/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-243/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-243/paulo-custodio/t/test-1.yaml create mode 100644 challenge-243/paulo-custodio/t/test-2.yaml diff --git a/challenge-243/paulo-custodio/Makefile b/challenge-243/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-243/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-243/paulo-custodio/c/ch-1.c b/challenge-243/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..39ffc3dc91 --- /dev/null +++ b/challenge-243/paulo-custodio/c/ch-1.c @@ -0,0 +1,64 @@ +/* +Challenge 243 + +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 +*/ + +#include "utarray.h" +#include +#include + +int num_reverse_pairs(UT_array* nums) { + int count = 0; + for (size_t i = 0; i < utarray_len(nums)-1; i++) { + for (size_t j = i+1; j < utarray_len(nums); j++) { + if (*(int*)utarray_eltptr(nums, i) > 2 * *(int*)utarray_eltptr(nums, j)) + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + fputs("Usage: ch-1 n n n ...\n", stderr); + exit(EXIT_FAILURE); + } + + UT_array* nums; + utarray_new(nums, &ut_int_icd); + + for (int i = 1; i < argc; i++) { + int n = atoi(argv[i]); + utarray_push_back(nums, &n); + } + + int count = num_reverse_pairs(nums); + printf("%d\n", count); + + utarray_free(nums); +} diff --git a/challenge-243/paulo-custodio/c/ch-2.c b/challenge-243/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..47974d633c --- /dev/null +++ b/challenge-243/paulo-custodio/c/ch-2.c @@ -0,0 +1,66 @@ +/* +Challenge 243 + +Task 2: Floor Sum +Submitted by: Mohammad S Anwar + +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 +*/ + +#include "utarray.h" +#include +#include + +int sum_floor(UT_array* nums) { + int sum = 0; + for (size_t i = 0; i < utarray_len(nums); i++) { + for (size_t j = 0; j < utarray_len(nums); j++) { + sum += *(int*)utarray_eltptr(nums, i) / *(int*)utarray_eltptr(nums, j); + } + } + return sum; +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + fputs("Usage: ch-2 n n n ...\n", stderr); + exit(EXIT_FAILURE); + } + + UT_array* nums; + utarray_new(nums, &ut_int_icd); + + for (int i = 1; i < argc; i++) { + int n = atoi(argv[i]); + utarray_push_back(nums, &n); + } + + int sum = sum_floor(nums); + printf("%d\n", sum); + + utarray_free(nums); +} diff --git a/challenge-243/paulo-custodio/c/utarray.h b/challenge-243/paulo-custodio/c/utarray.h new file mode 100644 index 0000000000..1fe8bc1c74 --- /dev/null +++ b/challenge-243/paulo-custodio/c/utarray.h @@ -0,0 +1,252 @@ +/* +Copyright (c) 2008-2022, Troy D. Hanson https://troydhanson.github.io/uthash/ +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* a dynamic array implementation using macros + */ +#ifndef UTARRAY_H +#define UTARRAY_H + +#define UTARRAY_VERSION 2.3.0 + +#include /* size_t */ +#include /* memset, etc */ +#include /* exit */ + +#ifdef __GNUC__ +#define UTARRAY_UNUSED __attribute__((__unused__)) +#else +#define UTARRAY_UNUSED +#endif + +#ifndef utarray_oom +#define utarray_oom() exit(-1) +#endif + +typedef void (ctor_f)(void *dst, const void *src); +typedef void (dtor_f)(void *elt); +typedef void (init_f)(void *elt); +typedef struct { + size_t sz; + init_f *init; + ctor_f *copy; + dtor_f *dtor; +} UT_icd; + +typedef struct { + unsigned i,n;/* i: index of next available slot, n: num slots */ + UT_icd icd; /* initializer, copy and destructor functions */ + char *d; /* n slots of size icd->sz*/ +} UT_array; + +#define utarray_init(a,_icd) do { \ + memset(a,0,sizeof(UT_array)); \ + (a)->icd = *(_icd); \ +} while(0) + +#define utarray_done(a) do { \ + if ((a)->n) { \ + if ((a)->icd.dtor) { \ + unsigned _ut_i; \ + for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ + (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \ + } \ + } \ + free((a)->d); \ + } \ + (a)->n=0; \ +} while(0) + +#define utarray_new(a,_icd) do { \ + (a) = (UT_array*)malloc(sizeof(UT_array)); \ + if ((a) == NULL) { \ + utarray_oom(); \ + } \ + utarray_init(a,_icd); \ +} while(0) + +#define utarray_free(a) do { \ + utarray_done(a); \ + free(a); \ +} while(0) + +#define utarray_reserve(a,by) do { \ + if (((a)->i+(by)) > (a)->n) { \ + char *utarray_tmp; \ + while (((a)->i+(by)) > (a)->n) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \ + utarray_tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz); \ + if (utarray_tmp == NULL) { \ + utarray_oom(); \ + } \ + (a)->d=utarray_tmp; \ + } \ +} while(0) + +#define utarray_push_back(a,p) do { \ + utarray_reserve(a,1); \ + if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \ + else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \ +} while(0) + +#define utarray_pop_back(a) do { \ + if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \ + else { (a)->i--; } \ +} while(0) + +#define utarray_extend_back(a) do { \ + utarray_reserve(a,1); \ + if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \ + else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \ + (a)->i++; \ +} while(0) + +#define utarray_len(a) ((a)->i) + +#define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL) +#define _utarray_eltptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j)))) + +#define utarray_insert(a,p,j) do { \ + if ((j) > (a)->i) utarray_resize(a,j); \ + utarray_reserve(a,1); \ + if ((j) < (a)->i) { \ + memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \ + ((a)->i - (j))*((a)->icd.sz)); \ + } \ + if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \ + else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \ + (a)->i++; \ +} while(0) + +#define utarray_inserta(a,w,j) do { \ + if (utarray_len(w) == 0) break; \ + if ((j) > (a)->i) utarray_resize(a,j); \ + utarray_reserve(a,utarray_len(w)); \ + if ((j) < (a)->i) { \ + memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \ + _utarray_eltptr(a,j), \ + ((a)->i - (j))*((a)->icd.sz)); \ + } \ + if ((a)->icd.copy) { \ + unsigned _ut_i; \ + for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \ + (a)->icd.copy(_utarray_eltptr(a, (j) + _ut_i), _utarray_eltptr(w, _ut_i)); \ + } \ + } else { \ + memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \ + utarray_len(w)*((a)->icd.sz)); \ + } \ + (a)->i += utarray_len(w); \ +} while(0) + +#define utarray_resize(dst,num) do { \ + unsigned _ut_i; \ + if ((dst)->i > (unsigned)(num)) { \ + if ((dst)->icd.dtor) { \ + for (_ut_i = (num); _ut_i < (dst)->i; ++_ut_i) { \ + (dst)->icd.dtor(_utarray_eltptr(dst, _ut_i)); \ + } \ + } \ + } else if ((dst)->i < (unsigned)(num)) { \ + utarray_reserve(dst, (num) - (dst)->i); \ + if ((dst)->icd.init) { \ + for (_ut_i = (dst)->i; _ut_i < (unsigned)(num); ++_ut_i) { \ + (dst)->icd.init(_utarray_eltptr(dst, _ut_i)); \ + } \ + } else { \ + memset(_utarray_eltptr(dst, (dst)->i), 0, (dst)->icd.sz*((num) - (dst)->i)); \ + } \ + } \ + (dst)->i = (num); \ +} while(0) + +#define utarray_concat(dst,src) do { \ + utarray_inserta(dst, src, utarray_len(dst)); \ +} while(0) + +#define utarray_erase(a,pos,len) do { \ + if ((a)->icd.dtor) { \ + unsigned _ut_i; \ + for (_ut_i = 0; _ut_i < (len); _ut_i++) { \ + (a)->icd.dtor(utarray_eltptr(a, (pos) + _ut_i)); \ + } \ + } \ + if ((a)->i > ((pos) + (len))) { \ + memmove(_utarray_eltptr(a, pos), _utarray_eltptr(a, (pos) + (len)), \ + ((a)->i - ((pos) + (len))) * (a)->icd.sz); \ + } \ + (a)->i -= (len); \ +} while(0) + +#define utarray_renew(a,u) do { \ + if (a) utarray_clear(a); \ + else utarray_new(a, u); \ +} while(0) + +#define utarray_clear(a) do { \ + if ((a)->i > 0) { \ + if ((a)->icd.dtor) { \ + unsigned _ut_i; \ + for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ + (a)->icd.dtor(_utarray_eltptr(a, _ut_i)); \ + } \ + } \ + (a)->i = 0; \ + } \ +} while(0) + +#define utarray_sort(a,cmp) do { \ + qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \ +} while(0) + +#define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp) + +#define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL) +#define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : (((a)->i != utarray_eltidx(a,e)+1) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL)) +#define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) != 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL)) +#define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL) +#define utarray_eltidx(a,e) (((char*)(e) - (a)->d) / (a)->icd.sz) + +/* last we pre-define a few icd for common utarrays of ints and strings */ +static void utarray_str_cpy(void *dst, const void *src) { + char *const *srcc = (char *const *)src; + char **dstc = (char**)dst; + if (*srcc == NULL) { + *dstc = NULL; + } else { + *dstc = (char*)malloc(strlen(*srcc) + 1); + if (*dstc == NULL) { + utarray_oom(); + } else { + strcpy(*dstc, *srcc); + } + } +} +static void utarray_str_dtor(void *elt) { + char **eltc = (char**)elt; + if (*eltc != NULL) free(*eltc); +} +static const UT_icd ut_str_icd UTARRAY_UNUSED = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor}; +static const UT_icd ut_int_icd UTARRAY_UNUSED = {sizeof(int),NULL,NULL,NULL}; +static const UT_icd ut_ptr_icd UTARRAY_UNUSED = {sizeof(void*),NULL,NULL,NULL}; + + +#endif /* UTARRAY_H */ diff --git a/challenge-243/paulo-custodio/cpp/ch-1.cpp b/challenge-243/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..ce52b9b8f2 --- /dev/null +++ b/challenge-243/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,61 @@ +/* +Challenge 243 + +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 +*/ + +#include +#include +using namespace std; + +int num_reverse_pairs(const vector& nums) { + int count = 0; + for (size_t i = 0; i < nums.size()-1; i++) { + for (size_t j = i+1; j < nums.size(); j++) { + if (nums[i] > 2 * nums[j]) + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + cerr << "Usage: ch-1 n n n ..." << endl; + exit(EXIT_FAILURE); + } + + vector nums; + + for (int i = 1; i < argc; i++) { + int n = atoi(argv[i]); + nums.push_back(n); + } + + int count = num_reverse_pairs(nums); + cout << count << endl; +} diff --git a/challenge-243/paulo-custodio/cpp/ch-2.cpp b/challenge-243/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..8bcad05d7c --- /dev/null +++ b/challenge-243/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,63 @@ +/* +Challenge 243 + +Task 2: Floor Sum +Submitted by: Mohammad S Anwar + +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 +*/ + +#include +#include +using namespace std; + +int sum_floor(const vector& nums) { + int sum = 0; + for (size_t i = 0; i < nums.size(); i++) { + for (size_t j = 0; j < nums.size(); j++) { + sum += nums[i] / nums[j]; + } + } + return sum; +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + cerr << "Usage: ch-1 n n n ..." << endl; + exit(EXIT_FAILURE); + } + + vector nums; + + for (int i = 1; i < argc; i++) { + int n = atoi(argv[i]); + nums.push_back(n); + } + + int sum = sum_floor(nums); + cout << sum << endl; +} diff --git a/challenge-243/paulo-custodio/perl/ch-1.pl b/challenge-243/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..157e5c0828 --- /dev/null +++ b/challenge-243/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 243 +# +# 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 Modern::Perl; + +@ARGV>0 or die "Usage: ch-1.pl n n ...\n"; +say num_reverse_pairs(@ARGV); + +sub num_reverse_pairs { + my(@n) = @_; + my $count = 0; + for my $i (0..$#n-1) { + for my $j ($i+1..$#n) { + $count++ if $n[$i] > 2* $n[$j]; + } + } + return $count; +} diff --git a/challenge-243/paulo-custodio/perl/ch-2.pl b/challenge-243/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..e8fd020119 --- /dev/null +++ b/challenge-243/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +# Challenge 243 +# +# Task 2: Floor Sum +# Submitted by: Mohammad S Anwar +# +# 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 Modern::Perl; + +@ARGV>0 or die "Usage: ch-2.pl n n ...\n"; +say sum_floor(@ARGV); + +sub sum_floor { + my(@n) = @_; + my $sum = 0; + for my $i (0..$#n) { + for my $j (0..$#n) { + $sum += int($n[$i] / $n[$j]); + } + } + return $sum; +} diff --git a/challenge-243/paulo-custodio/t/test-1.yaml b/challenge-243/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..2156a4274d --- /dev/null +++ b/challenge-243/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 3 2 3 1 + input: + output: 2 +- setup: + cleanup: + args: 2 4 3 5 1 + input: + output: 3 diff --git a/challenge-243/paulo-custodio/t/test-2.yaml b/challenge-243/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..fdbcf42adf --- /dev/null +++ b/challenge-243/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 5 9 + input: + output: 10 +- setup: + cleanup: + args: 7 7 7 7 7 7 7 + input: + output: 49 -- cgit From dee92c70c7a789145e5e8868ef282fb81b1b2573 Mon Sep 17 00:00:00 2001 From: Kjetil Skotheim Date: Mon, 13 Nov 2023 16:46:53 +0100 Subject: perl-weekly-challenge-243 --- challenge-243/kjetillll/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-243/kjetillll/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 challenge-243/kjetillll/perl/ch-1.pl create mode 100644 challenge-243/kjetillll/perl/ch-2.pl diff --git a/challenge-243/kjetillll/perl/ch-1.pl b/challenge-243/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..e625e158bc --- /dev/null +++ b/challenge-243/kjetillll/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; use warnings; + +@ARGV ? run_args(@ARGV) + : run_tests(); + +sub count_reverse_pairs { + my $indexes=join',',0..$#_; + scalar + grep { my($i,$j)=split/_/; $i<$j and $_[$i]>2*$_[$j] } + glob "{$indexes}_{$indexes}" +} + +sub run_args { print count_reverse_pairs(@_) } + +sub run_tests { + for my $test ( + [ [1,3,2,3,1] => 2 ], + [ [2,4,3,5,1] => 3 ], + ){ + my($input,$expected)=@$test; + my $got = count_reverse_pairs(@$input); + print $expected == $got ? 'ok' : '***NOT OK'; + print " input: @$input expected: $expected got: $got\n"; + } +} + diff --git a/challenge-243/kjetillll/perl/ch-2.pl b/challenge-243/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..6a3c903dcd --- /dev/null +++ b/challenge-243/kjetillll/perl/ch-2.pl @@ -0,0 +1,26 @@ +use strict; use warnings; + +@ARGV ? run_args(@ARGV) + : run_tests(); + +sub sum_of_floors { + eval + join '+', + map { /_/; "int($`/$')" } + glob "{@_}_{@_}" =~ s/ /,/gr +} + +sub run_args { print sum_of_floors(@_) } + +sub run_tests { + for my $test ( + [ [2,5,9] => 10 ], + [ [7,7,7,7,7,7,7] => 49 ], + ){ + my($input,$expected)=@$test; + my $got = sum_of_floors(@$input); + print $expected == $got ? 'ok' : '***NOT OK'; + print " input: @$input expected: $expected got: $got\n"; + } +} + -- cgit From 2f1d27e3fdca63eebd875960c6d9f07af5b4d782 Mon Sep 17 00:00:00 2001 From: Kjetil Skotheim Date: Mon, 13 Nov 2023 16:50:06 +0100 Subject: perl-weekly-challenge-243 --- challenge-243/kjetillll/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-243/kjetillll/perl/ch-2.pl b/challenge-243/kjetillll/perl/ch-2.pl index 6a3c903dcd..1f544f85d2 100644 --- a/challenge-243/kjetillll/perl/ch-2.pl +++ b/challenge-243/kjetillll/perl/ch-2.pl @@ -6,7 +6,7 @@ use strict; use warnings; sub sum_of_floors { eval join '+', - map { /_/; "int($`/$')" } + map { /_/; int($`/$') } glob "{@_}_{@_}" =~ s/ /,/gr } -- cgit From c82caebbb74525bd946303d3775e13564a91971f Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 13 Nov 2023 10:13:25 -0600 Subject: Solve PWC243 --- challenge-243/wlmb/blog.txt | 1 + challenge-243/wlmb/perl/ch-1.pl | 14 ++++++++++++++ challenge-243/wlmb/perl/ch-2.pl | 9 +++++++++ 3 files changed, 24 insertions(+) create mode 100644 challenge-243/wlmb/blog.txt create mode 100755 challenge-243/wlmb/perl/ch-1.pl create mode 100755 challenge-243/wlmb/perl/ch-2.pl diff --git a/challenge-243/wlmb/blog.txt b/challenge-243/wlmb/blog.txt new file mode 100644 index 0000000000..7fc9bcc983 --- /dev/null +++ b/challenge-243/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/11/13/PWC243/ diff --git a/challenge-243/wlmb/perl/ch-1.pl b/challenge-243/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..aacc2ff007 --- /dev/null +++ b/challenge-243/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 243 +# Task 1: Reverse Pairs +# +# See https://wlmb.github.io/2023/11/13/PWC243/#task-1-reverse-pairs +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find how many reversed pairs are in the array N1 N2... + FIN +my $matrix=pdl(@ARGV)->dummy(1,0+@ARGV); +say "@ARGV ->", + (($matrix->xvals < $matrix->yvals) & ($matrix > 2*$matrix->transpose))->sum; diff --git a/challenge-243/wlmb/perl/ch-2.pl b/challenge-243/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..78a1859f85 --- /dev/null +++ b/challenge-243/wlmb/perl/ch-2.pl @@ -0,0 +1,9 @@ +#!/usr/bin/env perl +# Perl weekly challenge 243 +# Task 2: Floor Sum +# +# See https://wlmb.github.io/2023/11/13/PWC243/#task-2-floor-sum +use v5.36; +use PDL; +my $in=pdl(@ARGV); +say "$in -> ", ($in/$in->dummy(0))->floor->sum; -- cgit From 707e1eb33b95344b35271dc223f3939738187acb Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 13 Nov 2023 16:26:40 +0000 Subject: add solutions week 243 in python --- challenge-243/steven-wilson/python/ch-01.py | 25 +++++++++++++++++++++++++ challenge-243/steven-wilson/python/ch-02.py | 26 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 challenge-243/steven-wilson/python/ch-01.py create mode 100644 challenge-243/steven-wilson/python/ch-02.py diff --git a/challenge-243/steven-wilson/python/ch-01.py b/challenge-243/steven-wilson/python/ch-01.py new file mode 100644 index 0000000000..59f53277bf --- /dev/null +++ b/challenge-243/steven-wilson/python/ch-01.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +from itertools import permutations + + +def reverse_pairs(*elements): + ''' + return the number of reverse pairs in the given array + >>> reverse_pairs(1, 3, 2, 3, 1) + 2 + >>> reverse_pairs(2, 4, 3, 5, 1) + 3 + ''' + count = 0 + perms = permutations(range(len(elements)), r=2) + for i, j in perms: + if elements[i] > 2 * elements[j] and 0 <= i and i < j and j < len(elements): + count += 1 + return count + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/challenge-243/steven-wilson/python/ch-02.py b/challenge-243/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..4dc1b96750 --- /dev/null +++ b/challenge-243/steven-wilson/python/ch-02.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +from itertools import product +from math import floor + + +def floor_sum(*elements): + ''' + return the sum of floor(nums[i] / nums[j]) where 0 <= i,j < nums.length + >>> floor_sum(2, 5, 9) + 10 + >>> floor_sum(7, 7, 7, 7, 7, 7, 7) + 49 + ''' + sum = 0 + prod = product(range(len(elements)), repeat=2) + for i, j in prod: + if 0 <= i and j < len(elements): + sum += floor(elements[i] / elements[j]) + return sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() -- cgit From 567a6451c2aa6fc0a89c71017e162aa33d920af0 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Mon, 13 Nov 2023 11:39:48 -0500 Subject: Challenge 242 by Jaldhar H. Vyas. --- challenge-242/jaldhar-h-vyas/blog.txt | 1 + challenge-242/jaldhar-h-vyas/perl/ch-1.pl | 32 +++++++++++++++++++++++++++++ challenge-242/jaldhar-h-vyas/perl/ch-2.pl | 8 ++++++++ challenge-242/jaldhar-h-vyas/raku/ch-1.raku | 19 +++++++++++++++++ challenge-242/jaldhar-h-vyas/raku/ch-2.raku | 10 +++++++++ 5 files changed, 70 insertions(+) create mode 100644 challenge-242/jaldhar-h-vyas/blog.txt create mode 100755 challenge-242/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-242/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-242/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-242/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-242/jaldhar-h-vyas/blog.txt b/challenge-242/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..bad309f0c4 --- /dev/null +++ b/challenge-242/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/11/perl_weekly_challenge_week_242.html diff --git a/challenge-242/jaldhar-h-vyas/perl/ch-1.pl b/challenge-242/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..150b04ab72 --- /dev/null +++ b/challenge-242/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use warnings; +use 5.030; + +sub setDifference { + my ($arr1, $arr2) = @_; + my %difference = map { $_ => 0 } @{$arr1}; + + for my $elem (@{$arr2}) { + if (exists $difference{$elem}) { + $difference{$elem}++; + } + } + + return sort grep { !$difference{$_} } keys %difference; +} + + +my ($arg1, $arg2) = @ARGV; + +my @arr1 = split q{ }, $arg1; +my @arr2 = split q{ }, $arg2; + +say q{(}, + ( + join q{, }, map { q{[} . ( join q{, }, $_ ) . q{]} } grep { length $_ } + ( + ( join q{, }, setDifference(\@arr1, \@arr2) ), + ( join q{, }, setDifference(\@arr2, \@arr1) ), + ) + ), +q{)}; diff --git a/challenge-242/jaldhar-h-vyas/perl/ch-2.pl b/challenge-242/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..1f58321076 --- /dev/null +++ b/challenge-242/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,8 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +my @matrix; +push @matrix, map { [split q{ }, $_] } @ARGV; +@matrix = map { [ map { $_ ^ 1 } reverse @{$_} ] } @matrix; +say q{(} . (join q{, }, map { q{[} . (join q{, }, @{$_}) . q{]} } @matrix) . q{)}; diff --git a/challenge-242/jaldhar-h-vyas/raku/ch-1.raku b/challenge-242/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..d81763d855 --- /dev/null +++ b/challenge-242/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/raku + +sub MAIN( + Str $arg1, + Str $arg2 +){ + my @arr1 = $arg1.split(q{ }); + my @arr2 = $arg2.split(q{ }); + + say q{(}, + ( + (@arr1 ∖ @arr2).keys.sort.join(q{, }), + (@arr2 ∖ @arr1).keys.sort.join(q{, }) + ) + .grep({ .chars }) + .map({ q{[} ~ $_.join(q{, }) ~ q{]} }) + .join(q{, }), + q{)}; +} \ No newline at end of file diff --git a/challenge-242/jaldhar-h-vyas/raku/ch-2.raku b/challenge-242/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..6f45993014 --- /dev/null +++ b/challenge-242/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/usr/bin/raku + +sub MAIN( + *@args +) { + my @matrix; + @matrix.push(| @args.map({ $_.split(q{ }).map({ $_.Int }) }) ); + @matrix = @matrix.map({ $_.reverse.map({ (!$_).Int }) }); + say q{(} ~ @matrix.map({ q{[} ~ $_.join(q{, }) ~ q{]} }).join(q{, }) ~ q{)}; +} \ No newline at end of file -- cgit From c6e4753bc493f3152eeef86c6e1c0e98f3db8171 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Nov 2023 20:25:38 +0200 Subject: PWC 243. Task 1 Raku done Task 2 Raku done. Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 Python done Task 2 Python done --- challenge-243/luca-ferrari/blog-1.txt | 1 + challenge-243/luca-ferrari/blog-2.txt | 1 + challenge-243/luca-ferrari/blog-3.txt | 1 + challenge-243/luca-ferrari/blog-4.txt | 1 + challenge-243/luca-ferrari/blog-5.txt | 1 + challenge-243/luca-ferrari/blog-6.txt | 1 + challenge-243/luca-ferrari/blog-7.txt | 1 + challenge-243/luca-ferrari/blog-8.txt | 1 + challenge-243/luca-ferrari/postgresql/ch-1.plperl | 25 +++++++++++++++++++ challenge-243/luca-ferrari/postgresql/ch-1.sql | 28 ++++++++++++++++++++++ challenge-243/luca-ferrari/postgresql/ch-2.plperl | 25 +++++++++++++++++++ challenge-243/luca-ferrari/postgresql/ch-2.sql | 28 ++++++++++++++++++++++ challenge-243/luca-ferrari/python/ch-1.py | 29 +++++++++++++++++++++++ challenge-243/luca-ferrari/python/ch-2.py | 28 ++++++++++++++++++++++ challenge-243/luca-ferrari/raku/ch-1.p6 | 22 +++++++++++++++++ challenge-243/luca-ferrari/raku/ch-2.p6 | 18 ++++++++++++++ 16 files changed, 211 insertions(+) create mode 100644 challenge-243/luca-ferrari/blog-1.txt create mode 100644 challenge-243/luca-ferrari/blog-2.txt create mode 100644 challenge-243/luca-ferrari/blog-3.txt create mode 100644 challenge-243/luca-ferrari/blog-4.txt create mode 100644 challenge-243/luca-ferrari/blog-5.txt create mode 100644 challenge-243/luca-ferrari/blog-6.txt create mode 100644 challenge-243/luca-ferrari/blog-7.txt create mode 100644 challenge-243/luca-ferrari/blog-8.txt create mode 100644 challenge-243/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-243/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-243/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-243/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-243/luca-ferrari/python/ch-1.py create mode 100644 challenge-243/luca-ferrari/python/ch-2.py create mode 100644 challenge-243/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-243/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-243/luca-ferrari/blog-1.txt b/challenge-243/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..9f781c0307 --- /dev/null +++ b/challenge-243/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task1 diff --git a/challenge-243/luca-ferrari/blog-2.txt b/challenge-243/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..50130d83b4 --- /dev/null +++ b/challenge-243/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task2 diff --git a/challenge-243/luca-ferrari/blog-3.txt b/challenge-243/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..3faf95cead --- /dev/null +++ b/challenge-243/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task1plperl diff --git a/challenge-243/luca-ferrari/blog-4.txt b/challenge-243/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..a23975ef5b --- /dev/null +++ b/challenge-243/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task2plperl diff --git a/challenge-243/luca-ferrari/blog-5.txt b/challenge-243/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..042c509f06 --- /dev/null +++ b/challenge-243/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task1plpgsql diff --git a/challenge-243/luca-ferrari/blog-6.txt b/challenge-243/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..815079b3fc --- /dev/null +++ b/challenge-243/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task2plpgsql diff --git a/challenge-243/luca-ferrari/blog-7.txt b/challenge-243/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..33035c70d2 --- /dev/null +++ b/challenge-243/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task1python diff --git a/challenge-243/luca-ferrari/blog-8.txt b/challenge-243/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..11e23b182c --- /dev/null +++ b/challenge-243/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/13/PerlWeeklyChallenge243.html#task2python diff --git a/challenge-243/luca-ferrari/postgresql/ch-1.plperl b/challenge-243/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..478c6552a5 --- /dev/null +++ b/challenge-243/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 243 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc243; + +CREATE OR REPLACE FUNCTION +pwc243.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + + my ( $nums ) = @_; + my @pairs; + + for my $i ( 0 .. $nums->@* ) { + for ( grep( { $nums->[ $i ] > 2 * $_ } $nums->@[ ( $i + 1 ) .. $nums->@* ] ) ) { + push @pairs, [ $nums->[ $i ], $_ ] if $_; + } + } + + return scalar @pairs; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-243/luca-ferrari/postgresql/ch-1.sql b/challenge-243/luca-ferrar