From 15f2a81d8910efebaf8ca536bc1e6f54ca8c430d Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 21 Feb 2024 01:59:20 -0500 Subject: Challenge 257 solutions by Packy Anderson * Raku * Perl * Python 1 Blog post --- challenge-257/packy-anderson/README.md | 2 +- challenge-257/packy-anderson/blog.txt | 1 + challenge-257/packy-anderson/perl/ch-1.pl | 32 +++++++ challenge-257/packy-anderson/perl/ch-2.pl | 140 +++++++++++++++++++++++++++ challenge-257/packy-anderson/python/ch-1.py | 29 ++++++ challenge-257/packy-anderson/python/ch-2.py | 122 ++++++++++++++++++++++++ challenge-257/packy-anderson/raku/ch-1.raku | 32 +++++++ challenge-257/packy-anderson/raku/ch-2.raku | 141 ++++++++++++++++++++++++++++ 8 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 challenge-257/packy-anderson/blog.txt create mode 100755 challenge-257/packy-anderson/perl/ch-1.pl create mode 100755 challenge-257/packy-anderson/perl/ch-2.pl create mode 100755 challenge-257/packy-anderson/python/ch-1.py create mode 100755 challenge-257/packy-anderson/python/ch-2.py create mode 100755 challenge-257/packy-anderson/raku/ch-1.raku create mode 100755 challenge-257/packy-anderson/raku/ch-2.raku diff --git a/challenge-257/packy-anderson/README.md b/challenge-257/packy-anderson/README.md index c576612b58..0a87407d66 100644 --- a/challenge-257/packy-anderson/README.md +++ b/challenge-257/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Merge the Maximum String Pairs](https://packy.dardan.com/b/Hg) +[Reduced is Smaller!](https://packy.dardan.com/b/Ht) diff --git a/challenge-257/packy-anderson/blog.txt b/challenge-257/packy-anderson/blog.txt new file mode 100644 index 0000000000..fef7769217 --- /dev/null +++ b/challenge-257/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Ht \ No newline at end of file diff --git a/challenge-257/packy-anderson/perl/ch-1.pl b/challenge-257/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..43a3f8a66c --- /dev/null +++ b/challenge-257/packy-anderson/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.38; + +sub smallerThan(@ints) { + my @counts; + foreach my $i (0 .. $#ints) { + @counts[$i] = 0; + foreach my $j (0 .. $#ints) { + next if $i == $j; + $counts[$i]++ if $ints[$j] < $ints[$i]; + } + } + return @counts; +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my @counts = smallerThan(@ints); + say 'Output: (' . join(', ', @counts) . ')'; +} + +say "Example 1:"; +solution(5, 2, 1, 6); + +say "\nExample 2:"; +solution(1, 2, 0, 3); + +say "\nExample 3:"; +solution(0, 1); + +say "\nExample 4:"; +solution(9, 4, 9, 2); \ No newline at end of file diff --git a/challenge-257/packy-anderson/perl/ch-2.pl b/challenge-257/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..eec5339e89 --- /dev/null +++ b/challenge-257/packy-anderson/perl/ch-2.pl @@ -0,0 +1,140 @@ +#!/usr/bin/env perl +use v5.38; + +sub rowIsEntirelyZeros(@row) { + foreach my $n (@row) { + next if $n == 0; + return 0; + } + return 1; +} + +sub rowHasLeadingOne(@row) { + foreach my $n (@row) { + next if $n == 0; + return $n == 1; + } +} + +sub leadingOnePosition(@row) { + foreach my $i (0 .. $#row) { + next if $row[$i] == 0; + return $i; + } +} + +sub columnHasZerosBesidesLeadingOne($matrix, $col) { + my $count = 0; + foreach my $row (@$matrix) { + next if $row->[$col] == 0; # skip zeroes + return 0 if $row->[$col] != 1; # fail if not one + $count++; # count ones + } + return $count == 1; +} + +sub isReducedRowEchelon(@matrix) { + my $foundAllZeroRow = 0; + my $lastLeadingOnePos = -1; # avoid comparison with undef + foreach my $row (@matrix) { + if (! rowIsEntirelyZeros(@$row)) { + # 1. If a row does not consist entirely of zeros, then + # the first nonzero number in the row is a 1. We call + # this the leading 1. + return 0 unless rowHasLeadingOne(@$row); + + # 2. If there are any rows that consist entirely of zeros, + # then they are grouped together at the bottom of the + # matrix. + return 0 if $foundAllZeroRow; + + # 3. In any two successive rows that do not consist + # entirely of zeros, the leading 1 in the lower row + # occurs farther to the right than the leading 1 in + # the higher row. + my $thisLeadingOnePos = leadingOnePosition(@$row); + return 0 if $lastLeadingOnePos > $thisLeadingOnePos; + $lastLeadingOnePos = $thisLeadingOnePos; + + # 4. Each column that contains a leading 1 has zeros + # everywhere else in that column. + return 0 unless columnHasZerosBesidesLeadingOne( + \@matrix, $thisLeadingOnePos + ); + } + else { + $foundAllZeroRow = 1; + } + } + return 1; +} + +sub formatMatrix($matrix, $indent=12) { + my @output; + foreach my $row (@$matrix) { + my $output_row = q{ } x $indent . " [ "; + $output_row .= join(', ', map { sprintf "%2d", $_ } @$row) . ']'; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +sub solution(@matrix) { + say 'Input: $M = ' . formatMatrix(\@matrix); + my $output = isReducedRowEchelon(@matrix); + say 'Output: ' . $output; +} + +say "Example 1:"; +solution( + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] +); + +say "\nExample 2:"; +solution( + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] +); + +say "\nExample 3:"; +solution( + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] +); + +say "\nExample 4:"; +solution( + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] +); + +say "\nExample 5:"; +solution( + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] +); + +say "\nExample 6:"; +solution( + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] +); + +say "\nExample 7:"; +solution( + [1, 0, -1], + [0, 1, 1], + [0, 0, 1] +); diff --git a/challenge-257/packy-anderson/python/ch-1.py b/challenge-257/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..85c2dd0c36 --- /dev/null +++ b/challenge-257/packy-anderson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +def smallerThan(ints): + counts = [] + for i in range(len(ints)): + counts.append(0) + for j in range(len(ints)): + if i != j and ints[j] < ints[i]: counts[i] += 1 + return counts + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + counts = smallerThan(ints) + print(f'Output: ({comma_join(counts)})') + +print('Example 1:') +solution([5, 2, 1, 6]) + +print('\nExample 2:') +solution([1, 2, 0, 3]) + +print('\nExample 3:') +solution([0, 1]) + +print('\nExample 4:') +solution([9, 4, 9, 2]) \ No newline at end of file diff --git a/challenge-257/packy-anderson/python/ch-2.py b/challenge-257/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..ecb171cf4c --- /dev/null +++ b/challenge-257/packy-anderson/python/ch-2.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python + +def rowIsEntirelyZeros(row): + for n in row: + if not n == 0: return 0 + return 1 + +def rowHasLeadingOne(row): + for n in row: + if not n == 0: return n == 1 + +def leadingOnePosition(row): + for i in range(len(row)): + if not row[i] == 0: return i + +def columnHasZerosBesidesLeadingOne(matrix, col): + count = 0 + for row in matrix: + if not row[col] == 0: # skip zeroes + if row[col] != 1: return 0 # fail if not one + count += 1 # count ones + return count == 1 + +def isReducedRowEchelon(matrix): + foundAllZeroRow = 0 + lastLeadingOnePos = -1 # avoid comparison with undef + for row in matrix: + if not rowIsEntirelyZeros(row): + # 1. If a row does not consist entirely of zeros, + # then the first nonzero number in the row is + # a 1. We call this the leading 1. + if not rowHasLeadingOne(row): return 0 + + # 2. If there are any rows that consist entirely + # of zeros, then they are grouped together at + # the bottom of the matrix. + if foundAllZeroRow: return 0 + + # 3. In any two successive rows that do not consist + # entirely of zeros, the leading 1 in the lower row + # occurs farther to the right than the leading 1 in + # the higher row. + thisLeadingOnePos = leadingOnePosition(row) + if lastLeadingOnePos > thisLeadingOnePos: return 0 + lastLeadingOnePos = thisLeadingOnePos + + # 4. Each column that contains a leading 1 has zeros + # everywhere else in that column. + if not columnHasZerosBesidesLeadingOne( + matrix, thisLeadingOnePos + ): return 0 + else: + foundAllZeroRow = 1 + return 1 + +def formatMatrix(matrix, indent=12): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: str(i), row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(matrix): + print(f'Input: $M = {formatMatrix(matrix)}') + print(f'Output: {isReducedRowEchelon(matrix)}') + +print('Example 1:') +solution([ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] +]) + +print('\nExample 2:') +solution([ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] +]) + +print('\nExample 3:') +solution([ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] +]) + +print('\nExample 4:') +solution([ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] +]) + +print('\nExample 5:') +solution([ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] +]) + +print('\nExample 6:') +solution([ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] +]) + +print('\nExample 7:') +solution([ + [1, 0, -1], + [0, 1, 1], + [0, 0, 1] +]) \ No newline at end of file diff --git a/challenge-257/packy-anderson/raku/ch-1.raku b/challenge-257/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..399e111cf5 --- /dev/null +++ b/challenge-257/packy-anderson/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use v6; + +sub smallerThan(@ints) { + my @counts; + for 0 .. @ints.end -> $i { + @counts[$i] = 0; + for 0 .. @ints.end -> $j { + next if $i == $j; + @counts[$i]++ if @ints[$j] < @ints[$i]; + } + } + return @counts; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @counts = smallerThan(@ints); + say 'Output: (' ~ @counts.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([5, 2, 1, 6]); + +say "\nExample 2:"; +solution([1, 2, 0, 3]); + +say "\nExample 3:"; +solution([0, 1]); + +say "\nExample 4:"; +solution([9, 4, 9, 2]); \ No newline at end of file diff --git a/challenge-257/packy-anderson/raku/ch-2.raku b/challenge-257/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..b174afb43b --- /dev/null +++ b/challenge-257/packy-anderson/raku/ch-2.raku @@ -0,0 +1,141 @@ +#!/usr/bin/env raku +use v6; + +sub rowIsEntirelyZeros(@row) { + for @row -> $n { + next if $n == 0; + return 0; + } + return 1; +} + +sub rowHasLeadingOne(@row) { + for @row -> $n { + next if $n == 0; + return $n == 1; + } +} + +sub leadingOnePosition(@row) { + for 0 .. @row.end -> $i { + next if @row[$i] == 0; + return $i; + } +} + +sub columnHasZerosBesidesLeadingOne(@matrix, $col) { + my $count = 0; + for @matrix -> @row { + next if @row[$col] == 0; # skip zeroes + return 0 if @row[$col] != 1; # fail if not one + $count++; # count ones + } + return $count == 1; +} + +sub isReducedRowEchelon(@matrix) { + my $foundAllZeroRow = 0; + my $lastLeadingOnePos = -1; # avoid comparison with undef + for @matrix -> @row { + if (! rowIsEntirelyZeros(@row)) { + # 1. If a row does not consist entirely of zeros, then + # the first nonzero number in the row is a 1. We call + # this the leading 1. + return 0 unless rowHasLeadingOne(@row); + + # 2. If there are any rows that consist entirely of zeros, + # then they are grouped together at the bottom of the + # matrix. + return 0 if $foundAllZeroRow; + + # 3. In any two successive rows that do not consist + # entirely of zeros, the leading 1 in the lower row + # occurs farther to the right than the leading 1 in + # the higher row. + my $thisLeadingOnePos = leadingOnePosition(@row); + return 0 if $lastLeadingOnePos > $thisLeadingOnePos; + $lastLeadingOnePos = $thisLeadingOnePos; + + # 4. Each column that contains a leading 1 has zeros + # everywhere else in that column. + return 0 unless columnHasZerosBesidesLeadingOne( + @matrix, $thisLeadingOnePos + ); + } + else { + $foundAllZeroRow = 1; + } + } + return 1; +} + +sub formatMatrix(@matrix, $indent=12) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " [ "; + $output_row ~= @row.map({ sprintf "%2d", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +sub solution(@matrix) { + say 'Input: $M = ' ~ formatMatrix(@matrix); + my $output = isReducedRowEchelon(@matrix); + say 'Output: ' ~ $output; +} + +say "Example 1:"; +solution([ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] +]); + +say "\nExample 2:"; +solution([ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] +]); + +say "\nExample 3:"; +solution([ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] +]); + +say "\nExample 4:"; +solution([ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] +]); + +say "\nExample 5:"; +solution([ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] +]); + +say "\nExample 6:"; +solution([ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] +]); + +say "\nExample 7:"; +solution([ + [1, 0, -1], + [0, 1, 1], + [0, 0, 1] +]); -- cgit From 5a1661fab22851502a6aba4416029438e456e500 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 26 Feb 2024 08:11:31 +0000 Subject: w258 - Task 1 & 2 --- challenge-258/perlboy1967/perl/ch1.pl | 34 ++++++++++++++++++++++++++++++ challenge-258/perlboy1967/perl/ch2.pl | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 challenge-258/perlboy1967/perl/ch1.pl create mode 100755 challenge-258/perlboy1967/perl/ch2.pl diff --git a/challenge-258/perlboy1967/perl/ch1.pl b/challenge-258/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..7b12ee6549 --- /dev/null +++ b/challenge-258/perlboy1967/perl/ch1.pl @@ -0,0 +1,34 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 258 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-258 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Count Even Digits Number +Submitted by: Mohammad Sajid Anwar + +You are given a array of positive integers, @ints. + +Write a script to find out how many integers have even number of digits. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +sub evenDigits (@ints) { + scalar grep { /^\d+$/ and (length $_) % 2 == 0 } @ints; +} + +is(evenDigits(10,1,111,24,1000),3); +is(evenDigits(111,1,11111),0); +is(evenDigits(2,8,1024,256),1); +is(evenDigits(20,'Perl',1234),2); + +done_testing; diff --git a/challenge-258/perlboy1967/perl/ch2.pl b/challenge-258/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..0c7501df71 --- /dev/null +++ b/challenge-258/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 258 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-258 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Sum of Values +Submitted by: Mohammad Sajid Anwar + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(sum0); + +sub sumOfValues ($k,@ints) { + sum0 map { $ints[$_] } grep { + my $b = sprintf('%b',$_); + length($b =~ s#0##gr) == $k; + } (0 .. $#ints); +} + +is(sumOfValues(1,2,5,9,11,3),17); +is(sumOfValues(2,2,5,9,11,3),11); +is(sumOfValues(0,2,5,9,11,3),2); + +done_testing; -- cgit From b21aeade81bb0eef51d9498abd150e3befb462a0 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 26 Feb 2024 10:04:08 +0100 Subject: Solve 258: Count Even Digits Number & Sum of Values by E. Choroba --- challenge-258/e-choroba/perl/ch-1.pl | 14 ++++++++++++++ challenge-258/e-choroba/perl/ch-2.pl | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100755 challenge-258/e-choroba/perl/ch-1.pl create mode 100755 challenge-258/e-choroba/perl/ch-2.pl diff --git a/challenge-258/e-choroba/perl/ch-1.pl b/challenge-258/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f31fb68f7f --- /dev/null +++ b/challenge-258/e-choroba/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub count_even_digits_number(@ints) { + scalar grep 0 == length() % 2, @ints +} + +use Test::More tests => 3; + +is count_even_digits_number(10, 1, 111, 24, 1000), 3, 'Example 1'; +is count_even_digits_number(111, 1, 11111), 0, 'Example 2'; +is count_even_digits_number(2, 8, 1024, 256), 1, 'Example 3'; diff --git a/challenge-258/e-choroba/perl/ch-2.pl b/challenge-258/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..5740dcd6b2 --- /dev/null +++ b/challenge-258/e-choroba/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub sum_of_values($ints, $k) { + # See perldoc -f unpack for this one. + sum(@$ints[ grep $k == unpack('%32b*', pack N => $_), 0 .. $#$ints ]) +} + +use Test::More tests => 3; + +is sum_of_values([2, 5, 9, 11, 3], 1), 17, 'Example 1'; +is sum_of_values([2, 5, 9, 11, 3], 2), 11, 'Example 2'; +is sum_of_values([2, 5, 9, 11, 3], 0), 2, 'Example 3'; -- cgit From 093553020c7cf39461a2be0e1c8851c983ba863b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 26 Feb 2024 09:28:02 +0100 Subject: PWC 258 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-258/luca-ferrari/blog-1.txt | 1 + challenge-258/luca-ferrari/blog-10.txt | 1 + challenge-258/luca-ferrari/blog-2.txt | 1 + challenge-258/luca-ferrari/blog-3.txt | 1 + challenge-258/luca-ferrari/blog-4.txt | 1 + challenge-258/luca-ferrari/blog-5.txt | 1 + challenge-258/luca-ferrari/blog-6.txt | 1 + challenge-258/luca-ferrari/blog-7.txt | 1 + challenge-258/luca-ferrari/blog-8.txt | 1 + challenge-258/luca-ferrari/blog-9.txt | 1 + challenge-258/luca-ferrari/pljava/pom.xml | 6 +- .../luca-ferrari/pljava/src/main/java/Task1.java | 62 +++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 81 ++++++++++++++++++++++ challenge-258/luca-ferrari/plperl/ch-1.plperl | 22 ++++++ challenge-258/luca-ferrari/plperl/ch-2.plperl | 28 ++++++++ challenge-258/luca-ferrari/plpgsql/ch-1.sql | 22 ++++++ challenge-258/luca-ferrari/plpgsql/ch-2.sql | 32 +++++++++ challenge-258/luca-ferrari/python/ch-1.py | 25 +++++++ challenge-258/luca-ferrari/python/ch-2.py | 36 ++++++++++ challenge-258/luca-ferrari/raku/ch-1.raku | 13 ++++ challenge-258/luca-ferrari/raku/ch-2.raku | 14 ++++ 21 files changed, 348 insertions(+), 3 deletions(-) create mode 100644 challenge-258/luca-ferrari/blog-1.txt create mode 100644 challenge-258/luca-ferrari/blog-10.txt create mode 100644 challenge-258/luca-ferrari/blog-2.txt create mode 100644 challenge-258/luca-ferrari/blog-3.txt create mode 100644 challenge-258/luca-ferrari/blog-4.txt create mode 100644 challenge-258/luca-ferrari/blog-5.txt create mode 100644 challenge-258/luca-ferrari/blog-6.txt create mode 100644 challenge-258/luca-ferrari/blog-7.txt create mode 100644 challenge-258/luca-ferrari/blog-8.txt create mode 100644 challenge-258/luca-ferrari/blog-9.txt create mode 100644 challenge-258/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-258/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-258/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-258/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-258/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-258/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-258/luca-ferrari/python/ch-1.py create mode 100644 challenge-258/luca-ferrari/python/ch-2.py create mode 100644 challenge-258/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-258/luca-ferrari/raku/ch-2.raku diff --git a/challenge-258/luca-ferrari/blog-1.txt b/challenge-258/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..6fba75eff2 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task1 diff --git a/challenge-258/luca-ferrari/blog-10.txt b/challenge-258/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..f0ea0bf318 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/[= date -%]/PerlWeeklyChallenge258.html#task2pljava diff --git a/challenge-258/luca-ferrari/blog-2.txt b/challenge-258/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..12172aca8d --- /dev/null +++ b/challenge-258/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task2 diff --git a/challenge-258/luca-ferrari/blog-3.txt b/challenge-258/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..f3ca0419a3 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task1plperl diff --git a/challenge-258/luca-ferrari/blog-4.txt b/challenge-258/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..a145ef8d3e --- /dev/null +++ b/challenge-258/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task2plperl diff --git a/challenge-258/luca-ferrari/blog-5.txt b/challenge-258/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..ff27319926 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task1plpgsql diff --git a/challenge-258/luca-ferrari/blog-6.txt b/challenge-258/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..7c37d2ecbc --- /dev/null +++ b/challenge-258/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task2plpgsql diff --git a/challenge-258/luca-ferrari/blog-7.txt b/challenge-258/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..3acf21468e --- /dev/null +++ b/challenge-258/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task1python diff --git a/challenge-258/luca-ferrari/blog-8.txt b/challenge-258/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..91ad2169e2 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task2python diff --git a/challenge-258/luca-ferrari/blog-9.txt b/challenge-258/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..28ce6da696 --- /dev/null +++ b/challenge-258/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2024/02/26/PerlWeeklyChallenge258.html#task1pljava diff --git a/challenge-258/luca-ferrari/pljava/pom.xml b/challenge-258/luca-ferrari/pljava/pom.xml index 6fe5f45834..1a0d8dbf4d 100644 --- a/challenge-258/luca-ferrari/pljava/pom.xml +++ b/challenge-258/luca-ferrari/pljava/pom.xml @@ -5,11 +5,11 @@ 4.0.0 PWC - PWC257 + PWC258 1 - Perl Weekly Challenge 257 - Implementation of the tasks in PL/Java for PWC 257 + Perl Weekly Challenge 258 + Implementation of the tasks in PL/Java for PWC 258 US-ASCII diff --git a/challenge-258/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-258/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..52e1cf3491 --- /dev/null +++ b/challenge-258/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,62 @@ +package PWC258; + +/** + * PL/Java implementation for PWC 258 + * Task 1 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC258.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC258-1.jar', 'PWC258', true ); + + select pwc258.task1_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC258-1.jar', 'PWC258', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc258", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task1_pljava( int nums[] ) throws SQLException { + logger.log( Level.INFO, "Entering task1_pljava" ); + + List numbers = new LinkedList(); + for ( int current : nums ) + numbers.add( current ); + + int count = numbers.stream().mapToInt( current -> { + if ( String.format( "%d", current ).length() % 2 == 0 ) + return 1; + else + return 0; + } ).sum(); + + return count; + } +} diff --git a/challenge-258/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-258/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..a724c021c6 --- /dev/null +++ b/challenge-258/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,81 @@ +package PWC258; + +/** + * PL/Java implementation for PWC 258 + * Task 2 + * See + * + * + * To compile on the local machine: + + $ export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ # if not already set + $ mvn clean build + $ scp target/PWC258.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC258-1.jar', 'PWC258', true ); + + select pwc258.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC258-1.jar', 'PWC258', true ); + +*/ + +import org.postgresql.pljava.*; +import org.postgresql.pljava.annotation.Function; +import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE; +import static org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL; + +import java.util.*; +import java.util.stream.*; +import java.sql.SQLException; +import java.util.logging.*; +import java.sql.ResultSet; +import java.sql.Date; + +public class Task2 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc258", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final int task2_pljava( int k, int[] nums ) throws SQLException { + logger.log( Level.INFO, "Entering task2_pljava" ); + + // int sum = 0; + // int count = 0; + // for ( int i = 0; i < nums.length; i++ ) { + // count = 0; + // for ( String d : Integer.toBinaryString( i ).split( "" ) ) + // if ( d.equals( "1" ) ) + // count++; + + + // if ( count == k ) + // sum += nums[ i ]; + // } + + // return sum + + return IntStream.range( 0, nums.length ) + .map( current -> { + int ones = 0; + for ( String d : Integer.toBinaryString( current ).split( "" ) ) + if ( d.equals( "1" ) ) + ones ++; + + + if ( ones == k ) + return nums[ current ]; + else + return 0; + + } ).sum(); +; + } +} diff --git a/challenge-258/luca-ferrari/plperl/ch-1.plperl b/challenge-258/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..533e6570f7 --- /dev/null +++ b/challenge-258/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 258 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc258; + +CREATE OR REPLACE FUNCTION +pwc258.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + + my ( $numbers ) = @_; + + my @counting = map { split( //, $_ ) % 2 == 0 ? 1 : 0 } ( $numbers->@* ); + my $sum = 0; + $sum += $_ for ( @counting ); + return $sum; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-258/luca-ferrari/plperl/ch-2.plperl b/challenge-258/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..8d6755c472 --- /dev/null +++ b/challenge-258/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 258 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc258; + +CREATE OR REPLACE FUNCTION +pwc258.task2_plperl( int, int[] ) +RETURNS int +AS $CODE$ + + my ( $k, $numbers ) = @_; + + my $count_binary_ones = sub { + my $num = shift; + my @digits = split //, sprintf( '%b', $num ); + return scalar( grep { $_ == 1 } @digits ); + }; + + my @indexes = grep { $count_binary_ones->( $_ ) == $k } ( 0 .. $numbers->@* - 1 ); + my $sum = 0; + $sum += $numbers->@[ $_ ] for ( @indexes ); + return $sum; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-258/luca-ferrari/plpgsql/ch-1.sql b/challenge-258/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..7e02f746b8 --- /dev/null +++ b/challenge-258/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,22 @@ +-- +-- Perl Weekly Challenge 258 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc258; + +CREATE OR REPLACE FUNCTION +pwc258.task1_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ + WITH q_nums AS ( + SELECT v, array_length( regexp_split_to_array( v::text, '' ), 1 ) as c + FROM unnest( nums ) v + ) + SELECT count( * ) + FROM q_nums + WHERE c % 2 = 0; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-258/luca-ferrari/plpgsql/ch-2.sql b/challenge-258/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..3241facae4 --- /dev/null +++ b/challenge-258/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 258 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc258; + +CREATE OR REPLACE FUNCTION +pwc258.task2_plpgsql( k int, nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + v_sum int := 0; + count_of_ones int := 0; +BEGIN + FOR i IN 1 .. array_length( nums, 1 ) LOOP + SELECT sum( v::int ) + INTO count_of_ones + FROM regexp_split_to_table( i::bit( 8 )::text, '' ) v; + + IF count_of_ones <> k THEN + continue; + END IF; + + v_sum := v_sum + nums[ i ]; + END LOOP; + + RETURN v_sum; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-258/luca-ferrari/python/ch-1.py b/challenge-258/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..4ba789cf54 --- /dev/null +++ b/challenge-258/luca-ferrari/python/ch-1.py @@ -0,0 +1,25 @@ +#!python + +# +# Perl Weekly Challenge 258 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + sum = 0 + for n in args: + if len( n ) % 2 == 0: + sum += 1 + + return sum + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-258/luca-ferrari/python/ch-2.py b/challenge-258/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..a5f6a97177 --- /dev/null +++ b/challenge-258/luca-ferrari/python/ch-2.py @@ -0,0 +1,36 @@ +#!python + +# +# Perl Weekly Challenge 258 +# Task 2 +# +# See +# + +import sys + + +# task implementation +# the return value will be printed +def task_2( args ): + k = int( args[ 0 ] ) + nums = list( map( int, args[ 1: ] ) ) + + def is_index_ok( v, k ): + b = '{0:08b}'.format( v ) + count = sum( map( int, list( '{0:08b}'.format( v ) ) ) ) + return count == k + + indexes = list( filter( lambda i: is_index_ok( i, k ), + range(0, len( nums ) ) ) ) + + summy = 0 + for i in indexes: + summy += nums[ i ] + + return summy + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-258/luca-ferrari/raku/ch-1.raku b/challenge-258/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..2864397a03 --- /dev/null +++ b/challenge-258/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!raku + +# +# Perl Weekly Challenge 258 +# Task 1 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + @nums.map( { $_.comb.elems %% 2 ?? 1 !! 0 } ).sum.say; + +} diff --git a/challenge-258/luca-ferrari/raku/ch-2.raku b/challenge-258/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..174f177137 --- /dev/null +++ b/challenge-258/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,14 @@ +#!raku + +# +# Perl Weekly Challenge 258 +# Task 2 +# +# See +# + +sub MAIN( Int $k where { $k > 0 } , *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems } ) { + my @indexes = ( 0 ..^ @nums.elems ).grep( { $_.base( 2 ).comb.grep( * == 1 ).elems == $k } ); + @nums[ @indexes ].sum.say; + +} -- cgit From 9d86cacc92e5e939a5a45d8e38a70b68169a7177 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 26 Feb 2024 14:04:35 +0000 Subject: Week 258 --- challenge-258/peter-campbell-smith/blog.txt | 1 + challenge-258/peter-campbell-smith/perl/ch-1.pl | 24 ++++++++++++++ challenge-258/peter-campbell-smith/perl/ch-2.pl | 43 +++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 challenge-258/peter-campbell-smith/blog.txt create mode 100755 challenge-258/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-258/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-258/peter-campbell-smith/blog.txt b/challenge-258/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4965f3d1a5 --- /dev/null +++ b/challenge-258/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/258 diff --git a/challenge-258/peter-campbell-smith/perl/ch-1.pl b/challenge-258/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..185c4163ce --- /dev/null +++ b/challenge-258/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-02-26 +use utf8; # Week 258 - task 1 - Count even digits number +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +count_even_digits_number (10, 1, 111, 24, 1000); +count_even_digits_number (111, 1, 11111); +count_even_digits_number (2, 8, 1024, 256); +count_even_digits_number (1234567890123456789901234567890, + 12345678901234567890, 123456789); + +sub count_even_digits_number { + + # count @ints with even length + my $count = 0; + $count += 1 - (length($_) % 2) for @_; + + say qq[\nInput: (] . join(', ', @_) . ')'; + say qq[Output: $count]; +} diff --git a/challenge-258/peter-campbell-smith/perl/ch-2.pl b/challenge-258/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..9e10eb19f8 --- /dev/null +++ b/challenge-258/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-02-26 +use utf8; # Week 258 - task 2 - Sum of values +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +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); +sum_of_values([2, 5, 9, 11, 3], 3); +sum_of_values([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 4); + +sub sum_of_values { + + my (@ints, $k, $j, $sum, $binary, $bit_count, $explain); + + # initialise + @ints = @{$_[0]}; + $k = $_[1]; + $sum = 0; + $explain = ''; + + # loop over @ints + for $j (0 .. @ints - 1) { + + # count bits in index + $binary = sprintf('%b', $j); + $bit_count = eval(join('+', split('', $binary))); + + # accumulate $sum where $j has $k bits set + next unless $bit_count == $k; + $sum += $ints[$j]; + $explain .= qq[$j == 0b$binary, ]; + } + + # show results + say qq[\nInput: \@ints = (] . join(', ', @ints) . qq[), \$k = $k]; + say qq[Output: $sum] . + ($explain ? ' ∵ ' . substr($explain, 0, -2) : ''); +} -- cgit From 659cdb7fad452141e1b2ec4a2bb14a9765501a6d Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 26 Feb 2024 18:50:34 +1100 Subject: pwc258 solution in python --- challenge-258/pokgopun/python/ch-1.py | 51 ++++++++++++++++++++++++++++ challenge-258/pokgopun/python/ch-2.py | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 challenge-258/pokgopun/python/ch-1.py create mode 100644 challenge-258/pokgopun/python/ch-2.py diff --git a/challenge-258/pokgopun/python/ch-1.py b/challenge-258/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..8232fe40f8 --- /dev/null +++ b/challenge-258/pokgopun/python/ch-1.py @@ -0,0 +1,51 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +""" + +Task 1: Count Even Digits Number + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + 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 + +Task 2: Sum of Values +""" +### solution by pokgopun@gmail.com + +def CEDN(ints: tuple): + return sum( + len(str(e)) % 2 == 0 for e in ints + ) + +import unittest + +class TestCEDN(unittest.TestCase): + def test(self): + for inpt, otpt in { + (10, 1, 111, 24, 1000): 3, + (111, 1, 11111): 0, + (2, 8, 1024, 256): 1, + }.items(): + self.assertEqual(CEDN(inpt),otpt) + +unittest.main() diff --git a/challenge-258/pokgopun/python/ch-2.py b/challenge-258/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..049d7348b1 --- /dev/null +++ b/challenge-258/pokgopun/python/ch-2.py @@ -0,0 +1,64 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +""" + +Task 2: Sum of Values + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + 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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 1st March 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def SOV(ints: tuple, k: int): + return sum( + ints[i] for i in range(len(ints)) + if sum( + int(e) for e in bin(i).lstrip('0b') + ) == k + ) +import unittest + +class TestSOV(unittest.TestCase): + def test(self): + for (ints, k), otpt in { + ((2, 5, 9, 11, 3), 1): 17, + ((2, 5, 9, 11, 3), 2): 11, + ((2, 5, 9, 11, 3), 0): 2, + }.items(): + self.assertEqual(SOV(ints,k),otpt) + +unittest.main() -- cgit From 771dae0212804b162ea3d34db423e921b91efe0d Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 27 Feb 2024 01:37:02 +1100 Subject: pwc258 solution in go --- challenge-258/pokgopun/go/ch-1.go | 78 +++++++++++++++++++++++++++++++++ challenge-258/pokgopun/go/ch-2.go | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 challenge-258/pokgopun/go/ch-1.go create mode 100644 challenge-258/pokgopun/go/ch-2.go diff --git a/challenge-258/pokgopun/go/ch-1.go b/challenge-258/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..83f4c84c0b --- /dev/null +++ b/challenge-258/pokgopun/go/ch-1.go @@ -0,0 +1,78 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +/*# + +Task 1: Count Even Digits Number + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + 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 + +Task 2: Sum of Values +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type num int + +func (nm num) countDigit() int { + c := 0 + for nm > 0 { + nm /= 10 + c++ + } + return c +} + +type nums []num + +func (ns nums) cedn() int { + c := 0 + for _, v := range ns { + if v.countDigit()%2 == 0 { + c++ + } + } + return c +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{10, 1, 111, 24, 1000}, 3}, + {nums{111, 1, 11111}, 0}, + {nums{2, 8, 1024, 256}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.cedn(), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-258/pokgopun/go/ch-2.go b/challenge-258/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..14a352f7f0 --- /dev/null +++ b/challenge-258/pokgopun/go/ch-2.go @@ -0,0 +1,92 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +/*# + +Task 2: Sum of Values + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + 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 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 1st March 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type num int + +func (nm num) countOn() int { + c := 0 + for nm > 0 { + if nm%2 == 1 { + c++ + } + nm /= 2 + } + return c +} + +type nums []int + +func (ns nums) sov(n int) int { + s := 0 + for i, v := range ns { + if num(i).countOn() == n { + s += v + } + } + return s +} + +func main() { + for _, data := range []struct { + ints nums + k int + output int + }{ + {nums{2, 5, 9, 11, 3}, 1, 17}, + {nums{2, 5, 9, 11, 3}, 2, 11}, + {nums{2, 5, 9, 11, 3}, 0, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.ints.sov(data.k), data.output)) // blank if ok, otherwise show the differences + } +} -- cgit From 66a050375c085ab32267a2c15bcb359490f9e646 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 26 Feb 2024 10:10:41 -0500 Subject: Week 258 --- challenge-258/zapwai/c/ch-1.c | 30 ++++++++++++++++++++++++ challenge-258/zapwai/c/ch-2.c | 41 +++++++++++++++++++++++++++++++++ challenge-258/zapwai/javascript/ch-1.js | 17 ++++++++++++++ challenge-258/zapwai/javascript/ch-2.js | 23 ++++++++++++++++++ challenge-258/zapwai/perl/ch-1.pl | 22 ++++++++++++++++++ challenge-258/zapwai/perl/ch-2.pl | 24 +++++++++++++++++++ challenge-258/zapwai/python/ch-1.py | 14 +++++++++++ challenge-258/zapwai/python/ch-2.py | 22 ++++++++++++++++++ challenge-258/zapwai/rust/ch-1.rs | 19 +++++++++++++++ challenge-258/zapwai/rust/ch-2.rs | 30 ++++++++++++++++++++++++ 10 files changed, 242 insertions(+) create mode 100644 challenge-258/zapwai/c/ch-1.c create mode 100644 challenge-258/zapwai/c/ch-2.c create mode 100644 challenge-258/zapwai/javascript/ch-1.js create mode 100644 challenge-258/zapwai/javascript/ch-2.js create mode 100644 challenge-258/zapwai/perl/ch-1.pl create mode 100644 challenge-258/zapwai/perl/ch-2.pl create mode 100644 challenge-258/zapwai/python/ch-1.py create mode 100644 challenge-258/zapwai/python/ch-2.py create mode 100644 challenge-258/zapwai/rust/ch-1.rs create mode 100644 challenge-258/zapwai/rust/ch-2.rs diff --git a/challenge-258/zapwai/c/ch-1.c b/challenge-258/zapwai/c/ch-1.c new file mode 100644 index 0000000000..65d6813c07 --- /dev/null +++ b/challenge-258/zapwai/c/ch-1.c @@ -0,0 +1,30 @@ +#include +#include + +void proc(int intslen, int ints[]) { + printf("Input: { "); + for (int i = 0; i < intslen; i++) { + printf("%d ", ints[i]); + } + printf("}\n"); + int cnt = 0; + for (int i = 0; i < intslen; i++) { + char word[50]; + sprintf(word, "%d", ints[i]); + if (strlen(word) % 2== 0) { + cnt++; + } + } + printf("Output: %d\n", cnt); +} + +int main() { + int ints[] = {10, 1, 111, 24, 1000}; + proc(sizeof(ints)/sizeof(int), ints); + int ints2[] = {111, 1, 11111}; + proc(sizeof(ints2)/sizeof(int), ints2); + int ints3[] = {2, 8, 1024, 256}; + proc(sizeof(ints3)/sizeof(int), ints3); + +} + diff --git a/challenge-258/zapwai/c/ch-2.c b/challenge-258/zapwai/c/ch-2.c new file mode 100644 index 0000000000..697bb706a6 --- /dev/null +++ b/challenge-258/zapwai/c/ch-2.c @@ -0,0 +1,41 @@ +#include + +int binary_tally(int num) { + int tally = 0; + int bitlen = sizeof(num) * 8; + for (int i = bitlen - 1; i >= 0; i--) { + if (num & (1 << i)) + tally++; + } + return tally; +} + +void proc(int k, int intlen, int ints[]) { + printf("Input: { "); + for (int i = 0; i < intlen; i++) { + printf("%d ", ints[i]); + } + printf("}, k = %d\n", k); + + int sum = 0; + for (int i = 0; i < intlen; i++) { + int tally = binary_tally(i); + if (tally == k) { + sum += ints[i]; + } + } + + printf("Output: %d\n", sum); +} + +int main() { + int ints1[] = {2, 5, 9, 11, 3}; int k1 = 1; + int ints2[] = {2, 5, 9, 11, 3}; int k2 = 2; + int ints3[] = {2, 5, 9, 11, 3}; int k3 = 0; + int l1 = sizeof(ints1) / sizeof(int); + int l2 = sizeof(ints2) / sizeof(int); + int l3 = sizeof(ints3) / sizeof(int); + proc(k1, l1, ints1); + proc(k2, l2, ints2); + proc(k3, l3, ints3); +} diff --git a/challenge-258/zapwai/javascript/ch-1.js b/challenge-258/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..b9bbd3ecb3 --- /dev/null +++ b/challenge-258/zapwai/javascript/ch-1.js @@ -0,0 +1,17 @@ +let ints = [10, 1, 111, 24, 1000]; +let ints2 = [111, 1, 11111]; +let ints3 = [2, 8, 1024, 256]; +proc(ints); +proc(ints2); +proc(ints3); + +function proc(ints) { + console.log("Input:", ints); + let cnt = 0; + for (let num of ints) { + if (num.toString().length % 2 == 0) { + cnt++; + } + } + console.log("Output:", cnt); +} diff --git a/challenge-258/zapwai/javascript/ch-2.js b/challenge-258/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..97e37a8340 --- /dev/null +++ b/challenge-258/zapwai/javascript/ch-2.js @@ -0,0 +1,23 @@ +let ints1 = [2, 5, 9, 11, 3]; let k1 = 1; +let ints2 = [2, 5, 9, 11, 3]; let k2 = 2; +let ints3 = [2, 5, 9, 11, 3]; let k3 = 0; +proc(k1, ints1); +proc(k2, ints2); +proc(k3, ints3); +function proc(k, ints) { + console.log("Input:", ints, "k:", k); + let sum = 0; + for (let i in ints) { + let tally = 0; + let x = Number(i).toString(2); + for (let c of x.split("")) { + if (c == '1') { + tally += 1; + } + } + if (tally == k) { + sum += ints[i]; + } + } + console.log("Output:", sum); +} diff --git a/challenge-258/zapwai/perl/ch-1.pl b/challenge-258/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..ad0c288489 --- /dev/null +++ b/challenge-258/zapwai/perl/ch-1.pl @@ -0,0 +1,22 @@ +use v5.36; + +my @ints = (10, 1, 111, 24, 1000); +my @ints2 = (111, 1, 11111); +my @ints3 = (2, 8, 1024, 256); + +proc(@ints); +proc(@ints2); +proc(@ints3); + +sub proc (@ints) { + say "Input: (@ints)"; + say "Output: ", cnt_even_lens(@ints); +} + +sub cnt_even_lens(@ints) { + my $cnt = 0; + foreach my $item (@ints) { + $cnt++ if (length($item) % 2 == 0); + } + return $cnt; +} diff --git a/challenge-258/zapwai/perl/ch-2.pl b/challenge-258/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..95c03ab65e --- /dev/null +++ b/challenge-258/zapwai/perl/ch-2.pl @@ -0,0 +1,24 @@ +use v5.36; +use List::Util qw( sum ); + +my @ints1 = (2, 5, 9, 11, 3); my $k1 = 1; +my @ints2 = (2, 5, 9, 11, 3); my $k2 = 2; +my @ints3 = (2, 5, 9, 11, 3); my $k3 = 0; + +proc($k1, @ints1); +proc($k2, @ints2); +proc($k3, @ints3); + +sub proc($k, @ints) { + say "Input: \@ints = (@ints), \$k = $k"; + my $sum = 0; + my @ind; + for my $i (0 .. $#ints) { + my $s = sprintf "%b", $i; + my @bit = split "", $s; + my $tally = sum( grep { 1 } @bit ); + push @ind, $i if ($tally == $k); + } + $sum += $ints[$_] foreach (@ind); + say "Output: $sum"; +} diff --git a/challenge-258/zapwai/python/ch-1.py b/challenge-258/zapwai/python/ch-1.py new file mode 100644 index 0000000000..e6c8785b3a --- /dev/null +++ b/challenge-258/zapwai/python/ch-1.py @@ -0,0 +1,14 @@ +def proc(ints): + cnt = 0; + for i in ints: + if len(str(i)) % 2 == 0: + cnt += 1 + print("Input:", ints) + print("Output:", cnt) + +ints = [10, 1, 111, 24, 1000] +ints2 = [111, 1, 11111] +ints3 = [2, 8, 1024, 256] +proc(ints) +proc(ints2) +proc(ints3) diff --git a/challenge-258/zapwai/python/ch-2.py b/challenge-258/zapwai/python/ch-2.py new file mode 100644 index 0000000000..d1fb04f3d8 --- /dev/null +++ b/challenge-258/zapwai/python/ch-2.py @@ -0,0 +1,22 @@ +def proc(k, ints): + print("Input: ints:", ints, "k:", k) + sum = 0 + ind = [] + for i in range(len(ints)): + tally = 0; + s = format(f'{i:b}') + for c in list(s): + if c == "1": + tally += 1 + if tally == k: + ind.append(i) + for i in ind: + sum += ints[i] + print("Output:",sum) +ints1 = [2, 5, 9, 11, 3]; k1 = 1 +ints2 = [2, 5, 9, 11, 3]; k2 = 2 +ints3 = [2, 5, 9, 11, 3]; k3 = 0 + +proc(k1, ints1) +proc(k2, ints2) +proc(k3, ints3) diff --git a/challenge-258/zapwai/rust/ch-1.rs b/challenge-258/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..507db0ceb4 --- /dev/null +++ b/challenge-258/zapwai/rust/ch-1.rs @@ -0,0 +1,19 @@ +fn main() { + let ints = vec![10, 1, 111, 24, 1000]; + let ints2 = vec![111, 1, 11111]; + let ints3 = vec![2, 8, 1024, 256]; + proc(ints); + proc(ints2); + proc(ints3); +} + +fn proc(ints : Vec) { + let mut cnt = 0; + for i in &ints { + if i.to_string().len() % 2 == 0 { + cnt += 1; + } + } + println!("Input: {:?}", ints); + println!("Output: {cnt}"); +} diff --git a/challenge-258/zapwai/rust/ch-2.rs b/challenge-258/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..a4519e44a6 --- /dev/null +++ b/challenge-258/zapwai/rust/ch-2.rs @@ -0,0 +1,30 @@ +fn proc(k : i32, ints : Vec) { + println!("Input: {:?}, k = {k}", ints); + let mut sum = 0; + let mut ind = Vec::new(); + for i in 0 .. ints.len() { + let mut tally = 0; + let s = format!("{:b}", i); + for c in s.to_string().chars() { + if c == '1' { + tally += 1; + } + } + if tally == k { + ind.push(i); + } + } + for i in ind { + sum += ints[i]; + } + println!("Output: {sum}"); +} + +fn main() { + let ints1 = vec![2, 5, 9, 11, 3]; let k1 = 1; + let ints2 = vec![2, 5, 9, 11, 3]; let k2 = 2; + let ints3 = vec![2, 5, 9, 11, 3]; let k3 = 0; + proc(k1, ints1); + proc(k2, ints2); + proc(k3, ints3); +} -- cgit From 97a4959fdf2f31cea3e91447f95b719c14d8660e Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 26 Feb 2024 17:53:49 +0100 Subject: challenge-258 --- challenge-258/peter-meszaros/perl/ch-1.pl | 47 +++++++++++++++++++++++ challenge-258/peter-meszaros/perl/ch-2.pl | 62 +++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100755 challenge-258/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-258/peter-meszaros/perl/ch-2.pl diff --git a/challenge-258/peter-meszaros/perl/ch-1.pl b/challenge-258/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..f1399beb7a --- /dev/null +++ b/challenge-258/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# +# 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 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [10, 1, 111, 24, 1000], + [111, 1, 11111], + [2, 8, 1024, 256], +]; + +sub count_even_digits_number +{ + my $ints = shift; + + return scalar grep { not split('') % 2 } @$ints; +} + +is(count_even_digits_number($cases->[0]), 3, 'Example 1'); +is(count_even_digits_number($cases->[1]), 0, 'Example 2'); +is(count_even_digits_number($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-258/peter-meszaros/perl/ch-2.pl b/challenge-258/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..49e7b3c13f --- /dev/null +++ b/challenge-258/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +# 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 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/sum0/; + +my $cases = [ + [[2, 5, 9, 11, 3], 1], + [[2, 5, 9, 11, 3], 2], + [[2, 5, 9, 11, 3], 0], +]; + +sub sum_of_values +{ + my ($l, $k) = $_[0]->@*; + + my $sum = 0; + for my $i (0..$#$l) { + my $ones = sum0(grep { $_ } split('', unpack("B32", pack("N", $i)))); + $sum += $l->[$i] if $ones == $k; + } + return $sum; +} + +is(sum_of_values($cases->[0]), 17, 'Example 1'); +is(sum_of_values($cases->[1]), 11, 'Example 2'); +is(sum_of_values($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; + -- cgit From 42985187d08b013efc571821618c13ac8ca70076 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 26 Feb 2024 11:55:40 -0500 Subject: 258 DAJ --- challenge-258/dave-jacoby/perl/ch-1.pl | 23 +++++++++++++++++ challenge-258/dave-jacoby/perl/ch-2.pl | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 challenge-258/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-258/dave-jacoby/perl/ch-2.pl diff --git a/challenge-258/dave-jacoby/perl/ch-1.pl b/challenge-258/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..e9152964ba --- /dev/null +++ b/challenge-258/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 10, 1, 111, 24, 1000 ], + [ 111, 1, 11111 ], + [ 2, 8, 1024, 256 ], +); + +for my $example (@examples) { + my $output = scalar grep { ( length $_ ) % 2 == 0 } $example->@*; + my $ints = join ', ', $example->@*; + + say <<~"END"; + Input: \@ints = ($ints) + Output: $output + END +} + diff --git a/challenge-258/dave-jacoby/perl/ch-2.pl b/challenge-258/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..501e50a669 --- /dev/null +++ b/challenge-258/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + { + ints => [ 2, 5, 9, 11, 3 ], + k => 1 + }, + { + ints => [ 2, 5, 9, 11, 3 ], + k => 2 + }, + { + ints => [ 2, 5, 9, 11, 3 ], + k => 0 + } +); + +for my $example (@examples) { + my @output = sum_of_values($example); + my $ints = join ', ', $example->{ints}->@*; + my $k = join ', ', $example->{k}; + my $output = join ', ', @output; + + say <<~"END"; + Input: \@ints = ($ints), \$k = $k + Output: $output + END +} + +sub sum_of_values ($obj) { + my @ints = $obj->{ints}->@*; + my $k = $obj->{k}; + my $output = 0; + + for my $i ( 0 .. $#ints ) { + my $s = sum0 split //, sprintf '%b', $i; + $output += $ints[$i] if $s == $k; + } + return $output; +} -- cgit From 0e59e25786af5ec1b483205e48a66a7257d7f934 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 26 Feb 2024 11:58:14 -0500 Subject: 258 DAJ blogged --- challenge-258/dave-jacoby/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-258/dave-jacoby/blog.txt diff --git a/challenge-258/dave-jacoby/blog.txt b/challenge-258/dave-jacoby/blog.txt new file mode 100644 index 0000000000..151213d6f6 --- /dev/null +++ b/challenge-258/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/02/26/no-mental-bandwidth-for-a-name-weekly-challenge-258.html -- cgit From 520ff2a4b215f71d37443addb8a4dfcab7a47902 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 26 Feb 2024 13:34:57 -0600 Subject: Solve PWC258 --- challenge-258/wlmb/blog.txt | 1 + challenge-258/wlmb/perl/ch-1.pl | 13 +++++++++++++ challenge-258/wlmb/perl/ch-2.pl | 16 ++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 challenge-258/wlmb/blog.txt create mode 100755 challenge-258/wlmb/perl/ch-1.pl create mode 100755 challenge-258/wlmb/perl/ch-2.pl diff --git a/challenge-258/wlmb/blog.txt b/challenge-258/wlmb/blog.txt new file mode 100644 index 0000000000..e14ee95eb1 --- /dev/null +++ b/challenge-258/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/02/26/PWC258/ diff --git a/challenge-258/wlmb/perl/ch-1.pl b/challenge-258/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..2b68642530 --- /dev/null +++ b/challenge-258/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 258 +# Task 1: Count Even Digits Number +# +# See https://wlmb.github.io/2024/02/26/PWC258/#task-1-count-even-digits-number +use v5.36; +use List::Util qw(all); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to count how many input numbers have an even number of digits. + FIN +die "Only digits allowed" unless all {/^\d+$/} @ARGV; +say "@ARGV -> ", 0+grep {(split "")%2==0}@ARGV; diff --git a/challenge-258/wlmb/perl/ch-2.pl b/challenge-258/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..7a7338d903 --- /dev/null +++ b/challenge-258/wlmb/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +# Perl weekly challenge 258 +# Task 2: Sum of Values +# +# See https://wlmb.github.io/2024/02/26/PWC258/#task-2-sum-of-values +use v5.36; +use List::Util qw(sum0); +die <<~"FIN" unless @ARGV; + Usage: $0 K N0 [N1...] + to sum the values of Nm whose index m has K ones in its binary representation + FIN +my ($k,@int)=@ARGV; +# Convert to binary, add ones and compare to $k to filter indices +my @indices=grep {$k==sum0 split "", sprintf "%b",$_}0..@int-1; +my $result=sum0 @int[@indices]; +say "k=$k, ints=@int -> ", $result; -- cgit From fa9b1aca1f30a93ca612663fac8b8d3c809d1aa4 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 26 Feb 2024 21:02:22 +0100 Subject: Add solution 258 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-258/jeanluc2020/blog-1.txt | 1 + challenge-258/jeanluc2020/blog-2.txt | 1 + challenge-258/jeanluc2020/perl/ch-1.pl | 56 +++++++++++++++++++++++ challenge-258/jeanluc2020/perl/ch-2.pl | 78 ++++++++++++++++++++++++++++++++ challenge-258/jeanluc2020/python/ch-1.py | 48 ++++++++++++++++++++ challenge-258/jeanluc2020/python/ch-2.py | 66 +++++++++++++++++++++++++++ 6 files changed, 250 insertions(+) create mode 100644 challenge-258/jeanluc2020/blog-1.txt create mode 100644 challenge-258/jeanluc2020/blog-2.txt create mode 100755 challenge-258/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-258/jeanluc2020/perl/ch-2.pl create mode 100755 challenge-258/jeanluc2020/python/ch-1.py create mode 100755 challenge-258/jeanluc2020/python/ch-2.py 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..782cf