diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-03-18 19:59:59 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-03-18 19:59:59 +0100 |
| commit | c234896c4e9fbb7e68245b20d6982768eadcbc0c (patch) | |
| tree | 063240b0b32b9fab1acf93b0a4ec66590d99460b | |
| parent | b91a4623cd4c2b51f779f59b53310a0bd745f03f (diff) | |
| download | perlweeklychallenge-club-c234896c4e9fbb7e68245b20d6982768eadcbc0c.tar.gz perlweeklychallenge-club-c234896c4e9fbb7e68245b20d6982768eadcbc0c.tar.bz2 perlweeklychallenge-club-c234896c4e9fbb7e68245b20d6982768eadcbc0c.zip | |
Add solution 261.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-261/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-261/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-261/jeanluc2020/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-261/jeanluc2020/perl/ch-2.pl | 72 | ||||
| -rwxr-xr-x | challenge-261/jeanluc2020/python/ch-1.py | 67 | ||||
| -rwxr-xr-x | challenge-261/jeanluc2020/python/ch-2.py | 68 |
6 files changed, 281 insertions, 0 deletions
diff --git a/challenge-261/jeanluc2020/blog-1.txt b/challenge-261/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..bc31fcb111 --- /dev/null +++ b/challenge-261/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-261-1.html diff --git a/challenge-261/jeanluc2020/blog-2.txt b/challenge-261/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..15a8fa76af --- /dev/null +++ b/challenge-261/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-261-2.html diff --git a/challenge-261/jeanluc2020/perl/ch-1.pl b/challenge-261/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..cb300cfe0f --- /dev/null +++ b/challenge-261/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK1 +# +# Task 1: Element Digit Sum +# ========================= +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# +## Example 1 +## +## Input: @ints = (1,2,3,45) +## Output: 36 +## +## Element Sum: 1 + 2 + 3 + 45 = 51 +## Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +## Absolute Difference: | 51 - 15 | = 36 +# +## Example 2 +## +## Input: @ints = (1,12,3) +## Output: 9 +## +## Element Sum: 1 + 12 + 3 = 16 +## Digit Sum: 1 + 1 + 2 + 3 = 7 +## Absolute Difference: | 16 - 7 | = 9 +# +## Example 3 +## +## Input: @ints = (1,2,3,4) +## Output: 0 +## +## Element Sum: 1 + 2 + 3 + 4 = 10 +## Digit Sum: 1 + 2 + 3 + 4 = 10 +## Absolute Difference: | 10 - 10 | = 0 +# +## Example 4 +## +## Input: @ints = (236, 416, 336, 350) +## Output: 1296 +# +############################################################ +## +## discussion +## +############################################################ +# +# This is pretty straight forward, calculate the sum for each +# list, then calculate the digit sum by splitting each list +# element into its digits and then calculating the sum from there. + +use strict; +use warnings; +use List::Util qw(sum); + +element_digit_sum(1,2,3,45); +element_digit_sum(1,12,3); +element_digit_sum(1,2,3,4); +element_digit_sum(236, 416, 336, 350); + +sub element_digit_sum { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $elem_sum = sum(@ints); + my $digit_sum = 0; + foreach my $elem (@ints) { + $digit_sum += sum(split//, $elem); + } + print "Output: ", abs($elem_sum - $digit_sum), "\n"; +} diff --git a/challenge-261/jeanluc2020/perl/ch-2.pl b/challenge-261/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..e30ee1ed91 --- /dev/null +++ b/challenge-261/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK2 +# +# Task 2: Multiply by Two +# ======================= +# +# You are given an array of integers, @ints and an integer $start. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# +## Example 1 +## +## Input: @ints = (5,3,6,1,12) and $start = 3 +## Output: 24 +## +## Step 1: 3 is in the array so 3 x 2 = 6 +## Step 2: 6 is in the array so 6 x 2 = 12 +## Step 3: 12 is in the array so 12 x 2 = 24 +## +## 24 is not found in the array so return 24. +# +## Example 2 +## +## Input: @ints = (1,2,4,3) and $start = 1 +## Output: 8 +## +## Step 1: 1 is in the array so 1 x 2 = 2 +## Step 2: 2 is in the array so 2 x 2 = 4 +## Step 3: 4 is in the array so 4 x 2 = 8 +## +## 8 is not found in the array so return 8. +# +## Example 3 +## +## Input: @ints = (5,6,7) and $start = 2 +## Output: 2 +## +## 2 is not found in the array so return 2. +# +############################################################ +## +## discussion +## +############################################################ +# +# Instead of walking the list for every round, we create a +# hash with the list elements as keys, which makes the lookup +# easier. The we just check in a loop whether the current value +# is still there. + +use strict; +use warnings; + +multiply_by_two( [5,3,6,1,12], 3); +multiply_by_two( [1,2,4,3], 1); +multiply_by_two( [5,6,7], 2); + +sub multiply_by_two { + my ($ints, $start) = @_; + print "Input: (", join(", ", @$ints), "), $start\n"; + my $hash; + map { $hash->{$_} = 1; } @$ints; + while($hash->{$start}) { + $start *= 2; + } + print "Output: $start\n"; +} diff --git a/challenge-261/jeanluc2020/python/ch-1.py b/challenge-261/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..d2ccf3ef66 --- /dev/null +++ b/challenge-261/jeanluc2020/python/ch-1.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK1 +# +# Task 1: Element Digit Sum +# ========================= +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# +## Example 1 +## +## Input: @ints = (1,2,3,45) +## Output: 36 +## +## Element Sum: 1 + 2 + 3 + 45 = 51 +## Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +## Absolute Difference: | 51 - 15 | = 36 +# +## Example 2 +## +## Input: @ints = (1,12,3) +## Output: 9 +## +## Element Sum: 1 + 12 + 3 = 16 +## Digit Sum: 1 + 1 + 2 + 3 = 7 +## Absolute Difference: | 16 - 7 | = 9 +# +## Example 3 +## +## Input: @ints = (1,2,3,4) +## Output: 0 +## +## Element Sum: 1 + 2 + 3 + 4 = 10 +## Digit Sum: 1 + 2 + 3 + 4 = 10 +## Absolute Difference: | 10 - 10 | = 0 +# +## Example 4 +## +## Input: @ints = (236, 416, 336, 350) +## Output: 1296 +# +############################################################ +## +## discussion +## +############################################################ +# +# This is pretty straight forward, calculate the sum for each +# list, then calculate the digit sum by splitting each list +# element into its digits and then calculating the sum from there. + +def element_digit_sum(ints: list) -> None: + print("Input: (", ", ".join(str(x) for x in ints), ")", sep="") + elem_sum = sum(ints) + digit_sum = 0 + for elem in ints: + digits = [ int(x) for x in list(str(elem)) ] + digit_sum += sum(digits) + print("Output:", abs(elem_sum - digit_sum)) + +element_digit_sum([1,2,3,45]); +element_digit_sum([1,12,3]); +element_digit_sum([1,2,3,4]); +element_digit_sum([236, 416, 336, 350]); + diff --git a/challenge-261/jeanluc2020/python/ch-2.py b/challenge-261/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..8acebd4043 --- /dev/null +++ b/challenge-261/jeanluc2020/python/ch-2.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/#TASK2 +# +# Task 2: Multiply by Two +# ======================= +# +# You are given an array of integers, @ints and an integer $start. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# +## Example 1 +## +## Input: @ints = (5,3,6,1,12) and $start = 3 +## Output: 24 +## +## Step 1: 3 is in the array so 3 x 2 = 6 +## Step 2: 6 is in the array so 6 x 2 = 12 +## Step 3: 12 is in the array so 12 x 2 = 24 +## +## 24 is not found in the array so return 24. +# +## Example 2 +## +## Input: @ints = (1,2,4,3) and $start = 1 +## Output: 8 +## +## Step 1: 1 is in the array so 1 x 2 = 2 +## Step 2: 2 is in the array so 2 x 2 = 4 +## Step 3: 4 is in the array so 4 x 2 = 8 +## +## 8 is not found in the array so return 8. +# +## Example 3 +## +## Input: @ints = (5,6,7) and $start = 2 +## Output: 2 +## +## 2 is not found in the array so return 2. +# +############################################################ +## +## discussion +## +############################################################ +# +# Instead of walking the list for every round, we create a +# hash with the list elements as keys, which makes the lookup +# easier. The we just check in a loop whether the current value +# is still there. + +def multiply_by_two(ints: list, start: int) -> None: + print("Input: (", ", ".join(str(x) for x in ints), "), ", start, sep="") + hash: dict = {} + for x in ints: + hash[x] = 1 + while start in hash: + start *= 2 + print(f"Output: {start}") + +multiply_by_two( [5,3,6,1,12], 3); +multiply_by_two( [1,2,4,3], 1); +multiply_by_two( [5,6,7], 2); + |
