From 9a6935f8fcf04fd05aacf1dcba58f4353ab2b9d0 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 15 Jan 2024 19:49:25 +1100 Subject: pwc252 solution in python --- challenge-252/pokgopun/python/ch-1.py | 67 +++++++++++++++++++++++++++++++++ challenge-252/pokgopun/python/ch-2.py | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 challenge-252/pokgopun/python/ch-1.py create mode 100644 challenge-252/pokgopun/python/ch-2.py (limited to 'challenge-252') diff --git a/challenge-252/pokgopun/python/ch-1.py b/challenge-252/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..ae6a9021e9 --- /dev/null +++ b/challenge-252/pokgopun/python/ch-1.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +""" + +Task 1: Special Numbers + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the sum of the squares of all special elements + of the given array. +An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +Where n is the length of the given array. Also the array is 1-indexed for the ta +sk. + +Example 1 + +Input: @ints = (1, 2, 3, 4) +Output: 21 + +There are exactly 3 special elements in the given array: +$ints[1] since 1 divides 4, +$ints[2] since 2 divides 4, and +$ints[4] since 4 divides 4. + +Hence, the sum of the squares of all special elements of given array: +1 * 1 + 2 * 2 + 4 * 4 = 21. + +Example 2 + +Input: @ints = (2, 7, 1, 19, 18, 3) +Output: 63 + +There are exactly 4 special elements in the given array: +$ints[1] since 1 divides 6, +$ints[2] since 2 divides 6, +$ints[3] since 3 divides 6, and +$ints[6] since 6 divides 6. + +Hence, the sum of the squares of all special elements of given array: +2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 + +Task 2: Unique Sum Zero +""" +### solution by pokgopun@gmail.com + +def snum(tup: tuple): + l = len(tup) + return sum( + tup[i]**2 for i in range(0,l) + if l % (i+1) == 0 + ) + +import unittest + +class TestSnum(unittest.TestCase): + def test(self): + for inpt,otpt in { + (1, 2, 3, 4): 21, + (2, 7, 1, 19, 18, 3): 63, + }.items(): + self.assertEqual(snum(inpt),otpt) + +unittest.main() + + diff --git a/challenge-252/pokgopun/python/ch-2.py b/challenge-252/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..5eb2dc31da --- /dev/null +++ b/challenge-252/pokgopun/python/ch-2.py @@ -0,0 +1,70 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +""" + +Task 2: Unique Sum Zero + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + You are given an integer, $n. + + Write a script to find an array containing $n unique integers such that + they add up to zero. + +Example 1 + +Input: $n = 5 +Output: (-7, -1, 1, 3, 4) + +Two other possible solutions could be as below: +(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). + +Example 2 + +Input: $n = 3 +Output: (-1, 0, 1) + +Example 3 + +Input: $n = 1 +Output: (0) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import combinations + +def isUsz(n: int, tup: tuple): + # assuming array member is less than n, the min value for the member will be -(1 + .. + n - 1) + p = range(-sum(i for i in range(1,n)), n) + return set(tup) in ( set(cmb) for cmb in combinations(p,n) if sum(cmb)==0 ) + +import unittest + +class TestIsUsz(unittest.TestCase): + def test1(self): + for n,tup in { + 5: (-7, -1, 1, 3, 4), + 5: (-5, -1, 1, 2, 3), + 5: (-3, -1, 2, -2, 4), + 3: (-1, 0, 1), + 1: (0,), + }.items(): + self.assertEqual(isUsz(n,tup),True) + def test2(self): + for n,tup in { + 5: (-1, 0, 1, 2, 3), + 5: (-2, -1, 0, 1, 3), + 5: (-3, -1, 0, 1, 2), + 3: (-2, 0, 1), + 1: (1,), + }.items(): + self.assertEqual(isUsz(n,tup),False) + +unittest.main() -- cgit From 30da28de6974e7dad3e917b89662285e683e92ac Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 15 Jan 2024 09:34:52 +0000 Subject: Challenge 252 --- challenge-252/simon-proctor/raku/ch-1.raku | 26 ++++++++++++++++++++++++++ challenge-252/simon-proctor/raku/ch-2.raku | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 challenge-252/simon-proctor/raku/ch-1.raku create mode 100644 challenge-252/simon-proctor/raku/ch-2.raku (limited to 'challenge-252') diff --git a/challenge-252/simon-proctor/raku/ch-1.raku b/challenge-252/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..79b880dc11 --- /dev/null +++ b/challenge-252/simon-proctor/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +#| Run the test suite +multi sub MAIN('test') { + use Test; + is specials(1,2,3,4), (1,2,4); + is squares(1,2,4), (1,4,16); + is specials(2, 7, 1, 19, 18, 3), (2,7,1,3); + done-testing; +} + +#| Find the sum of the special numbers +multi sub MAIN( + *@vars where all(@vars) ~~ Int #= List of integers +) { + say [+] squares(specials(|@vars)); +} + +sub specials(*@vars) { + @vars.grep({ @vars.elems %% ++$ }); +} + +sub squares(*@vars) { + @vars.map( *² ); +} + diff --git a/challenge-252/simon-proctor/raku/ch-2.raku b/challenge-252/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..b4cf5b3963 --- /dev/null +++ b/challenge-252/simon-proctor/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +#| Run test suite +multi MAIN('test') { + use Test; + is number-list(4), (-2,-1,1,2); + for (1..10) -> $n { + is ([+] number-list($n)), 0; + } + done-testing; +} + +#| give a list of unqiue integers that add up to zero +multi sub MAIN( + $n where Int #= List length +) { + number-list($n).join(', ').say; +} + +multi sub number-list( $n where $n %% 2 ) { + my $p = $n div 2; + my @neg = (-$p..^0).list; + my @pos = @neg.map(* * -1); + ( |@neg, |@pos ).sort; +} + +multi sub number-list($n) { + ( |number-list($n-1), 0 ).sort; +} -- cgit From ee736fc4a988e77f5c1239895877565dd61c5153 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Jan 2024 09:57:33 +0000 Subject: w252 - Task 1 & 2 --- challenge-252/perlboy1967/perl/ch1.pl | 39 ++++++++++++++++++++++++++++++++++ challenge-252/perlboy1967/perl/ch2.pl | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 challenge-252/perlboy1967/perl/ch1.pl create mode 100755 challenge-252/perlboy1967/perl/ch2.pl (limited to 'challenge-252') diff --git a/challenge-252/perlboy1967/perl/ch1.pl b/challenge-252/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..0e2f5ffd72 --- /dev/null +++ b/challenge-252/perlboy1967/perl/ch1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 252 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-252 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Special Numbers +Submitted by: Mohammad S Anwar + +You are given an array of integers, @ints. + +Write a script to find the sum of the squares of all special elements of the given array. + +|| An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +|| Where n is the length of the given array. Also the array is 1-indexed for the task. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::Util qw(sum0); + +sub specialNumbers (@ints) { + my $n = $#ints + 1; + sum0 map { my $i = $_ - 1; + $n % $_ == 0 ? $ints[$i] ** 2 : 0 } 1 .. $n; +} + +is(specialNumbers(1,2,3,4),21); +is(specialNumbers(2,7,1,19,18,3),63); + +done_testing; diff --git a/challenge-252/perlboy1967/perl/ch2.pl b/challenge-252/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..9ae1906899 --- /dev/null +++ b/challenge-252/perlboy1967/perl/ch2.pl @@ -0,0 +1,40 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 252 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-252 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Unique Sum Zero +Submitted by: Mohammad S Anwar + +You are given an integer, $n. + +Write a script to find an array containing $n unique integers such that they add up to zero. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +sub uniqSumZero ($n) { + my @n = (0); + if ($n > 1) { + @n = sort { $a <=> $b } map { (-$_,$_) } 1 .. $n / 2; + splice(@n,$n>>1,1,0,$n[$n>>1]) if ($n % 2 != 0); + } + [@n]; +} + +is(uniqSumZero(1),[0]); +is(uniqSumZero(2),[-1,1]); +is(uniqSumZero(3),[-1,0,1]); +is(uniqSumZero(4),[-2,-1,1,2]); +is(uniqSumZero(5),[-2,-1,0,1,2]); + +done_testing; -- cgit From 919be22f93482ce1033cca5bde16c312605ff537 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 15 Jan 2024 09:59:40 +0000 Subject: Challenge 252 Solutions (Raku) --- challenge-252/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-252/mark-anderson/raku/ch-2.raku | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-252/mark-anderson/raku/ch-1.raku create mode 100644 challenge-252/mark-anderson/raku/ch-2.raku (limited to 'challenge-252') diff --git a/challenge-252/mark-anderson/raku/ch-1.raku b/challenge-252/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..61ee7f19da --- /dev/null +++ b/challenge-252/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Prime::Factor; +use Test; + +is special-numbers([1,2,3,4]), 21; +is special-numbers([2,7,1,19,18,3]), 63; + +sub special-numbers(@ints) +{ + @ints.unshift: Any; + + [+] @ints[divisors @ints.end] >>**>> 2 +} diff --git a/challenge-252/mark-anderson/raku/ch-2.raku b/challenge-252/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..cfc70ac832 --- /dev/null +++ b/challenge-252/mark-anderson/raku/ch-2.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku +use Test; + +is-deeply uniq-sum-zero(5), (-2,-1,1,2,0); +is-deeply uniq-sum-zero(3), (-1,1,0); +is-deeply uniq-sum-zero(1), (0,); +is-deeply uniq-sum-zero(0), (); +is-deeply uniq-sum-zero(-0), (); +is-deeply uniq-sum-zero(-1), (0,); +is-deeply uniq-sum-zero(-8), (-4,-3,-2,-1,1,2,3,4); +is-deeply uniq-sum-zero(-9), (-4,-3,-2,-1,1,2,3,4,0); + +multi uniq-sum-zero($n where 0) { () } + +multi uniq-sum-zero($n where 1|-1) { (0,) } + +multi uniq-sum-zero($n is copy) +{ + $n .= abs; + + my $i = $n div 2; + + (-$i,-$i+1...-1,1,2,3...$i,0)[^$n] +} -- cgit From a40acff603dcb5720d85b8d08a40650e4360f776 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Jan 2024 10:05:11 +0000 Subject: Task 1 - Delete not needed $i --- challenge-252/perlboy1967/perl/ch1.pl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/perlboy1967/perl/ch1.pl b/challenge-252/perlboy1967/perl/ch1.pl index 0e2f5ffd72..45f0b7d04e 100755 --- a/challenge-252/perlboy1967/perl/ch1.pl +++ b/challenge-252/perlboy1967/perl/ch1.pl @@ -29,8 +29,7 @@ use List::Util qw(sum0); sub specialNumbers (@ints) { my $n = $#ints + 1; - sum0 map { my $i = $_ - 1; - $n % $_ == 0 ? $ints[$i] ** 2 : 0 } 1 .. $n; + sum0 map { $n % $_ == 0 ? $ints[$_ - 1] ** 2 : 0 } 1 .. $n; } is(specialNumbers(1,2,3,4),21); -- cgit From df7d7976cc76e9574d2806c70ce0dd9af4b27929 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Jan 2024 10:20:02 +0000 Subject: Task 2 - reduce code by introducing: $i = $n >> 1 --- challenge-252/perlboy1967/perl/ch2.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/perlboy1967/perl/ch2.pl b/challenge-252/perlboy1967/perl/ch2.pl index 9ae1906899..c6ef2e5808 100755 --- a/challenge-252/perlboy1967/perl/ch2.pl +++ b/challenge-252/perlboy1967/perl/ch2.pl @@ -25,8 +25,9 @@ use Test2::V0; sub uniqSumZero ($n) { my @n = (0); if ($n > 1) { - @n = sort { $a <=> $b } map { (-$_,$_) } 1 .. $n / 2; - splice(@n,$n>>1,1,0,$n[$n>>1]) if ($n % 2 != 0); + my $i = $n >> 1; + @n = sort { $a <=> $b } map { (-$_,$_) } 1 .. $i; + splice(@n,$i,1,0,$n[$i]) if ($n % 2 != 0); } [@n]; } -- cgit From 6cedbb5cf07438403f6d9a58a0defd581e264a14 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Jan 2024 10:35:18 +0000 Subject: Task 2 - Simplify code --- challenge-252/perlboy1967/perl/ch2.pl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/perlboy1967/perl/ch2.pl b/challenge-252/perlboy1967/perl/ch2.pl index c6ef2e5808..225f32ac2a 100755 --- a/challenge-252/perlboy1967/perl/ch2.pl +++ b/challenge-252/perlboy1967/perl/ch2.pl @@ -25,11 +25,10 @@ use Test2::V0; sub uniqSumZero ($n) { my @n = (0); if ($n > 1) { - my $i = $n >> 1; - @n = sort { $a <=> $b } map { (-$_,$_) } 1 .. $i; - splice(@n,$i,1,0,$n[$i]) if ($n % 2 != 0); + @n = map { (-$_,$_) } 1 .. $n >> 1; + push(@n,0) if ($n % 2 != 0); } - [@n]; + [sort { $a <=> $b } @n]; } is(uniqSumZero(1),[0]); -- cgit From e35ca085014d2015a5fd5b756ba3058b6f88865c Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 15 Jan 2024 22:17:19 +1100 Subject: pwc252 solution in go --- challenge-252/pokgopun/go/ch-1.go | 80 +++++++++++++++++++++ challenge-252/pokgopun/go/ch-2.go | 144 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 challenge-252/pokgopun/go/ch-1.go create mode 100644 challenge-252/pokgopun/go/ch-2.go (limited to 'challenge-252') diff --git a/challenge-252/pokgopun/go/ch-1.go b/challenge-252/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..af61241188 --- /dev/null +++ b/challenge-252/pokgopun/go/ch-1.go @@ -0,0 +1,80 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +/*# + +Task 1: Special Numbers + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the sum of the squares of all special elements + of the given array. +An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +Where n is the length of the given array. Also the array is 1-indexed for the ta +sk. + +Example 1 + +Input: @ints = (1, 2, 3, 4) +Output: 21 + +There are exactly 3 special elements in the given array: +$ints[1] since 1 divides 4, +$ints[2] since 2 divides 4, and +$ints[4] since 4 divides 4. + +Hence, the sum of the squares of all special elements of given array: +1 * 1 + 2 * 2 + 4 * 4 = 21. + +Example 2 + +Input: @ints = (2, 7, 1, 19, 18, 3) +Output: 63 + +There are exactly 4 special elements in the given array: +$ints[1] since 1 divides 6, +$ints[2] since 2 divides 6, +$ints[3] since 3 divides 6, and +$ints[6] since 6 divides 6. + +Hence, the sum of the squares of all special elements of given array: +2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 + +Task 2: Unique Sum Zero +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) snum() int { + l := len(is) + r := 0 + for i, v := range is { + if l%(i+1) == 0 { + r += v * v + } + } + return r +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{1, 2, 3, 4}, 21}, + {ints{2, 7, 1, 19, 18, 3}, 63}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.snum(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-252/pokgopun/go/ch-2.go b/challenge-252/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..d8d9654546 --- /dev/null +++ b/challenge-252/pokgopun/go/ch-2.go @@ -0,0 +1,144 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +/*# + +Task 2: Unique Sum Zero + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + You are given an integer, $n. + + Write a script to find an array containing $n unique integers such that + they add up to zero. + +Example 1 + +Input: $n = 5 +Output: (-7, -1, 1, 3, 4) + +Two other possible solutions could be as below: +(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). + +Example 2 + +Input: $n = 3 +Output: (-1, 0, 1) + +Example 3 + +Input: $n = 1 +Output: (0) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st January + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +func isUsz(n int, s []int) bool { + slices.Sort(s) + l := 0 + for i := 1; i < n; i++ { + l -= i + } + p := make([]int, n-l) + for i := 0; i < n-l; i++ { + p[i] = l + i + } + for v := range Combinations(p, uint(n)) { + sum := 0 + for _, i := range v { + sum += i + } + if sum == 0 && cmp.Diff(v, s) == "" { + return true + } + } + return false +} + +type ints []int + +func main() { + for _, data := range []struct { + n int + arr ints + res bool + }{ + {5, ints{-7, -1, 1, 3, 4}, true}, + {5, ints{-5, -1, 1, 2, 3}, true}, + {5, ints{-3, -1, 2, -2, 4}, true}, + {3, ints{-1, 0, 1}, true}, + {1, ints{0}, true}, + {5, ints{-6, -1, 1, 3, 4}, false}, + {5, ints{-5, 1, 1, 2, 3}, false}, + {5, ints{-3, -1, 1, 2, 4}, false}, + {3, ints{-2, 0, 1}, false}, + {1, ints{1}, false}, + } { + io.WriteString(os.Stdout, cmp.Diff(isUsz(data.n, data.arr), data.res)) // blank if ok, otherwise show the difference + } +} + +// transcribed from https://docs.python.org/3/library/itertools.html#itertools.combinations +func Combinations[E any](s []E, r uint) chan []E { + res := make(chan []E) + n := uint(len(s)) + if r > n { + go func() { + res <- []E{} + close(res) + }() + return res + } + go func() { + res <- s[:r] + idx := make([]uint, r) + for i := range idx { + idx[i] = uint(i) + } + var i, j uint + for { + i = r + for { + i-- + if idx[i] != i+n-r { + break + } + if i > 0 { + continue + } + close(res) + return + } + idx[i]++ + j = i + 1 + for j < r { + idx[j] = idx[j-1] + 1 + j++ + } + result := make([]E, r) + i = 0 + for _, v := range idx { + result[i] = s[v] + i++ + } + res <- result + } + close(res) + }() + return res +} -- cgit From a200f84e96cb99ec38b7d0cc2e8db325f25842fa Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 15 Jan 2024 11:25:06 +0000 Subject: Task 1 - Squeeze in 'grep()' --- challenge-252/perlboy1967/perl/ch1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-252') diff --git a/challenge-252/perlboy1967/perl/ch1.pl b/challenge-252/perlboy1967/perl/ch1.pl index 45f0b7d04e..e8ae528469 100755 --- a/challenge-252/perlboy1967/perl/ch1.pl +++ b/challenge-252/perlboy1967/perl/ch1.pl @@ -29,7 +29,7 @@ use List::Util qw(sum0); sub specialNumbers (@ints) { my $n = $#ints + 1; - sum0 map { $n % $_ == 0 ? $ints[$_ - 1] ** 2 : 0 } 1 .. $n; + sum0 map { $ints[$_ - 1] ** 2 } grep { $n % $_ == 0 } 1 .. $n; } is(specialNumbers(1,2,3,4),21); -- cgit From 65a2d0d671dfe4c0ce3e2664b6ac7abce8f0856c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 15 Jan 2024 09:12:38 +0100 Subject: PWC 252 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 Task 1 Java done Task 2 Java done --- challenge-252/luca-ferrari/blog-1.txt | 1 + challenge-252/luca-ferrari/blog-2.txt | 1 + challenge-252/luca-ferrari/blog-3.txt | 1 + challenge-252/luca-ferrari/blog-4.txt | 1 + challenge-252/luca-ferrari/blog-5.txt | 1 + challenge-252/luca-ferrari/blog-6.txt | 1 + challenge-252/luca-ferrari/blog-7.txt | 1 + challenge-252/luca-ferrari/blog-8.txt | 1 + challenge-252/luca-ferrari/blog_10.txt | 1 + challenge-252/luca-ferrari/blog_9.txt | 1 + challenge-252/luca-ferrari/java/ch_1.java | 13 +++++++ challenge-252/luca-ferrari/java/ch_2.java | 40 ++++++++++++++++++++++ challenge-252/luca-ferrari/postgresql/ch-1.plperl | 24 +++++++++++++ challenge-252/luca-ferrari/postgresql/ch-1.sql | 29 ++++++++++++++++ challenge-252/luca-ferrari/postgresql/ch-2.plperl | 37 ++++++++++++++++++++ challenge-252/luca-ferrari/postgresql/ch-2.sql | 41 +++++++++++++++++++++++ challenge-252/luca-ferrari/python/ch-1.py | 27 +++++++++++++++ challenge-252/luca-ferrari/python/ch-2.py | 39 +++++++++++++++++++++ challenge-252/luca-ferrari/raku/ch-1.p6 | 12 +++++++ challenge-252/luca-ferrari/raku/ch-2.p6 | 23 +++++++++++++ 20 files changed, 295 insertions(+) create mode 100644 challenge-252/luca-ferrari/blog-1.txt create mode 100644 challenge-252/luca-ferrari/blog-2.txt create mode 100644 challenge-252/luca-ferrari/blog-3.txt create mode 100644 challenge-252/luca-ferrari/blog-4.txt create mode 100644 challenge-252/luca-ferrari/blog-5.txt create mode 100644 challenge-252/luca-ferrari/blog-6.txt create mode 100644 challenge-252/luca-ferrari/blog-7.txt create mode 100644 challenge-252/luca-ferrari/blog-8.txt create mode 100644 challenge-252/luca-ferrari/blog_10.txt create mode 100644 challenge-252/luca-ferrari/blog_9.txt create mode 100644 challenge-252/luca-ferrari/java/ch_1.java create mode 100644 challenge-252/luca-ferrari/java/ch_2.java create mode 100644 challenge-252/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-252/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-252/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-252/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-252/luca-ferrari/python/ch-1.py create mode 100644 challenge-252/luca-ferrari/python/ch-2.py create mode 100644 challenge-252/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-252/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-252') diff --git a/challenge-252/luca-ferrari/blog-1.txt b/challenge-252/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..f410e3161f --- /dev/null +++ b/challenge-252/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task1 diff --git a/challenge-252/luca-ferrari/blog-2.txt b/challenge-252/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..175a30bc9d --- /dev/null +++ b/challenge-252/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task2 diff --git a/challenge-252/luca-ferrari/blog-3.txt b/challenge-252/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..153b11f11a --- /dev/null +++ b/challenge-252/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task1plperl diff --git a/challenge-252/luca-ferrari/blog-4.txt b/challenge-252/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..df5d6c25dc --- /dev/null +++ b/challenge-252/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task2plperl diff --git a/challenge-252/luca-ferrari/blog-5.txt b/challenge-252/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..8db6bdda83 --- /dev/null +++ b/challenge-252/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task1plpgsql diff --git a/challenge-252/luca-ferrari/blog-6.txt b/challenge-252/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..122385a3fa --- /dev/null +++ b/challenge-252/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task2plpgsql diff --git a/challenge-252/luca-ferrari/blog-7.txt b/challenge-252/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..b9eb4b6eb6 --- /dev/null +++ b/challenge-252/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task1python diff --git a/challenge-252/luca-ferrari/blog-8.txt b/challenge-252/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..a94acff5f2 --- /dev/null +++ b/challenge-252/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task2python diff --git a/challenge-252/luca-ferrari/blog_10.txt b/challenge-252/luca-ferrari/blog_10.txt new file mode 100644 index 0000000000..addc13bcda --- /dev/null +++ b/challenge-252/luca-ferrari/blog_10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task1java diff --git a/challenge-252/luca-ferrari/blog_9.txt b/challenge-252/luca-ferrari/blog_9.txt new file mode 100644 index 0000000000..6d2f931e4d --- /dev/null +++ b/challenge-252/luca-ferrari/blog_9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/01/15/PerlWeeklyChallenge252.html#task2java diff --git a/challenge-252/luca-ferrari/java/ch_1.java b/challenge-252/luca-ferrari/java/ch_1.java new file mode 100644 index 0000000000..c903dfbb88 --- /dev/null +++ b/challenge-252/luca-ferrari/java/ch_1.java @@ -0,0 +1,13 @@ +public class ch_1 { + public static void main( String argv[] ) { + int sum = 0; + for ( int i = 0; i < argv.length; i++ ) { + if ( argv.length % ( i + 1 ) != 0 ) + continue; + + sum += Math.pow( Integer.parseInt( argv[ i ] ), 2 ); + } + + System.out.println( sum ); + } +} diff --git a/challenge-252/luca-ferrari/java/ch_2.java b/challenge-252/luca-ferrari/java/ch_2.java new file mode 100644 index 0000000000..5a4b46083a --- /dev/null +++ b/challenge-252/luca-ferrari/java/ch_2.java @@ -0,0 +1,40 @@ +import java.util.*; + +public class ch_2 { + public static void main( String argv[] ) throws Exception { + int size = Integer.parseInt( argv[ 0 ] ); + + if ( size <= 3 ) + throw new Exception( "Cannot work with a size less than 3!" ); + + List nums = new LinkedList(); + + + if ( size % 2 == 0 ) { + for ( int i = 1; i < size / 2 ; i++ ) { + nums.add( i ); + nums.add( i * -1 ); + } + } + else { + for ( int i = 1; i < ( size - 1 ) / 2 ; i++ ) { + nums.add( i ); + nums.add( i * -1 ); + } + + int next = ( size - 1 ) / 2 + 1; + nums.add( next ); + nums.add( next + 1 ); + nums.add( ( next * 2 + 1 ) * -1 ); + } + + + boolean printedOne = false; + for ( int i : nums ) { + System.out.print( i + ( printedOne ? ", " : "" ) ); + printedOne = true; + } + + System.out.println(); + } +} diff --git a/challenge-252/luca-ferrari/postgresql/ch-1.plperl b/challenge-252/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..5d8298c3e7 --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- +-- Perl Weekly Challenge 252 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc252; + +CREATE OR REPLACE FUNCTION +pwc252.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $nums ) = @_; + + my ( $sum ) = 0; + + for ( 0 .. $nums->@* - 1 ) { + next if $nums->@* % ( $_ + 1 ) != 0; + $sum += $nums->@[ $_ ] ** 2; + } + + return( $sum ); +$CODE$ +LANGUAGE plperl; diff --git a/challenge-252/luca-ferrari/postgresql/ch-1.sql b/challenge-252/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..2e629884e7 --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,29 @@ +-- +-- Perl Weekly Challenge 252 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc252; + +CREATE OR REPLACE FUNCTION +pwc252.task1_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + sumx int := 0; +BEGIN + + FOR i IN 1 .. array_length( nums, 1 ) LOOP + IF mod( array_length( nums, 1 ), i ) <> 0 THEN + CONTINUE; + END IF; + + sumx := sumx + pow( nums[ i ], 2 ); + END LOOP; + + RETURN sumx; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-252/luca-ferrari/postgresql/ch-2.plperl b/challenge-252/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..a7abf56217 --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 252 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc252; + +CREATE OR REPLACE FUNCTION +pwc252.task2_plperl( int ) +RETURNS SETOF int +AS $CODE$ + my ( $size ) = @_; + + die "Cannot have a size less than 3!" if ( $size <= 3 ); + + if ( $size % 2 == 0 ) { + for ( 1 .. $size / 2 ) { + return_next( $_ ); + return_next( $_ * -1 ); + } + } + else { + for ( 1 .. ( $size - 1 ) / 2 ) { + return_next( $_ ); + return_next( $_ * -1 ); + } + + my $next = int( $size / 2 ) + 1; + return_next( $next ); + return_next( $next + 1 ); + return_next( ( $next + $next + 1 ) * -1 ); + } + +return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-252/luca-ferrari/postgresql/ch-2.sql b/challenge-252/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..b97601e8fa --- /dev/null +++ b/challenge-252/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,41 @@ +-- +-- Perl Weekly Challenge 252 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc252; + +CREATE OR REPLACE FUNCTION +pwc252.task2_plpgsql( size int ) +RETURNS SETOF int +AS $CODE$ +DECLARE + next_value int; +BEGIN + + IF size <= 3 THEN + RAISE 'Cannot work with a size less than 3!'; + END IF; + + IF mod( size, 2 ) = 0 THEN + FOR i IN 1 .. ( size / 2 ) LOOP + RETURN NEXT i; + RETURN NEXT (i * -1); + END LOOP; + ELSE + FOR i IN 2 .. ( size - 1 ) / 2 LOOP + RETURN NEXT i; + RETURN NEXT (i * -1); + END LOOP; + + next_value := ( size - 1 ) / 2 + 1; + RETURN NEXT next_value; + RETURN NEXT next_value + 1; + RETURN NEXT ( next_value + next_value + 1 ) * -1; + END IF; +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-252/luca-ferrari/python/ch-1.py b/challenge-252/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..cae2cc2c1c --- /dev/null +++ b/challenge-252/luca-ferrari/python/ch-1.py @@ -0,0 +1,27 @@ +#!python + +# +# Perl Weekly Challenge 252 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def main( argv ): + nums = list( map( int, argv ) ) + sum = 0 + for i in range( 0, len( nums ) ): + if len( nums ) % ( i + 1 ) == 0: + sum += nums[ i ] ** 2; + + return sum + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) + diff --git a/challenge-252/luca-ferrari/python/ch-2.py b/challenge-252/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..4862800e92 --- /dev/null +++ b/challenge-252/luca-ferrari/python/ch-2.py @@ -0,0 +1,39 @@ +#!python + +# +# Perl Weekly Challenge 252 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def main( argv ): + size = int( argv[ 0 ] ) + nums = [] + + if size % 2 == 0: + for i in range( 1, size / 2 ): + nums.append( i ) + nums.append( i * -1 ) + else: + for i in range( 1, int( ( size - 1 ) / 2 ) ): + nums.append( i ) + nums.append( i * -1 ) + + next = max( nums ) + 1 + nums.append( next ) + nums.append( next + 1 ) + nums.append( ( next + next + 1 ) * -1 ) + + return nums + + + +# invoke the main without the command itself +if __name__ == '__main__': + print( main( sys.argv[ 1: ] ) ) + diff --git a/challenge-252/luca-ferrari/raku/ch-1.p6 b/challenge-252/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..3dbb8a96cc --- /dev/null +++ b/challenge-252/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 252 +# Task 1 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + @nums.kv.map( { @nums.elems %% ( $^index + 1 ) ?? $^value ** 2 !! 0 } ).sum.say; +} diff --git a/challenge-252/luca-ferrari/raku/ch-2.p6 b/challenge-252/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..952e96e397 --- /dev/null +++ b/challenge-252/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,23 @@ +#!raku + +# +# Perl Weekly Challenge 252 +# Task 2 +# +# See +# + +sub MAIN( $n where { $n >= 3 && $n ~~ Int } ) { + my @nums; + + if $n %% 2 { + @nums.push: $_, $_ * -1 for 1 .. ( $n / 2 ); + } + else { + @nums.push: $_, $_ * -1 for 1 .. ( ( $n - 2 ) / 2 ); + my $next = @nums.max + 1; + @nums.push: $next, $next + 1, ( $next + $next + 1 ) * -1; + } + + @nums.join( ',' ).say; +} -- cgit From 04368ad24f28250401393f1096c9f6a323e9ef7a Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 15 Jan 2024 12:35:05 +0100 Subject: Solve 252: Special Numbers & Unique Sum Zero by E. Choroba --- challenge-252/e-choroba/perl/ch-1.pl | 16 ++++++++++++++++ challenge-252/e-choroba/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100755 challenge-252/e-choroba/perl/ch-1.pl create mode 100755 challenge-252/e-choroba/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/e-choroba/perl/ch-1.pl b/challenge-252/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6b5022d0c3 --- /dev/null +++ b/challenge-252/e-choroba/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub special_numbers(@ints) { + my @s = @ints[grep 0 == @ints % ($_ + 1), 0 .. $#ints]; + return sum(map $_ * $_, @s) +} + +use Test::More tests => 2; + +is special_numbers(1, 2, 3, 4), 21, 'Example 1'; +is special_numbers(2, 7, 1, 19, 18, 3), 63, 'Example 2'; diff --git a/challenge-252/e-choroba/perl/ch-2.pl b/challenge-252/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..321c4500cc --- /dev/null +++ b/challenge-252/e-choroba/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub unique_sum_zero($n) { + return [(0) x ($n % 2), map { $_, -$_ } 1 .. $n / 2] +} + +use Test::More tests => 7; +use List::Util qw{ sum uniq }; + +subtest "n=$_" => sub { + plan tests => 3; + my $output = unique_sum_zero($_); + is scalar @$output, $_, 'length'; + my @u = uniq(@$output); + is scalar @u, $_, 'uniq'; + is sum(@$output), 0, 'sum 0'; +} for 1 .. 7; -- cgit From 4f6ce750b551df16ffeb86f5010545cc4cefbe42 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 15 Jan 2024 07:37:38 -0600 Subject: Week 252 Task 1 done --- challenge-252/bob-lied/README | 6 ++-- challenge-252/bob-lied/perl/ch-1.pl | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 challenge-252/bob-lied/perl/ch-1.pl (limited to 'challenge-252') diff --git a/challenge-252/bob-lied/README b/challenge-252/bob-lied/README index 1fb5d8a320..8b958edfe8 100644 --- a/challenge-252/bob-lied/README +++ b/challenge-252/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 251 by Bob Lied +Solutions to weekly challenge 252 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-251/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-251/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-252/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-252/bob-lied diff --git a/challenge-252/bob-lied/perl/ch-1.pl b/challenge-252/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..35d16224fc --- /dev/null +++ b/challenge-252/bob-lied/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# +# ch-1.pl Perl Weekly Challenge 252 Task 1 Special Numbers +#============================================================================= +# You are given an array of integers, @ints. +# Write a script to find the sum of the squares of all special elements of +# the given array. An element $int[i] of @ints is called special if +# i divides n, i.e. n % i == 0. Where n is the length of the given array. +# Also the array is 1-indexed for the task. +# Example 1 Input: @ints = (1, 2, 3, 4) +# Output: 21 +# There are exactly 3 special elements in the given array: +# $ints[1] since 1 divides 4, +# $ints[2] since 2 divides 4, and +# $ints[4] since 4 divides 4. +# Hence, the sum of the squares of all special elements of given array: +# 1 * 1 + 2 * 2 + 4 * 4 = 21. +# Example 2 Input: @ints = (2, 7, 1, 19, 18, 3) +# Output: 63 +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; +use List::Util qw/sum0/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub specialNumbers(@ints) +{ + my $len = @ints; + # Insert an extra element at the front to make it 1-indexed + unshift @ints, $len+1; + + return sum0 map { $_ * $_ } @ints[ grep { $len % $_ == 0 } 1 .. $len ]; +} + +sub runTest +{ + use Test2::V0; + + is( specialNumbers(1,2,3,4), 21, "Example 1"); + is( specialNumbers(2,7,1,19,18,3), 63, "Example 2"); + is( specialNumbers(8 ), 64, "Singleton"); + is( specialNumbers() , 0, "Empty list"); + + done_testing; +} -- cgit From 73704e823a01f00d461dac41dfae852471186d34 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Mon, 15 Jan 2024 22:29:52 +0800 Subject: Week 252 --- challenge-252/cheok-yin-fung/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-252/cheok-yin-fung/perl/ch-2.pl | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenge-252/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-252/cheok-yin-fung/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/cheok-yin-fung/perl/ch-1.pl b/challenge-252/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..d76ccfecda --- /dev/null +++ b/challenge-252/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,20 @@ +# The Weekly Challenge 252 +# Task 1 Special Numbers +use v5.30.0; +use warnings; +use Math::Prime::Util qw/divisors/; + +sub sn { + my @ints = @_; + my @divisors = divisors(scalar @ints); + my $sum = 0; + for (@divisors) { + my $j = $_-1; + $sum += $ints[$j]*$ints[$j] + } + return $sum; +} + +use Test::More tests=>2; +ok sn(1,2,3,4)==21; +ok sn(2, 7, 1, 19, 18, 3)==63; diff --git a/challenge-252/cheok-yin-fung/perl/ch-2.pl b/challenge-252/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..03c3366077 --- /dev/null +++ b/challenge-252/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,23 @@ +# The Weekly Challenge 252 +# Task 2 Unique Sum Zero +# Generate all combinations sum to zero, +# [optionally] given a lower bound and an upper bound +use v5.30.0; +use warnings; +use List::Util qw/sum/; +use Algorithm::Combinatorics qw/combinations/; + +sub sz { + my $n = $_[0]; + die "Invalid n\n" if $n <= 0; + my $lower = $_[1] || -$n; + my $upper = $_[2] || $n; + my @arr = ($lower..$upper); + my $iter = combinations(\@arr, $n); + while (my $c = $iter->next) { + my @com = $c->@*; + say "@com" if 0 == sum @com; + } +} + +sz @ARGV || (5,-5,4); -- cgit From cfbda4f35def49228312b4327b001704e78300aa Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 15 Jan 2024 14:48:59 +0000 Subject: Special unique solutions for week 252 --- challenge-252/peter-campbell-smith/blog.txt | 1 + challenge-252/peter-campbell-smith/perl/ch-1.pl | 39 ++++++++++++++++ challenge-252/peter-campbell-smith/perl/ch-2.pl | 59 +++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 challenge-252/peter-campbell-smith/blog.txt create mode 100755 challenge-252/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-252/peter-campbell-smith/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/peter-campbell-smith/blog.txt b/challenge-252/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..f879c1046a --- /dev/null +++ b/challenge-252/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/252 diff --git a/challenge-252/peter-campbell-smith/perl/ch-1.pl b/challenge-252/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..4dda903106 --- /dev/null +++ b/challenge-252/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2024-01-15 +use utf8; # Week 252 task 1 - Special numbers +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge +binmode STDOUT, ':utf8'; + +my ($j, @ints); + +special_numbers(1, 2, 3, 4); +special_numbers(2, 7, 1, 19, 18, 3); +special_numbers(7, 7, 7, 7, 11); + +for $j (0 .. 31) { + push @ints, int(rand(20) + 1); +} +special_numbers(@ints); + +sub special_numbers { + + my (@ints, $j, $squares, @specials, $last); + + # initialise + @ints = @_; + $last = @ints - 1; + + # check each member + for $j (0 .. $last) { + if (($last + 1) / ($j + 1) == int(($last + 1) / ($j + 1))) { + $squares += $ints[$j] ** 2; + push @specials, $j + 1; + } + } + + # show results + say qq[\nInput: \@ints = (] . join(', ', @ints) . ')'; + say qq[Output: $squares ∵ \@specials: ] . join(', ', @specials); +} diff --git a/challenge-252/peter-campbell-smith/perl/ch-2.pl b/challenge-252/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..09ebb5bb0c --- /dev/null +++ b/challenge-252/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2024-01-15 +use utf8; # Week 252 task 2 - Unique sum zero +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge +binmode STDOUT, ':utf8'; + +unique_sum_zero(1); +unique_sum_zero(5); +unique_sum_zero(50); + +sub unique_sum_zero { + + my ($n, $method, $sum, $j, @array, %used); + + $n = shift @_; + say qq[\nInput: \$n = $n]; + + # try two ways + for $method (1 .. 2) { + @array = (); + + # use -n to +n, including 0 in the middle if an odd number + if ($method == 1) { + push @array, 0 if $n & 1; + for $j (1 .. int($n / 2)) { + push @array, -$j; + unshift @array, $j + } + + # choose $n - 1 random numbers and add - $sum - unless it has already been used + } elsif ($method == 2) { + while (1) { + $sum = 0; + @array = (); + %used = (); + + # fill @array with $n - 1 random integers from the range -$n to +$n + while (@array < $n - 1) { + $j = int(rand($n * 2)) - $n; + next if $used{$j}; + $used{$j} = 1; + push @array, $j; + $sum += $j; + } + + # add the negated sum unless it has been used already + push @array, -$sum; + last unless $used{-$sum}; + } + } + + # show result + $sum = 0; + $sum += $array[$_] for 0 .. $n - 1; + say qq[Output: (] . join(', ', @array) . qq[), \$sum = $sum]; + } +} -- cgit From 244a9670f9e964e12744ee0f93dfea0b6c3bd41c Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 15 Jan 2024 09:20:33 -0600 Subject: Week 252 complete --- challenge-252/bob-lied/perl/ch-2.pl | 87 +++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 challenge-252/bob-lied/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/bob-lied/perl/ch-2.pl b/challenge-252/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..505be53aed --- /dev/null +++ b/challenge-252/bob-lied/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 252 Task 2 Unique Sum Zero +#============================================================================= +# You are given an integer, $n. Write a script to find an array containing +# $n unique integers such that they add up to zero. +# Example 1 Input: $n = 5 +# Output: (-7, -1, 1, 3, 4) +# Two other possible solutions could be as below: +# (-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). +# Example 2 Input: $n = 3 +# Output: (-1, 0, 1) +# Example 3 Input: $n = 1 +# Output: (0) +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub usz($n) +{ + my @list; + my $pick = 2; + while ( $n > 0 ) + { + if ( $n == 1 ) + { + push @list, 0; + $n -= 1; + } + elsif ( $n == 2 ) + { + push @list, (1, -1); + $n -= 2; + } + else + { + push @list, ( $pick, $pick+1, -($pick+$pick+1) ); + $pick += 2; + $n -= 3; + } + } + say "\@usz=(@list)" if $Verbose; + return @list; +} + +sub usz2($n) +{ + return ( $n == 0 ? () : ( (1 .. $n-1), -( $n*($n-1)/2 ) ) ); +} + +sub runTest +{ + use Test2::V0; + use List::Util qw/sum0 uniqint/; + + my @usz; + + for my $n ( 0 .. 5 ) + { + @usz = usz($n); + is( scalar(@usz), $n, "n=$n count"); + is( sum0(@usz), 0, "n=$n sum"); + is( scalar(uniqint(@usz)), scalar(@usz), "n=$n unique"); + } + + for my $n ( reverse 0 .. 5 ) + { + @usz = usz2($n); + is( scalar(@usz), $n, "usz2 n=$n count"); + is( sum0(@usz), 0, "nusz2 =$n sum"); + is( scalar(uniqint(@usz)), scalar(@usz), "nusz2 =$n unique"); + } + + done_testing; +} -- cgit From 76a57e5d996ef75edcaa011991818b5d2665e506 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 15 Jan 2024 10:24:58 -0600 Subject: Solve PWC252 --- challenge-252/wlmb/blog.txt | 1 + challenge-252/wlmb/perl/ch-1.pl | 12 ++++++++++++ challenge-252/wlmb/perl/ch-2.pl | 14 ++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 challenge-252/wlmb/blog.txt create mode 100755 challenge-252/wlmb/perl/ch-1.pl create mode 100755 challenge-252/wlmb/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/wlmb/blog.txt b/challenge-252/wlmb/blog.txt new file mode 100644 index 0000000000..2490bf4e93 --- /dev/null +++ b/challenge-252/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/01/15/PWC252/ diff --git a/challenge-252/wlmb/perl/ch-1.pl b/challenge-252/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..e42ac0e2ff --- /dev/null +++ b/challenge-252/wlmb/perl/ch-1.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +# Perl weekly challenge 252 +# Task 1: Special Numbers +# +# See https://wlmb.github.io/2024/01/15/PWC252/#task-1-special-numbers +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to sum the squares of the special elements of N1 N2... + FIN +say "@ARGV -> ", sum0 map {$ARGV[$_-1]**2} grep {@ARGV%$_==0} 1..@ARGV; diff --git a/challenge-252/wlmb/perl/ch-2.pl b/challenge-252/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..927509eb6c --- /dev/null +++ b/challenge-252/wlmb/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 252 +# Task 2: Unique Sum Zero +# +# See https://wlmb.github.io/2024/01/15/PWC252/#task-2-unique-sum-zero +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to obtain arrays of N_j numbers that add up to zero. + FIN +for(@ARGV){ + warn("Input must be greater than one"), next unless $_>=1; + say "$_ -> [", join(" ", -$_/2..-1, $_%2?(0):(), 1..$_/2), "]"; +} -- cgit From faf09e55883803345ac3dd71b7055ff61479be3a Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 15 Jan 2024 10:29:26 -0600 Subject: Week 252, alternative solution for task 1 --- challenge-252/bob-lied/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'challenge-252') diff --git a/challenge-252/bob-lied/perl/ch-1.pl b/challenge-252/bob-lied/perl/ch-1.pl index 35d16224fc..90da967a76 100644 --- a/challenge-252/bob-lied/perl/ch-1.pl +++ b/challenge-252/bob-lied/perl/ch-1.pl @@ -44,6 +44,26 @@ sub specialNumbers(@ints) return sum0 map { $_ * $_ } @ints[ grep { $len % $_ == 0 } 1 .. $len ]; } +sub factorsOf($n) +{ + use List::Util qw/uniqint/; + my @flist = (1, $n); + for my $f ( 2 .. int(sqrt($n)) ) + { + push @flist, ($f, $n/$f) if $n % $f == 0; + } + return [ uniqint sort { $a <=> $b } @flist ] +} + +sub sn2(@ints) +{ + use List::MoreUtils qw/before/; + my $len = @ints; + return 0 if $len == 0; + my @choose = map { $_ - 1 } before { $_ > $len } factorsOf($len)->@*; + return sum0 map { $_ * $_ } @ints[@choose]; +} + sub runTest { use Test2::V0; @@ -53,5 +73,13 @@ sub runTest is( specialNumbers(8 ), 64, "Singleton"); is( specialNumbers() , 0, "Empty list"); + is( sn2(1,2,3,4), 21, "sn2 Example 1"); + is( sn2(2,7,1,19,18,3), 63, "sn2 Example 2"); + is( sn2(8 ), 64, "sn2 Singleton"); + is( sn2() , 0, "sn2 Empty list"); + + is( factorsOf( 6), [1,2,3,6], "factorsOf 6"); + is( factorsOf(36), [1,2,3,4,6,9,12,18,36], "factorsOf 36"); + done_testing; } -- cgit From ff8c1a85f0c2e5e5b6cae9721f8462dcf6cfe7eb Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 15 Jan 2024 17:06:15 +0000 Subject: Challenge 252 Solutions (Raku) --- challenge-252/mark-anderson/raku/ch-1.raku | 1 - challenge-252/mark-anderson/raku/ch-2.raku | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/mark-anderson/raku/ch-1.raku b/challenge-252/mark-anderson/raku/ch-1.raku index 61ee7f19da..5de0eeaf33 100644 --- a/challenge-252/mark-anderson/raku/ch-1.raku +++ b/challenge-252/mark-anderson/raku/ch-1.raku @@ -8,6 +8,5 @@ is special-numbers([2,7,1,19,18,3]), 63; sub special-numbers(@ints) { @ints.unshift: Any; - [+] @ints[divisors @ints.end] >>**>> 2 } diff --git a/challenge-252/mark-anderson/raku/ch-2.raku b/challenge-252/mark-anderson/raku/ch-2.raku index cfc70ac832..6083c3456f 100644 --- a/challenge-252/mark-anderson/raku/ch-2.raku +++ b/challenge-252/mark-anderson/raku/ch-2.raku @@ -5,20 +5,13 @@ is-deeply uniq-sum-zero(5), (-2,-1,1,2,0); is-deeply uniq-sum-zero(3), (-1,1,0); is-deeply uniq-sum-zero(1), (0,); is-deeply uniq-sum-zero(0), (); -is-deeply uniq-sum-zero(-0), (); -is-deeply uniq-sum-zero(-1), (0,); -is-deeply uniq-sum-zero(-8), (-4,-3,-2,-1,1,2,3,4); -is-deeply uniq-sum-zero(-9), (-4,-3,-2,-1,1,2,3,4,0); - -multi uniq-sum-zero($n where 0) { () } - -multi uniq-sum-zero($n where 1|-1) { (0,) } +is-deeply uniq-sum-zero(8), (-4,-3,-2,-1,1,2,3,4); +is-deeply uniq-sum-zero(9), (-4,-3,-2,-1,1,2,3,4,0); +multi uniq-sum-zero($n where 0) { () } +multi uniq-sum-zero($n where 1) { (0,) } multi uniq-sum-zero($n is copy) { - $n .= abs; - my $i = $n div 2; - (-$i,-$i+1...-1,1,2,3...$i,0)[^$n] } -- cgit From 12ee61db18a0cee1763f7b7cdf9492e220e88978 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 15 Jan 2024 17:10:44 +0000 Subject: Challenge 252 Solutions (Raku) --- challenge-252/mark-anderson/raku/ch-2.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/mark-anderson/raku/ch-2.raku b/challenge-252/mark-anderson/raku/ch-2.raku index 6083c3456f..5ad9f9751e 100644 --- a/challenge-252/mark-anderson/raku/ch-2.raku +++ b/challenge-252/mark-anderson/raku/ch-2.raku @@ -1,10 +1,10 @@ #!/usr/bin/env raku use Test; -is-deeply uniq-sum-zero(5), (-2,-1,1,2,0); -is-deeply uniq-sum-zero(3), (-1,1,0); -is-deeply uniq-sum-zero(1), (0,); -is-deeply uniq-sum-zero(0), (); +is-deeply uniq-sum-zero(5), (-2,-1,1,2,0); +is-deeply uniq-sum-zero(3), (-1,1,0); +is-deeply uniq-sum-zero(1), (0,); +is-deeply uniq-sum-zero(0), (); is-deeply uniq-sum-zero(8), (-4,-3,-2,-1,1,2,3,4); is-deeply uniq-sum-zero(9), (-4,-3,-2,-1,1,2,3,4,0); -- cgit From 429584069ee41376d181a37fe6597c1c72b8d5d9 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 15 Jan 2024 17:26:08 +0000 Subject: Challenge 252 Solutions (Raku) --- challenge-252/mark-anderson/raku/ch-2.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'challenge-252') diff --git a/challenge-252/mark-anderson/raku/ch-2.raku b/challenge-252/mark-anderson/raku/ch-2.raku index 5ad9f9751e..6c72b3aae2 100644 --- a/challenge-252/mark-anderson/raku/ch-2.raku +++ b/challenge-252/mark-anderson/raku/ch-2.raku @@ -8,9 +8,9 @@ is-deeply uniq-sum-zero(0), (); is-deeply uniq-sum-zero(8), (-4,-3,-2,-1,1,2,3,4); is-deeply uniq-sum-zero(9), (-4,-3,-2,-1,1,2,3,4,0); -multi uniq-sum-zero($n where 0) { () } -multi uniq-sum-zero($n where 1) { (0,) } -multi uniq-sum-zero($n is copy) +multi uniq-sum-zero($n where 0) { () } +multi uniq-sum-zero($n where 1) { (0,) } +multi uniq-sum-zero($n where * > 1) { my $i = $n div 2; (-$i,-$i+1...-1,1,2,3...$i,0)[^$n] -- cgit From 1db3afdd13bf352c2ebbb1b9ebbc2d74b5954ad1 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 15 Jan 2024 18:31:07 +0100 Subject: challenge-252 --- challenge-252/peter-meszaros/perl/ch-1.pl | 66 +++++++++++++++++++++++++++++++ challenge-252/peter-meszaros/perl/ch-2.pl | 58 +++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100755 challenge-252/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-252/peter-meszaros/perl/ch-2.pl (limited to 'challenge-252') diff --git a/challenge-252/peter-meszaros/perl/ch-1.pl b/challenge-252/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..f13598dec3 --- /dev/null +++ b/challenge-252/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +# You are given an array of integers, @ints. +# +# Write a script to find the sum of the squares of all special elements of the given array. +# +# An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +# Where n is the length of the given array. Also the array is 1-indexed for the task. +# +# Example 1 +# +# Input: @ints = (1, 2, 3, 4) +# Output: 21 +# +# There are exactly 3 special elements in the given array: +# $ints[1] since 1 divides 4, +# $ints[2] since 2 divides 4, and +# $ints[4] since 4 divides 4. +# +# Hence, the sum of the squares of all special elements of given array: +# 1 * 1 + 2 * 2 + 4 * 4 = 21. +# +# Example 2 +# +# Input: @ints = (2, 7, 1, 19, 18, 3) +# Output: 63 +# +# There are exactly 4 special elements in the given array: +# $ints[1] since 1 divides 6, +# $ints[2] since 2 divides 6, +# $ints[3] since 3 divides 6, and +# $ints[6] since 6 divides 6. +# +# Hence, the sum of the squares of all special elements of given array: +# 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 +# +# +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [1, 2, 3, 4], + [2, 7, 1, 19, 18, 3], +]; + +sub special_numbers +{ + my $l = shift; + + my $len = @$l; + my $sum = 0; + for my $i (1..$len) { + unless ($len % $i) { + $sum += $l->[$i-1]**2; + } + } + return $sum; +} + +is(special_numbers($cases->[0]), 21, 'Example 1'); +is(special_numbers($cases->[1]), 63, 'Example 2'); +done_testing(); + +exit 0; diff --git a/challenge-252/peter-meszaros/perl/ch-2.pl b/challenge-252/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d3db6ad3ec --- /dev/null +++ b/challenge-252/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# +# You are given an integer, $n. +# +# Write a script to find an array containing $n unique integers such that they add up to zero. +# Example 1 +# +# Input: $n = 5 +# Output: (-7, -1, 1, 3, 4) +# +# Two other possible solutions could be as below: +# (-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). +# +# Example 2 +# +# Input: $n = 3 +# Output: (-1, 0, 1) +# +# Example 3 +# +# Input: $n = 1 +# Output: (0) +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + 5, + 3, + 1, + 6, +]; + +sub unique_sum_zero +{ + my $n = shift; + + my $arr = []; + for my $i (1..($n/2)) { + push @$arr, $i, -$i; + } + push @$arr, 0 if $n % 2; + + return $arr; +} + +is(unique_sum_zero($cases->[0]), [1, -1, 2, -2, 0], 'Example 1'); +is(unique_sum_zero($cases->[1]), [1, -1, 0], 'Example 2'); +is(unique_sum_zero($cases->[2]), [0], 'Example 3'); +is(unique_sum_zero($cases->[3]), [1, -1, 2, -2, 3, -3], 'Example 4'); +done_testing(); + +exit 0; + + -- cgit From 9282ab98cbd52bace35f36e5cff3e6366a85b6fb Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 15 Jan 2024 18:32:09 +0100 Subject: Add C++ solutions --- challenge-252/e-choroba/cpp/ch-1.cpp | 21 ++++++++++++++++++ challenge-252/e-choroba/cpp/ch-2.cpp | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 challenge-252/e-choroba/cpp/ch-1.cpp create mode 100644 challenge-252/e-choroba/cpp/ch-2.cpp (limited to 'challenge-252') diff --git a/challenge-252/e-choroba/cpp/ch-1.cpp b/challenge-252/e-choroba/cpp/ch-1.cpp new file mode 100644 index 0000000000..ec85e36bf0 --- /dev/null +++ b/challenge-252/e-choroba/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +long special_numbers(std::vector ints) { + size_t s = ints.size(); + long r = 0; + for (size_t i = 0; i <= s; ++i) { + if (s % (i + 1) == 0) { + r += std::pow(ints[i], 2); + } + } + return r; +} + +int main(int argc, char* argv[]) { + assert(special_numbers({1, 2, 3, 4}) == 21); + assert(special_numbers({2, 7, 1, 19, 18, 3}) == 63); + std::cout << "Ok." << std::endl; +} diff --git a/challenge-252/e-choroba/cpp/ch-2.cpp b/challenge-252/e-choroba/cpp/ch-2.cpp new file mode 100644 index 0000000000..e10d27aae6 --- /dev/null +++ b/challenge-252/e-choroba/cpp/ch-2.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +std::vector unique_sum_zero(int n) { + std::vector v; + if (n % 2) { + v = {0}; + } + for (int i = 1; i <= n / 2; ++i) { + v.insert(v.end(), {i, -i}); + } + return v; +} + +int main(int argc, char* argv[]) { + + auto u1 = unique_sum_zero(1); + assert(u1.size() == 1); + assert(u1[0] == 0); + + auto u2 = unique_sum_zero(2); + assert(u2.size() == 2); + assert(u2[0] == 1); + assert(u2[1] == -1); + + auto u3 = unique_sum_zero(3); + assert(u3.size() == 3); + assert(u3[0] == 0); + assert(u3[1] == 1); + assert(u3[2] == -1); + + auto u4 = unique_sum_zero(4); + assert(u4.size() == 4); + assert(u4[0] == 1); + assert(u4[1] == -1); + assert(u4[2] == 2); + assert(u4[3] == -2); + + std::cout << "Ok." << std::endl; +} -- cgit From dd84ff9b392f33fa468e62ada780f8713e9fbccb Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 15 Jan 2024 18:08:48 +0000 Subject: Add Perl solution --- challenge-252/paulo-custodio/Makefile | 2 ++ challenge-252/paulo-custodio/README | 1 + challenge-252/paulo-custodio/perl/ch-1.pl | 56 ++++++++++++++++++++++++++++++ challenge-252/paulo-custodio/perl/ch-2.pl | 36 +++++++++++++++++++ challenge-252/paulo-custodio/t/test-1.yaml | 10 ++++++ challenge-252/paulo-custodio/t/test-2.yaml | 20 +++++++++++ 6 files changed, 125 insertions(+) create mode 100644 challenge-252/paulo-custodio/Makefile create mode 100644 challenge-252/paulo-custodio/README create mode 100644 challenge-252/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-252/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-252/paulo-custodio/t/test-1.yaml create mode 100644 challenge-252/paulo-custodio/t/test-2.yaml (limited to 'challenge-252') diff --git a/challenge-252/paulo-custodio/Makefile b/challenge-252/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-252/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-252/paulo-custodio/README b/challenge-252/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-252/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-252/paulo-custodio/perl/ch-1.pl b/challenge-252/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..b662b32b0d --- /dev/null +++ b/challenge-252/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Challenge 252 +# +# Task 1: Special Numbers +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to find the sum of the squares of all special elements of the given array. +# +# An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +# Where n is the length of the given array. Also the array is 1-indexed for the task. +# +# +# Example 1 +# +# Input: @ints = (1, 2, 3, 4) +# Output: 21 +# +# There are exactly 3 special elements in the given array: +# $ints[1] since 1 divides 4, +# $ints[2] since 2 divides 4, and +# $ints[4] since 4 divides 4. +# +# Hence, the sum of the squares of all special elements of given array: +# 1 * 1 + 2 * 2 + 4 * 4 = 21. +# +# Example 2 +# +# Input: @ints = (2, 7, 1, 19, 18, 3) +# Output: 63 +# +# There are exactly 4 special elements in the given array: +# $ints[1] since 1 divides 6, +# $ints[2] since 2 divides 6, +# $ints[3] since 3 divides 6, and +# $ints[6] since 6 divides 6. +# +# Hence, the sum of the squares of all special elements of given array: +# 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 + +use Modern::Perl; + +sub sum_squares_special_nums { + my(@nums) = @_; + my $sum = 0; + for my $i (1 .. @nums) { + if (scalar(@nums) % $i == 0) { + $sum += $nums[$i-1] * $nums[$i-1]; + } + } + return $sum; +} + +say sum_squares_special_nums(@ARGV); diff --git a/challenge-252/paulo-custodio/perl/ch-2.pl b/challenge-252/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..9b814a359c --- /dev/null +++ b/challenge-252/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# Challenge 252 +# +# Task 2: Unique Sum Zero +# Submitted by: Mohammad S Anwar +# +# You are given an integer, $n. +# +# Write a script to find an array containing $n unique integers such that they add up to zero. +# Example 1 +# +# Input: $n = 5 +# Output: (-7, -1,