diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-02-26 21:02:22 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-02-26 21:02:22 +0100 |
| commit | fa9b1aca1f30a93ca612663fac8b8d3c809d1aa4 (patch) | |
| tree | 7a327cb20bfb6d0471f5798d10f422e162c2096f | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-fa9b1aca1f30a93ca612663fac8b8d3c809d1aa4.tar.gz perlweeklychallenge-club-fa9b1aca1f30a93ca612663fac8b8d3c809d1aa4.tar.bz2 perlweeklychallenge-club-fa9b1aca1f30a93ca612663fac8b8d3c809d1aa4.zip | |
Add solution 258
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-258/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-258/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-258/jeanluc2020/perl/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-258/jeanluc2020/perl/ch-2.pl | 78 | ||||
| -rwxr-xr-x | challenge-258/jeanluc2020/python/ch-1.py | 48 | ||||
| -rwxr-xr-x | challenge-258/jeanluc2020/python/ch-2.py | 66 |
6 files changed, 250 insertions, 0 deletions
diff --git a/challenge-258/jeanluc2020/blog-1.txt b/challenge-258/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..29bcd60356 --- /dev/null +++ b/challenge-258/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-258-1.html diff --git a/challenge-258/jeanluc2020/blog-2.txt b/challenge-258/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..782cf4678b --- /dev/null +++ b/challenge-258/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-258-2.html diff --git a/challenge-258/jeanluc2020/perl/ch-1.pl b/challenge-258/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..605addcc8a --- /dev/null +++ b/challenge-258/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/#TASK1 +# +# Task 1: Count Even Digits Number +# ================================ +# +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +## Example 1 +## +## Input: @ints = (10, 1, 111, 24, 1000) +## Output: 3 +## +## There are 3 integers having even digits i.e. 10, 24 and 1000. +# +## Example 2 +## +## Input: @ints = (111, 1, 11111) +## Output: 0 +# +## Example 3 +## +## Input: @ints = (2, 8, 1024, 256) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# Just calculate the length of each integer an check if it is +# even, then in the end tell how many those were. + +use strict; +use warnings; + +count_even_digits_number(10, 1, 111, 24, 1000); +count_even_digits_number(111, 1, 11111); +count_even_digits_number(2, 8, 1024, 256); + +sub count_even_digits_number { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result = 0; + foreach my $number (@ints) { + if(length($number) % 2 == 0) { + $result++; + } + } + print "Output: $result\n"; +} + + diff --git a/challenge-258/jeanluc2020/perl/ch-2.pl b/challenge-258/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..9c9d8e3d6a --- /dev/null +++ b/challenge-258/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/#TASK2 +# +# Task 2: Sum of Values +# ===================== +# +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has +# exactly $k number of 1-bit set. +# +## Example 1 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 1 +## Output: 17 +## +## Binary representation of index 0 = 0 +## Binary representation of index 1 = 1 +## Binary representation of index 2 = 10 +## Binary representation of index 3 = 11 +## Binary representation of index 4 = 100 +## +## So the indices 1, 2 and 4 have total one 1-bit sets. +## Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 +# +## Example 2 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 2 +## Output: 11 +# +## Example 3 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 0 +## Output: 2 +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of the list: +# - calculate the number of 1-bit sets +# - compare to $k +# - return result of all ints[$i] where $i has $k 1-bits set + +use strict; +use warnings; + +sum_of_values([2, 5, 9, 11, 3], 1); +sum_of_values([2, 5, 9, 11, 3], 2); +sum_of_values([2, 5, 9, 11, 3], 0); + +sub dec2bin { + my $num = shift; + return unpack("B32", pack("N", $num)); +} + +sub sum_of_values { + my ($ints, $k) = @_; + my @tmp = @$ints; + print "Input: (", join(", ", @tmp), "), $k\n"; + my $result = 0; + foreach my $index (0..$#tmp) { + my $bin = dec2bin($index); + my $bits_set = 0; + foreach my $bit (split//, $bin) { + if($bit eq "1") { + $bits_set++; + } + } + if($bits_set == $k) { + $result += $tmp[$index]; + } + } + print "Output: $result\n"; +} + diff --git a/challenge-258/jeanluc2020/python/ch-1.py b/challenge-258/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..efa600dc7e --- /dev/null +++ b/challenge-258/jeanluc2020/python/ch-1.py @@ -0,0 +1,48 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/#TASK1 +# +# Task 1: Count Even Digits Number +# ================================ +# +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +## Example 1 +## +## Input: @ints = (10, 1, 111, 24, 1000) +## Output: 3 +## +## There are 3 integers having even digits i.e. 10, 24 and 1000. +# +## Example 2 +## +## Input: @ints = (111, 1, 11111) +## Output: 0 +# +## Example 3 +## +## Input: @ints = (2, 8, 1024, 256) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# Just calculate the length of each integer an check if it is +# even, then in the end tell how many those were. + +def count_even_digits_number(ints: list): + print("Input: (", ", ".join(str(x) for x in ints), ")", sep="") + result = 0 + for number in ints: + if len(str(number)) % 2 == 0: + result += 1 + print(f"Output: {result}") + +count_even_digits_number([10, 1, 111, 24, 1000]); +count_even_digits_number([111, 1, 11111]); +count_even_digits_number([2, 8, 1024, 256]); + diff --git a/challenge-258/jeanluc2020/python/ch-2.py b/challenge-258/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..79f0382aa2 --- /dev/null +++ b/challenge-258/jeanluc2020/python/ch-2.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/#TASK2 +# +# Task 2: Sum of Values +# ===================== +# +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has +# exactly $k number of 1-bit set. +# +## Example 1 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 1 +## Output: 17 +## +## Binary representation of index 0 = 0 +## Binary representation of index 1 = 1 +## Binary representation of index 2 = 10 +## Binary representation of index 3 = 11 +## Binary representation of index 4 = 100 +## +## So the indices 1, 2 and 4 have total one 1-bit sets. +## Therefore the sum, $ints[1] + $ints[2] + $ints[3] = 17 +# +## Example 2 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 2 +## Output: 11 +# +## Example 3 +## +## Input: @ints = (2, 5, 9, 11, 3), $k = 0 +## Output: 2 +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of the list: +# - calculate the number of 1-bit sets +# - compare to $k +# - return result of all ints[$i] where $i has $k 1-bits set + +def dec2bin(num: int) -> str: + return bin(num)[2:] + +def sum_of_values(ints: list, k: int): + print("Input: (", ", ".join(str(x) for x in ints) , "), ", k, sep="") + result = 0 + for index in range(len(ints)): + bin = dec2bin(index) + bits_set = 0 + for bit in list(bin): + if bit == "1": + bits_set += 1 + if bits_set == k: + result += ints[index] + print(f"Output: {result}") + +sum_of_values([2, 5, 9, 11, 3], 1); +sum_of_values([2, 5, 9, 11, 3], 2); +sum_of_values([2, 5, 9, 11, 3], 0); + |
