From e0f33dc8918149dfc29fdc1dfbb040c671851630 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 13 Nov 2023 17:46:15 +0000 Subject: - Added solutions by Ali Moradi. - Added solutions by Eric Cheung. - Added solutions by Mark Anderson. - Added solutions by E. Choroba. - Added solutions by Niels van Dijke. - Added solutions by PokGoPun. - Added solutions by Paulo Custodio. - Added solutions by Kjetil Skotheim. - Added solutions by W. Luis Mochan. - Added solutions by Steven Wilson. - Added solutions by David Ferrone. --- challenge-243/perlboy1967/perl/ch-1.pl | 41 +++++++++++++++++++++++++++++ challenge-243/perlboy1967/perl/ch-2.pl | 36 +++++++++++++++++++++++++ challenge-243/perlboy1967/perl/ch1.pl | 41 ----------------------------- challenge-243/perlboy1967/perl/ch2.pl | 36 ------------------------- challenge-243/steven-wilson/python/ch-01.py | 25 ------------------ challenge-243/steven-wilson/python/ch-02.py | 26 ------------------ challenge-243/steven-wilson/python/ch-1.py | 25 ++++++++++++++++++ challenge-243/steven-wilson/python/ch-2.py | 26 ++++++++++++++++++ 8 files changed, 128 insertions(+), 128 deletions(-) create mode 100755 challenge-243/perlboy1967/perl/ch-1.pl create mode 100755 challenge-243/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-243/perlboy1967/perl/ch1.pl delete mode 100755 challenge-243/perlboy1967/perl/ch2.pl delete mode 100644 challenge-243/steven-wilson/python/ch-01.py delete mode 100644 challenge-243/steven-wilson/python/ch-02.py create mode 100644 challenge-243/steven-wilson/python/ch-1.py create mode 100644 challenge-243/steven-wilson/python/ch-2.py (limited to 'challenge-243') diff --git a/challenge-243/perlboy1967/perl/ch-1.pl b/challenge-243/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..b0d3bf22dc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch-1.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/ch-2.pl b/challenge-243/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..78b89dbddc --- /dev/null +++ b/challenge-243/perlboy1967/perl/ch-2.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; diff --git a/challenge-243/perlboy1967/perl/ch1.pl b/challenge-243/perlboy1967/perl/ch1.pl deleted file mode 100755 index b0d3bf22dc..0000000000 --- a/challenge-243/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,41 +0,0 @@ -#!/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 deleted file mode 100755 index 78b89dbddc..0000000000 --- a/challenge-243/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,36 +0,0 @@ -#!/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; diff --git a/challenge-243/steven-wilson/python/ch-01.py b/challenge-243/steven-wilson/python/ch-01.py deleted file mode 100644 index 59f53277bf..0000000000 --- a/challenge-243/steven-wilson/python/ch-01.py +++ /dev/null @@ -1,25 +0,0 @@ -#!/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 deleted file mode 100644 index 4dc1b96750..0000000000 --- a/challenge-243/steven-wilson/python/ch-02.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/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() diff --git a/challenge-243/steven-wilson/python/ch-1.py b/challenge-243/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..59f53277bf --- /dev/null +++ b/challenge-243/steven-wilson/python/ch-1.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-2.py b/challenge-243/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..4dc1b96750 --- /dev/null +++ b/challenge-243/steven-wilson/python/ch-2.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