diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2023-11-13 19:51:42 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2023-11-13 19:51:42 +0100 |
| commit | 88d84fbfdf481fbcef86fbd015a8512ba67188f0 (patch) | |
| tree | 8f27d570e83b2a67fc0f344370c55ed3f2cb71e3 | |
| parent | e0f33dc8918149dfc29fdc1dfbb040c671851630 (diff) | |
| download | perlweeklychallenge-club-88d84fbfdf481fbcef86fbd015a8512ba67188f0.tar.gz perlweeklychallenge-club-88d84fbfdf481fbcef86fbd015a8512ba67188f0.tar.bz2 perlweeklychallenge-club-88d84fbfdf481fbcef86fbd015a8512ba67188f0.zip | |
Add solution 243
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-243/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-243/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-243/jeanluc2020/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-243/jeanluc2020/perl/ch-2.pl | 57 | ||||
| -rwxr-xr-x | challenge-243/jeanluc2020/python/ch-1.py | 53 | ||||
| -rwxr-xr-x | challenge-243/jeanluc2020/python/ch-2.py | 51 |
6 files changed, 221 insertions, 0 deletions
diff --git a/challenge-243/jeanluc2020/blog-1.txt b/challenge-243/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..25bb149236 --- /dev/null +++ b/challenge-243/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-243-1.html diff --git a/challenge-243/jeanluc2020/blog-2.txt b/challenge-243/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0ca06a13f1 --- /dev/null +++ b/challenge-243/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-243-2.html diff --git a/challenge-243/jeanluc2020/perl/ch-1.pl b/challenge-243/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..f3386dd399 --- /dev/null +++ b/challenge-243/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK1 +# +# Task 1: Reverse Pairs +# ===================== +# +# You are given an array of integers. +# +# Write a script to return the number of reverse pairs in the given +# array. +# +# A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length +# and b) nums[i] > 2 * nums[j]. +# +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk the array twice (once from 0, once from the first index + 1) +# Check the condition in each case, counting the occurrences where +# the condition holds true. + +use strict; +use warnings; + +reverse_pairs(1, 3, 2, 3, 1); +reverse_pairs(2, 4, 3, 5, 1); + +sub reverse_pairs { + my @nums = @_; + my $result = 0; + print "Input: (" . join(", ", @nums) . ")\n"; + foreach my $i (0..$#nums) { + foreach my $j ($i+1..$#nums) { + $result++ if $nums[$i] > 2 * $nums[$j]; + } + } + print "Output: $result\n"; +} diff --git a/challenge-243/jeanluc2020/perl/ch-2.pl b/challenge-243/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..95e31a434f --- /dev/null +++ b/challenge-243/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK2 +# +# Task 2: Floor Sum +# ================= +# +# You are given an array of positive integers (>=1). +# +# Write a script to return the sum of floor(nums[i] / nums[j]) where +# 0 <= i,j < nums.length. The floor() function returns the integer part +# of the division. +# +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Sum up the calculated floor(nums[i]/nums[$j]) for all combinations. + +use strict; +use warnings; + +floor_sum(2, 5, 9); +floor_sum(7, 7, 7, 7, 7, 7, 7); + +sub floor_sum { + my @nums = @_; + my $result = 0; + print "Input: (" . join(", ", @nums) . ")\n"; + foreach my $i (0..$#nums) { + foreach my $j (0..$#nums) { + $result += int($nums[$i] / $nums[$j]); + } + } + print "Output: $result\n"; +} diff --git a/challenge-243/jeanluc2020/python/ch-1.py b/challenge-243/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..c557de5347 --- /dev/null +++ b/challenge-243/jeanluc2020/python/ch-1.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK1 +# +# Task 1: Reverse Pairs +# ===================== +# +# You are given an array of integers. +# +# Write a script to return the number of reverse pairs in the given +# array. +# +# A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length +# and b) nums[i] > 2 * nums[j]. +# +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Walk the array twice (once from 0, once from the first index + 1) +# Check the condition in each case, counting the occurrences where +# the condition holds true. + +def reverse_pairs(nums: list): + print("Input: (", ", ".join(str(x) for x in nums), ")") + result = 0 + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i] > 2 * nums[j]: + result += 1 + print("Output:", str(result)) + +reverse_pairs( [ 1, 3, 2, 3, 1 ] ); +reverse_pairs( [ 2, 4, 3, 5, 1 ] ); + diff --git a/challenge-243/jeanluc2020/python/ch-2.py b/challenge-243/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..7c49e6d055 --- /dev/null +++ b/challenge-243/jeanluc2020/python/ch-2.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/#TASK2 +# +# Task 2: Floor Sum +# ================= +# +# You are given an array of positive integers (>=1). +# +# Write a script to return the sum of floor(nums[i] / nums[j]) where +# 0 <= i,j < nums.length. The floor() function returns the integer part +# of the division. +# +## 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Sum up the calculated floor(nums[i]/nums[$j]) for all combinations. + +def floor_sum(nums: list): + result = 0 + print("Input: (", ", ".join(str(x) for x in nums), ")") + for i in range(len(nums)): + for j in range(len(nums)): + result += int(nums[i] / nums[j]) + print("Output:", str(result)) + +floor_sum( [ 2, 5, 9 ] ); +floor_sum( [ 7, 7, 7, 7, 7, 7, 7 ] ); + |
