diff options
Diffstat (limited to 'challenge-252')
| -rw-r--r-- | challenge-252/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-252/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-252/jeanluc2020/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-252/jeanluc2020/perl/ch-2.pl | 68 | ||||
| -rwxr-xr-x | challenge-252/jeanluc2020/python/ch-1.py | 65 | ||||
| -rwxr-xr-x | challenge-252/jeanluc2020/python/ch-2.py | 67 |
6 files changed, 271 insertions, 0 deletions
diff --git a/challenge-252/jeanluc2020/blog-1.txt b/challenge-252/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..b8884ecfc9 --- /dev/null +++ b/challenge-252/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-252-1.html diff --git a/challenge-252/jeanluc2020/blog-2.txt b/challenge-252/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..a5bed3d6f6 --- /dev/null +++ b/challenge-252/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-252-2.html diff --git a/challenge-252/jeanluc2020/perl/ch-1.pl b/challenge-252/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..21043391f3 --- /dev/null +++ b/challenge-252/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/#TASK1 +# +# 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 +## +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# We initialize a variable $result = 0, then we look at each +# element in the list and add its square to $result if $i divides n. + +use strict; +use warnings; + +special_numbers(1, 2, 3, 4); +special_numbers(2, 7, 1, 19, 18, 3); + +sub special_numbers { + my @ints = @_; + print "Input: (", join(", ", @ints), ")\n"; + my $n = @ints; + my $result = 0; + foreach my $i (1..$n) { + my $elem = $ints[$i-1]; + if($n % $i == 0) { + $result += $elem*$elem; + } + } + print "Output: $result\n"; +} diff --git a/challenge-252/jeanluc2020/perl/ch-2.pl b/challenge-252/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..2ce197cc1f --- /dev/null +++ b/challenge-252/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/#TASK2 +# +# 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) +# +############################################################ +## +## discussion +## +############################################################ +# +# Since we only need to find one array with unique integers, we can +# simply use one with both i and -i in it, with |i| growing, starting +# at 1. In case of an odd number we just add 0 to the array. +use strict; +use warnings; + +unique_sum_zero(5); +unique_sum_zero(3); +unique_sum_zero(1); +unique_sum_zero(2); +unique_sum_zero(4); +unique_sum_zero(0); + +sub unique_sum_zero { + my $n = shift; + print "Input: $n\n"; + my @result = (); + return print "Output: ()\n" if $n == 0; + if($n % 2 == 0) { + # We always use i and -i, ending up at a sum of 0. + my $val = $n / 2; + push @result, (-$val..-1); + push @result, (1..$val); + } else { + # We always use i and -i, ending up at a sum of 0 if we add 0. + my $val = int($n / 2); + push @result, (-$val..-1); + push @result, 0; + push @result, (1..$val); + } + print "Output: (", join(", ", @result), ")\n"; +} + diff --git a/challenge-252/jeanluc2020/python/ch-1.py b/challenge-252/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..baf5f0e66e --- /dev/null +++ b/challenge-252/jeanluc2020/python/ch-1.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/#TASK1 +# +# 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 +## +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# We initialize a variable $result = 0, then we look at each +# element in the list and add its square to $result if $i divides n. + + +def special_numbers(ints: list) -> int: + print("Input: (", ", ".join([str(x) for x in ints]), ")") + n = len(ints) + result = 0 + for i in range(n): + elem = ints[i] + if n % (i+1) == 0: + result += elem*elem + print(f"Output: {result}") + return result + +special_numbers([1, 2, 3, 4]) +special_numbers([2, 7, 1, 19, 18, 3]) + diff --git a/challenge-252/jeanluc2020/python/ch-2.py b/challenge-252/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..45141abfd0 --- /dev/null +++ b/challenge-252/jeanluc2020/python/ch-2.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/#TASK2 +# +# 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) +# +############################################################ +## +## discussion +## +############################################################ +# +# Since we only need to find one array with unique integers, we can +# simply use one with both i and -i in it, with |i| growing, starting +# at 1. In case of an odd number we just add 0 to the array. + +def unique_sum_zero(n: int) -> list: + result = [] + print(f"Input: {n}") + if n == 0: + print("Output: ()") + return + if n % 2 == 0: + val = n / 2 + for i in range(1, int(val+1)): + result.append(i) + result.append(-i) + else: + val = int(n / 2) + result.append(0) + for i in range(1, int(val+1)): + result.append(i) + result.append(-i) + print("Output: (", ", ".join([str(x) for x in result]), ")") + return result + +unique_sum_zero(5) +unique_sum_zero(3) +unique_sum_zero(1) +unique_sum_zero(2) +unique_sum_zero(4) +unique_sum_zero(0) + + |
