From c059b0cd682251245b1884778f6dbd791225a8ed Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 11:33:23 +0100 Subject: PWC 245 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 --- challenge-245/luca-ferrari/blog-1.txt | 1 + challenge-245/luca-ferrari/blog-2.txt | 1 + challenge-245/luca-ferrari/blog-3.txt | 1 + challenge-245/luca-ferrari/blog-4.txt | 1 + challenge-245/luca-ferrari/blog-5.txt | 1 + challenge-245/luca-ferrari/blog-6.txt | 1 + challenge-245/luca-ferrari/blog-7.txt | 1 + challenge-245/luca-ferrari/blog-8.txt | 1 + challenge-245/luca-ferrari/postgresql/ch-1.plperl | 30 +++++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-1.sql | 25 ++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-2.plperl | 31 +++++++++++++++++++ challenge-245/luca-ferrari/postgresql/ch-2.sql | 16 ++++++++++ challenge-245/luca-ferrari/python/ch-1.py | 31 +++++++++++++++++++ challenge-245/luca-ferrari/python/ch-2.py | 36 +++++++++++++++++++++++ challenge-245/luca-ferrari/raku/ch-1.p6 | 22 ++++++++++++++ challenge-245/luca-ferrari/raku/ch-2.p6 | 21 +++++++++++++ 16 files changed, 220 insertions(+) create mode 100644 challenge-245/luca-ferrari/blog-1.txt create mode 100644 challenge-245/luca-ferrari/blog-2.txt create mode 100644 challenge-245/luca-ferrari/blog-3.txt create mode 100644 challenge-245/luca-ferrari/blog-4.txt create mode 100644 challenge-245/luca-ferrari/blog-5.txt create mode 100644 challenge-245/luca-ferrari/blog-6.txt create mode 100644 challenge-245/luca-ferrari/blog-7.txt create mode 100644 challenge-245/luca-ferrari/blog-8.txt create mode 100644 challenge-245/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-245/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-245/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-245/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-245/luca-ferrari/python/ch-1.py create mode 100644 challenge-245/luca-ferrari/python/ch-2.py create mode 100644 challenge-245/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-245/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-245/luca-ferrari/blog-1.txt b/challenge-245/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..2a976c1f21 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1 diff --git a/challenge-245/luca-ferrari/blog-2.txt b/challenge-245/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..34de71d420 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2 diff --git a/challenge-245/luca-ferrari/blog-3.txt b/challenge-245/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..5807d09548 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1plperl diff --git a/challenge-245/luca-ferrari/blog-4.txt b/challenge-245/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..835c77240b --- /dev/null +++ b/challenge-245/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2plperl diff --git a/challenge-245/luca-ferrari/blog-5.txt b/challenge-245/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..7321c71d7b --- /dev/null +++ b/challenge-245/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1plpgsql diff --git a/challenge-245/luca-ferrari/blog-6.txt b/challenge-245/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..f9276df669 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2plpgsql diff --git a/challenge-245/luca-ferrari/blog-7.txt b/challenge-245/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..486ba8e95a --- /dev/null +++ b/challenge-245/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1python diff --git a/challenge-245/luca-ferrari/blog-8.txt b/challenge-245/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..eb15ccc483 --- /dev/null +++ b/challenge-245/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task2python diff --git a/challenge-245/luca-ferrari/postgresql/ch-1.plperl b/challenge-245/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..8742a00d10 --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task1_plperl( text[], int[] ) +RETURNS SETOF text +AS $CODE$ + my ( $langs, $popularity ) = @_; + + die "Not same length arrays!" if ( $langs->@* != $popularity->@* ); + + my $sorting = {}; + + for my $index ( 0 .. $popularity->@* - 1 ) { + $sorting->{ $popularity->[ $index ] } = $langs->[ $index ]; + } + + for ( sort { $b <=> $a } $popularity->@* ) { + return_next( $sorting->{ $_ } ); + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-245/luca-ferrari/postgresql/ch-1.sql b/challenge-245/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..236af438b8 --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,25 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task1_plpgsql( langs text[], popularity int[] ) +RETURNS SETOF text +AS $CODE$ + + WITH sorting AS ( + SELECT l + FROM unnest( langs ) l + , unnest( popularity ) p + ORDER BY p DESC + ) + SELECT distinct( l ) + FROM sorting; + +$CODE$ +LANGUAGE sql; diff --git a/challenge-245/luca-ferrari/postgresql/ch-2.plperl b/challenge-245/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..f7ac4c602f --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,31 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + use Algorithm::Combinatorics qw/ permutations /; + my ( $digits ) = @_; + + die "Digits must be between 0 and 9" if ( grep( { $_ > 9 || $_ < 0 } $digits->@* ) ); + + my $result = -1; + for my $k ( 0 .. $digits->@* ) { + my $permutations = permutations( \ $digits->@*, $k ); + while ( my $iter = $permutations->next ) { + my $value = join('', $iter->@* ); + next if ( $value % 3 != 0 ); + $result = $value if ( $value > $result ); + } + } + + return $result; + +$CODE$ +LANGUAGE plperlu; diff --git a/challenge-245/luca-ferrari/postgresql/ch-2.sql b/challenge-245/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..9f3e8da91f --- /dev/null +++ b/challenge-245/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 245 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc245; + +CREATE OR REPLACE FUNCTION +pwc245.task2_plpgsql( digits int[] ) +RETURNS int +AS $CODE$ + SELECT pwc245.task2_plperl( digits ); +$CODE$ +LANGUAGE sql; diff --git a/challenge-245/luca-ferrari/python/ch-1.py b/challenge-245/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..4e63b95892 --- /dev/null +++ b/challenge-245/luca-ferrari/python/ch-1.py @@ -0,0 +1,31 @@ +#!python + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + langs = argv[ 0 ].split( "|" ) + popularity = list( map( int, argv[1].split( "|" ) ) ) + + sorting = {} + for i in range( 0, len( popularity ) ): + sorting[ langs[ i ] ] = popularity[ i ] + + + for v in sorted( sorting.items(), key=lambda x: x[1], reverse=True ): + print( v[0] ) + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-245/luca-ferrari/python/ch-2.py b/challenge-245/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..a6a7d4ecde --- /dev/null +++ b/challenge-245/luca-ferrari/python/ch-2.py @@ -0,0 +1,36 @@ +#!python + +# +# Perl Weekly Challenge 245 +# Task 2 +# +# See +# + +import sys +from itertools import permutations + +# task implementation +def main( argv ): + digits = list( map( int, argv ) ) + + result = -1 + + for p in permutations( digits ): + value = int( "".join( map( str, p ) ) ) + + if value % 3 != 0: + continue + + if value > result: + result = value + + print( result ) + + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-245/luca-ferrari/raku/ch-1.p6 b/challenge-245/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..dd55dadcf5 --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +# $ raku raku/ch-1.p6 --langs="c" --langs="java" --langs="python" --popularity=2 --popularity=3 --popularity=5 + +sub MAIN( :@langs where { @langs.elems == @langs.grep( * ~~ Str ).elems } + , :@popularity where { @popularity.elems == @langs.elems == @popularity.grep( * ~~ Int ).elems } ) { + + my %sorted; + %sorted{ @popularity[ $_ ] } = @langs[ $_ ] for 0 ..^ @langs.elems; + + my @output; + @output.push: %sorted{ $_ } for %sorted.keys.sort( { $^b <=> $^a } ); + @output.join( ', ' ).say; + +} diff --git a/challenge-245/luca-ferrari/raku/ch-2.p6 b/challenge-245/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..aad5089c8e --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,21 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 2 +# +# See +# + +sub MAIN( *@nums where { @nums.elems == @nums.grep( * ~~ Int ).elems + && @nums.grep( 0 <= * <= 9 ).elems } ) { + + my $largest = -1; + for @nums.permutations { + my $value = $_.join.Int; + next if $value !%% 3; + $largest = $value if ( $value > $largest ); + } + + $largest.say; +} -- cgit From b25d7be5fb1d090cea6b41c889120de935c4b5e0 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Mon, 27 Nov 2023 09:31:22 -0300 Subject: This week's oneliners --- challenge-245/massa/raku/ch-1.raku | 58 +++++++++++++++++++++++++++++++++ challenge-245/massa/raku/ch-2.raku | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 challenge-245/massa/raku/ch-1.raku create mode 100644 challenge-245/massa/raku/ch-2.raku diff --git a/challenge-245/massa/raku/ch-1.raku b/challenge-245/massa/raku/ch-1.raku new file mode 100644 index 0000000000..1290eda32a --- /dev/null +++ b/challenge-245/massa/raku/ch-1.raku @@ -0,0 +1,58 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Sort Language + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given two array of languages and its popularity. + +Write a script to sort the language based on popularity. + +=head3 Example 1: + + Input: @lang = ('perl', 'c', 'python') + @popularity = (2, 1, 3) + Output: ('c', 'perl', 'python')=head3 Example 2: + +=head3 Example 3: + + Input: @lang = ('c++', 'haskell', 'java') + @popularity = (1, 3, 2) + Output: ('c++', 'java', 'haskell') + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@ (@lang, @pop)) { + (@pop Z=> @lang).sort(*.keys)».value +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = + %{ input => (, (2, 1, 3)), + output => }, + %{ input => (, (1, 3, 2)), + output => }, + ; + + ..&SOLUTION.deepmap({$_}).&is-deeply: ., . for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-245/massa/raku/ch-2.raku b/challenge-245/massa/raku/ch-2.raku new file mode 100644 index 0000000000..fef55ba845 --- /dev/null +++ b/challenge-245/massa/raku/ch-2.raku @@ -0,0 +1,67 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Largest of Three + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of integers >= 0. + +Write a script to return the largest number formed by concatenating some of the +given integers in any order which is also multiple of 3. Return -1 if none +found. + +=head3 Example 1: + + Input: @digits = (8, 1, 9) + Output: 981 + + 981 % 3 == 0 + +=head3 Example 2: + + Input: @digits = (8, 6, 7, 1, 0) + Output: 8760 + +=head3 Example 3: + + Input: @digits = (1) + Output: -1 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(@_) { + @_.combinations.skip(1).map(*.permutations».join».Int).flat.sort(-*).first(* %% 3) or -1 +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = + %{ input => (8, 1, 9), + output => (981,) }, + %{ input => (8, 6, 7, 1, 0), + output => (8760,) }, + %{ input => (1,), + output => (-1,) }, + ; + + ..&SOLUTION.deepmap({$_}).&is-deeply: ., . for @tests +} # end of multi MAIN (Bool :$test!) + + -- cgit From 604e7c781e9220952021980cb1379242d5873b62 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 15:36:11 +0100 Subject: Another implementation in Raku --- challenge-245/luca-ferrari/raku/ch-1b.p6 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-245/luca-ferrari/raku/ch-1b.p6 diff --git a/challenge-245/luca-ferrari/raku/ch-1b.p6 b/challenge-245/luca-ferrari/raku/ch-1b.p6 new file mode 100644 index 0000000000..f1fb96d322 --- /dev/null +++ b/challenge-245/luca-ferrari/raku/ch-1b.p6 @@ -0,0 +1,16 @@ +#!raku + +# +# Perl Weekly Challenge 245 +# Task 1 +# +# See +# + +# $ raku raku/ch-1.p6 --langs="c" --langs="java" --langs="python" --popularity=2 --popularity=3 --popularity=5 + +sub MAIN( :@langs where { @langs.elems == @langs.grep( * ~~ Str ).elems } + , :@popularity where { @popularity.elems == @langs.elems == @popularity.grep( * ~~ Int ).elems } ) { + + ( @langs [Z] @popularity ).sort( { $^b[1] <=> $^a[1] } ).map( *[ 0 ] ).join( ',' ).say; +} -- cgit From d75e53fddfcada34523e16efded96b7e5b895528 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 27 Nov 2023 15:40:16 +0100 Subject: Another approach blog reference --- challenge-245/luca-ferrari/blog-9.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-245/luca-ferrari/blog-9.txt diff --git a/challenge-245/luca-ferrari/blog-9.txt b/challenge-245/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..91d5c777fa --- /dev/null +++ b/challenge-245/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/11/27/PerlWeeklyChallenge245.html#task1b -- cgit From 4a23c97a11a0f11ed2eb6d555a34f2b8183da019 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 27 Nov 2023 15:31:55 +0000 Subject: w245 - Task 1 & 2 --- challenge-245/perlboy1967/perl/ch1.pl | 39 ++++++++++++++++++++++++++++ challenge-245/perlboy1967/perl/ch2.pl | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 challenge-245/perlboy1967/perl/ch1.pl create mode 100755 challenge-245/perlboy1967/perl/ch2.pl diff --git a/challenge-245/perlboy1967/perl/ch1.pl b/challenge-245/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..93878d5564 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Sort Language +Submitted by: Mohammad S Anwar + +You are given two array of languages and its popularity. + +Write a script to sort the language based on popularity. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use List::MoreUtils qw(pairwise); + +sub sortLanguage (\@\@) { + map { $_->[0] } + sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } + pairwise { [$a,$b] } @{$_[0]},@{$_[1]}; +} + +is([sortLanguage(@{[qw{Perl C Python}]},@{[2,1,3]})], + [qw(C Perl Python)]); +is([sortLanguage(@{[qw{C++ Haskell Java}]},@{[1,3,2]})], + [qw(C++ Java Haskell)]); +is([sortLanguage(@{[qw{C Basic Assembly}]},@{[1,1,1]})], + [qw(Assembly Basic C)]); + +done_testing; diff --git a/challenge-245/perlboy1967/perl/ch2.pl b/challenge-245/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..8c18d7fde4 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch2.pl @@ -0,0 +1,49 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Largest of Three +Submitted by: Mohammad S Anwar + +You are given an array of integers >= 0. + +Write a script to return the largest number formed by concatenating some of the given +integers in any order which is also multiple of 3. Return -1 if none found. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(permutations subsets); + +sub largestOfThree (@) { + my @l = sort { $b <=> $a } @_; + + my $subsetSize = $#l+1; + while ($subsetSize > 0) { + for my $subset (subsets(\@l,$subsetSize)) { + my @subset = sort { $b <=> $a } @$subset; + for my $p (permutations(\@subset)) { + my $s = join('',@$p); + return $s if ($s % 3 == 0); + } + $subsetSize--; + } + } + return -1; +} + + +is(largestOfThree(8,1,9),981); +is(largestOfThree(8,6,7,1,0),8760); +is(largestOfThree(1),-1); + +done_testing; -- cgit From b340a22018bf5f874e9a0749132647cd4e411773 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 27 Nov 2023 17:44:02 +0000 Subject: Week 245 --- challenge-245/peter-campbell-smith/blog.txt | 1 + challenge-245/peter-campbell-smith/perl/ch-1.pl | 24 +++++++++ challenge-245/peter-campbell-smith/perl/ch-2.pl | 69 +++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 challenge-245/peter-campbell-smith/blog.txt create mode 100755 challenge-245/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-245/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-245/peter-campbell-smith/blog.txt b/challenge-245/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..ab6c9e16fb --- /dev/null +++ b/challenge-245/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/245 diff --git a/challenge-245/peter-campbell-smith/perl/ch-1.pl b/challenge-245/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..6cad9a701d --- /dev/null +++ b/challenge-245/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-11-27 +use utf8; # Week 245 task 1 - Sort language +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +sort_language(['perl', 'c', 'python'], [2, 1, 3]); +sort_language(['perl', 'fortran', 'algol', 'pascal', 'html', 'apl', 'c++', 'French'], + [7, 3, 5, 1, 8, 2, 4, 6]); + +sub sort_language { + + my (@languages, @ranks, @order); + + @languages = @{$_[0]}; + @ranks = @{$_[1]}; + + $order[$ranks[$_] - 1] = $languages[$_] for 0 .. @languages - 1; + + say qq[\nInput: \@lang = ('] . join(q[', '], @languages) . q[')]; + say qq[ \@popularity = (] . join(', ', @ranks) . ')'; + say qq[Output: ('] . join(q[', '], @order) . q[')]; +} diff --git a/challenge-245/peter-campbell-smith/perl/ch-2.pl b/challenge-245/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..a9f90640ff --- /dev/null +++ b/challenge-245/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-11-27 +use utf8; # Week 245 task 2 - Largest of three +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use Algorithm::Combinatorics 'combinations'; + +my ($j, @digits); + +largest_of_three(8, 6, 7, 1, 0); +largest_of_three(0); +largest_of_three(18446744073709551614, 3); +largest_of_three(333, 666, 1, 4, 7); + +# for $j (0..9) { + # push @digits, int(rand(10000)); +# } +# largest_of_three(@digits); +largest_of_three(13, 14, 1, 102); + +sub largest_of_three { + + my (@digits, $c, $iter, $comb, @set, $value, $result, $length); + + # loop over sizes of combination to consider + @digits = @_; + $result = -1; + $length = 0; + + for ($c = @digits; $c > 0; $c --) { + + # iterate over combinations of length $c numbers + $iter = combinations(\@digits, $c); + while ($comb = $iter->next) { + + # reverse sort them alphabetically to get best bet first + @set = reverse sort @$comb; + + # concatenate them and see if that's the best so far + $value = join('', @set); + + # this if is just: if (mult_of_3($value) and $value > $result) accommodating BigInts + if (is_mult_of_3($value) and (length($value) > $length + or (length($value) eq $length and $value gt $result))) { + $result = $value; + $length = length($result); + } + } + + # if we have an answer, no smaller subset will produce a larger number + last if $result >= 0; + } + + say qq[\nInput: \@digits = (] . join(q[, ], @_) . q[)]; + say qq[Output: $result]; +} + +sub is_mult_of_3 { + + my ($input, $sum); + + $input = shift; + $sum = 0; + $sum += $1 while $input =~ m|(\d)|g; + return $sum % 3 == 0; + +} -- cgit From 7684627bd26166f4c958e3e4c75d894ecb4476d3 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 27 Nov 2023 19:55:41 +0100 Subject: Add solution 245 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-245/jeanluc2020/blog-1.txt | 1 + challenge-245/jeanluc2020/blog-2.txt | 1 + challenge-245/jeanluc2020/perl/ch-1.pl | 50 +++++++++++++++++++++ challenge-245/jeanluc2020/perl/ch-2.pl | 74 ++++++++++++++++++++++++++++++++ challenge-245/jeanluc2020/python/ch-1.py | 42 ++++++++++++++++++ challenge-245/jeanluc2020/python/ch-2.py | 74 ++++++++++++++++++++++++++++++++ 6 files changed, 242 insertions(+) create mode 100644 challenge-245/jeanluc2020/blog-1.txt create mode 100644 challenge-245/jeanluc2020/blog-2.txt create mode 100755 challenge-245/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-245/jeanluc2020/perl/ch-2.pl create mode 100755 challenge-245/jeanluc2020/python/ch-1.py create mode 100755 challenge-245/jeanluc2020/python/ch-2.py diff --git a/challenge-245/jeanluc2020/blog-1.txt b/challenge-245/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..3f87b3fcdb --- /dev/null +++ b/challenge-245/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-245-1.html diff --git a/challenge-245/jeanluc2020/blog-2.txt b/challenge-245/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..ae1707d73e --- /dev/null +++ b/challenge-245/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-245-2.html diff --git a/challenge-245/jeanluc2020/perl/ch-1.pl b/challenge-245/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..55400d0010 --- /dev/null +++ b/challenge-245/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK1 +# +# Task 1: Sort Language +# ===================== +# +# You are given two array of languages and its popularity. +# +# Write a script to sort the language based on popularity. +# Example 1 +# +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') +# +############################################################ +## +## discussion +## +############################################################ +# +# The solution that jumps right into my face is to fill the data +# into a hash table, using lang as the key and popularity as the +# value. Then sort the keys by their values and put the result into +# the output. + +use strict; +use warnings; + +sort_language( ['perl', 'c', 'python'], [2, 1, 3] ); +sort_language( ['c++', 'haskell', 'java'], [1, 3, 2] ); + +sub sort_language { + my ($lang, $popularity) = @_; + print "Input: (" . join(", ", @$lang) . ")\n (" . join(", ", @$popularity) . ")\n"; + my $hash = {}; + my $index = 0; + foreach my $elem (@$lang) { + $hash->{$elem} = $popularity->[$index]; + $index++; + } + print "Output: (" . join(", ", sort { $hash->{$a} <=> $hash->{$b} } keys %$hash) . ")\n"; +} + diff --git a/challenge-245/jeanluc2020/perl/ch-2.pl b/challenge-245/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..f35a03c03f --- /dev/null +++ b/challenge-245/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK2 +# +# Task 2: Largest of Three +# ======================== +# +# You are given an array of integers >= 0. +# +# Write a script to return the largest number formed by concatenating some of +# the given integers in any order which is also multiple of 3. Return -1 if +# none found. +# +## Example 1 +## +## Input: @ints = (8, 1, 9) +## Output: 981 +## +## 981 % 3 == 0 +# +## Example 2 +## +## Input: @ints = (8, 6, 7, 1, 0) +## Output: 8760 +# +## Example 3 +## +## Input: @ints = (1) +## Output: -1 +# +############################################################ +## +## discussion +## +############################################################ +# +# While all examples in the description use single-digit numbers, +# there is nothing that would require this. So in order to catch all +# solutions, we need all possible permutations of all subsets of the +# array and of the numbers created out of those we need the biggest +# one that is divisible by 3. + +use strict; +use warnings; +use Algorithm::Combinatorics qw(permutations subsets); + +largest_of_three(8, 1, 9); +largest_of_three(8, 6, 7, 1, 0); +largest_of_three(1); +largest_of_three(8, 60, 7); +largest_of_three(80, 6, 7); + +sub largest_of_three { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result; + my $iter = subsets(\@ints); + while(my $s = $iter->next) { + next unless @$s; + my $iter2 = permutations($s); + while(my $p = $iter2->next) { + my $num = join("", @$p); + next unless $num; + next unless $num % 3 == 0; + $result = $num unless defined $result; + $result = $num if $num > $result; + } + } + if(defined $result) { + print "Output: $result\n"; + } else { + print "Output: -1\n"; + } +} + diff --git a/challenge-245/jeanluc2020/python/ch-1.py b/challenge-245/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..98da163a99 --- /dev/null +++ b/challenge-245/jeanluc2020/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK1 +# +# Task 1: Sort Language +# ===================== +# +# You are given two array of languages and its popularity. +# +# Write a script to sort the language based on popularity. +# Example 1 +# +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') +# +############################################################ +## +## discussion +## +############################################################ +# +# The solution that jumps right into my face is to fill the data +# into a hash table, using lang as the key and popularity as the +# value. Then sort the keys by their values and put the result into +# the output. + +def sort_language(lang: list, popularity: list): + print("Input: (", ", ".join(str(x) for x in lang), "), (", ", ".join(str(x) for x in popularity), ")") + hash = {} + for i in range(len(lang)): + hash[lang[i]] = popularity[i] + print("Output: (", ", ".join(str(x) for x in sorted(hash, key=hash.get)), ")") + +sort_language( ['perl', 'c', 'python'], [2, 1, 3] ); +sort_language( ['c++', 'haskell', 'java'], [1, 3, 2] ); + diff --git a/challenge-245/jeanluc2020/python/ch-2.py b/challenge-245/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..5b11711684 --- /dev/null +++ b/challenge-245/jeanluc2020/python/ch-2.py @@ -0,0 +1,74 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/#TASK2 +# +# Task 2: Largest of Three +# ======================== +# +# You are given an array of integers >= 0. +# +# Write a script to return the largest number formed by concatenating some of +# the given integers in any order which is also multiple of 3. Return -1 if +# none found. +# +## Example 1 +## +## Input: @ints = (8, 1, 9) +## Output: 981 +## +## 981 % 3 == 0 +# +## Example 2 +## +## Input: @ints = (8, 6, 7, 1, 0) +## Output: 8760 +# +## Example 3 +## +## Input: @ints = (1) +## Output: -1 +# +############################################################ +## +## discussion +## +############################################################ +# +# While all examples in the description use single-digit numbers, +# there is nothing that would require this. So in order to catch all +# solutions, we need all possible permutations of all subsets of the +# array and of the numbers created out of those we need the biggest +# one that is divisible by 3. + +from itertools import chain, combinations, permutations + +def powerset(iterable): + "powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" + s = list(iterable) + return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) + +def largest_of_three(ints: list): + print("Input: (", ", ".join(str(x) for x in ints), ")") + result = int() + for myset in powerset(ints): + for c in permutations(myset, len(myset)): + v = str("".join(str(x) for x in c)) + # print("-> ", v) + if len(v) > 0: + # print("Considering", v, "as a value") + value = int(v) + if value % 3 == 0: + if len(str(result)) == 0: + result = value + if value > result: + result = value + if len(str(result)) == 0: + print("Output: -1") + else: + print("Output:", str(result)) + +largest_of_three([8, 1, 9]); +largest_of_three([8, 6, 7, 1, 0]); +largest_of_three([1]); +largest_of_three([8, 60, 7]); +largest_of_three([80, 6, 7]); + -- cgit From 7b2bc56dcf7849d613cdd0f956bb776b79484877 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 27 Nov 2023 21:10:15 +0100 Subject: challenge-245 --- challenge-245/peter-meszaros/perl/ch-1.pl | 44 ++++++++++++++++++++ challenge-245/peter-meszaros/perl/ch-2.pl | 67 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100755 challenge-245/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-245/peter-meszaros/perl/ch-2.pl diff --git a/challenge-245/peter-meszaros/perl/ch-1.pl b/challenge-245/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c112798e54 --- /dev/null +++ b/challenge-245/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# +# You are given two array of languages and its popularity. +# +# Write a script to sort the language based on popularity. +# Example 1 +# +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [ ['perl', 'c', 'python'], [2, 1, 3] ], + [ ['c++', 'haskell', 'java'], [1, 3, 2] ], +]; + +sub sort_language +{ + my $lang = shift; + my $popularity = shift; + + my %langpop = map { $lang->[$_] => $popularity->[$_] } (0..$#$lang); + return [ sort { $langpop{$a} <=> $langpop{$b}} keys %langpop ]; +} + +is_deeply(sort_language($cases->[0]->@*), ['c', 'perl', 'python'], "['perl', 'c', 'python'], [2, 1, 3]"); +is_deeply(sort_language($cases->[1]->@*), ['c++', 'java', 'haskell'], "['c++', 'haskell', 'java'], [1, 3, 2]"); +done_testing(); + +exit 0; + + diff --git a/challenge-245/peter-meszaros/perl/ch-2.pl b/challenge-245/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..d010b0e2e0 --- /dev/null +++ b/challenge-245/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# +# You are given an array of integers >= 0. +# +# Write a script to return the largest number formed by concatenating some of +# the given integers in any order which is also multiple of 3. Return -1 if none +# found. +# +# Example 1 +# +# Input: @ints = (8, 1, 9) +# Output: 981 +# +# 981 % 3 == 0 +# +# Example 2 +# +# Input: @ints = (8, 6, 7, 1, 0) +# Output: 8760 +# +# Example 3 +# +# Input: @ints = (1) +# Output: -1 +# + +use strict; +use warnings; +use Test::More; +use Data::Dumper; +use Algorithm::Combinatorics qw/variations/; + +my $cases = [ + [8, 1, 9], + [8, 6, 7, 1, 0], + [1], +]; + +sub largest_of_three +{ + my $l = shift; + + my $num = -1; + my @l; + for my $k (1..(scalar @$l)) { + my $iter = variations($l, $k); + while (my $p = $iter->next) { + push @l, 0+join('', @$p); + } + } + + for my $n (sort {$b <=> $a} @l) { + if ($n % 3 == 0) { + $num = $n; + last; + } + } + + return $num; +} + +is(largest_of_three($cases->[0]), 981, '[8, 1, 9]'); +is(largest_of_three($cases->[1]), 8760, '[8, 6, 7, 1, 0]'); +is(largest_of_three($cases->[2]), -1, '[1]'); +done_testing(); + +exit 0; -- cgit From 09e358d530f921d49e36bafaa2819fdd6a645962 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 27 Nov 2023 21:43:28 +0100 Subject: Add solutions to 245: Sort Language & Largest of Three by E. Choroba --- challenge-245/e-choroba/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-245/e-choroba/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 challenge-245/e-choroba/perl/ch-1.pl create mode 100755 challenge-245/e-choroba/perl/ch-2.pl diff --git a/challenge-245/e-choroba/perl/ch-1.pl b/challenge-245/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..a2db097214 --- /dev/null +++ b/challenge-245/e-choroba/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::MoreUtils qw{ nsort_by }; + +sub sort_language($lang, $popularity) { + [@$lang[ nsort_by { $popularity->[$_] } 0 .. $#$popularity ]] +} + +use Test::More tests => 2; + +is_deeply sort_language(['perl', 'c', 'python'], [2, 1, 3]), + ['c', 'perl', 'python'], + 'Example 1'; + +is_deeply sort_language(['c++', 'haskell', 'java'], [1, 3, 2]), + ['c++', 'java', 'haskell'], + 'Example 2'; diff --git a/challenge-245/e-choroba/perl/ch-2.pl b/challenge-245/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..73a789f123 --- /dev/null +++ b/challenge-245/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Algorithm::Combinatorics qw{ variations }; +use List::Util qw{ sum }; + +sub largest_of_three(@ints) { + my $max = -1; + for my $length (reverse 1 .. @ints) { + for my $var (variations(\@ints, $length)) { + next unless 0 == sum(@$var) % 3; + + my $candidate = 0 + join "", @$var; + $max = $candidate if $candidate > $max; + } + } + return $max +} + +use Test::More tests => 3 + 6; + +is largest_of_three(8, 1, 9), 981, 'Example 1'; +is largest_of_three(8, 6, 7, 1, 0), 8760, 'Example 2'; +is largest_of_three(1), -1, 'Example 3'; + +is largest_of_three(0, 0, 0), 0, 'Zero'; +is largest_of_three(4, 8, 911), 9114, 'n>9'; +is largest_of_three(8, 85, 0), 8850, 'n>9'; +is largest_of_three(8, 89, 2), 8982, 'n>9'; +is largest_of_three(8, 76, 0), 8760, 'n>9'; +is largest_of_three(8, 94, 0), 9480, 'n>9'; -- cgit From 27232da67afa8341279a318f5d22591c44f5a611 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 27 Nov 2023 16:31:30 -0500 Subject: Week 245 --- challenge-245/zapwai/perl/ch-1.pl | 9 +++++++++ challenge-245/zapwai/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 challenge-245/zapwai/perl/ch-1.pl create mode 100644 challenge-245/zapwai/perl/ch-2.pl diff --git a/challenge-245/zapwai/perl/ch-1.pl b/challenge-245/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..86820748f0 --- /dev/null +++ b/challenge-245/zapwai/perl/ch-1.pl @@ -0,0 +1,9 @@ +use v5.30; +my @lang = ('perl', 'c', 'python'); +my @pop = (2, 1, 3); +my @ans; +foreach my $i (0 .. $#pop) { + $ans[$pop[$i] - 1] = $lang[$i]; +} +say "Input : \@lang = @lang\n \t\@pop = @pop"; +say "Output: @ans"; diff --git a/challenge-245/zapwai/perl/ch-2.pl b/challenge-245/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e2acbbfd13 --- /dev/null +++ b/challenge-245/zapwai/perl/ch-2.pl @@ -0,0 +1,29 @@ +use v5.30; +use Math::Combinatorics; +use Algorithm::Permute; +use List::Util; +my @digits = (8,1,9); +@digits = (8,6,7,1,0); +my @poss; + +for my $cnt (1 .. @digits) { + my $comb = Math::Combinatorics->new( + count => $cnt, + data => [@digits] + ); + while (my @combo = $comb->next_combination) { + my $p = Algorithm::Permute->new(\@combo); + while (my @perm = $p->next) { + my $num = join("", @perm); + push @poss, [@perm] if ($num % 3 == 0); + } + } +} + +my $max = -1; +for my $r (@poss) { + my $num = join("", @$r); + $max = $num if ($max < $num); +} +say "Input : \@digits = (" .join(", ",@digits).")"; +say "Output: $max"; -- cgit From 5386519863ab30940ffe068e984f9dceaf473037 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 27 Nov 2023 18:58:24 -0600 Subject: Solve PWC245 --- challenge-245/wlmb/blog.txt | 1 + challenge-245/wlmb/perl/ch-1.pl | 19 ++++++++++++++++++ challenge-245/wlmb/perl/ch-2.pl | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 challenge-245/wlmb/blog.txt create mode 100755 challenge-245/wlmb/perl/ch-1.pl create mode 100755 challenge-245/wlmb/perl/ch-2.pl diff --git a/challenge-245/wlmb/blog.txt b/challenge-245/wlmb/blog.txt new file mode 100644 index 0000000000..0719f68949 --- /dev/null +++ b/challenge-245/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/11/27/PWC245/ diff --git a/challenge-245/wlmb/perl/ch-1.pl b/challenge-245/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..bb318bfa83 --- /dev/null +++ b/challenge-245/wlmb/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 245 +# Task 1: Sort Language +# +# See https://wlmb.github.io/2023/11/27/PWC245/#task-1-sort-language +use v5.36; +die <<~"FIN" unless @ARGV && @ARGV%2==0; + Usage: $0 L1 P1 [L2 P2...] + to sort the space separated list of languages Ln + according to their popularity Pn + FIN +while(@ARGV){ + my @language=split " ", shift; + my @popularity=split " ", shift; + warn("Number of elements should coincide: languages: @language vs. popularities @popularity\n"), + next unless @language==@popularity; + my @sorted=map {$language[$_]} sort {$popularity[$a]<=>$popularity[$b]} 0..@language-1; + say "languages: @language\npopularities: @popularity\nsorted: @sorted\n"; +} diff --git a/challenge-245/wlmb/perl/ch-2.pl b/challenge-245/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..2c1b27e2a9 --- /dev/null +++ b/challenge-245/wlmb/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +# Perl weekly challenge 245 +# Task 2: Largest of Three +# +# See https://wlmb.github.io/2023/11/27/PWC245/#task-2-largest-of-three +use v5.36; +use List::Util qw(all); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find the largest concatenation of numbers N_i which yield a + multiple of 3. + FIN +die "Only non-negative numbers allowed" unless all {/\d+/} @ARGV; +my $index=0; +my $total; +my @one; +my @two; +my @sorted= sort{$a<=>$b}@ARGV; +for(@sorted){ + my $residue=$_%3; + push @one, $index if $residue==1; + push @two, $index if $residue==2; + $total+=$residue; + ++$index; +} +$total%=3; +my @candidates; +if($total==1){ + push @candidates, [$one[0]] if(@one>=1); + push @candidates, [@two[1,0]] if(@two>=2); +} +if($total==2){ + push @candidates, [@one[1,0]] if(@one>=2); + push @candidates, [$two[0]] if(@two>=1); +} +if(@candidates){ + my @to_remove=map {join "", @sorted[@$_]} @candidates; + my $index_to_remove=@candidates == 2 && $to_remove[1]<$to_remove[0]?1:0; + splice @sorted, $_, 1 for @{$candidates[$index_to_remove]}; +} +my $result=join "", reverse @sorted; +$result=-1 if $result eq ""; +$result=0 if $result==0; # "0000->0"; +say "@ARGV -> $result"; -- cgit From 7a0976d301a1090cbf3a9ae302e45b9a612d29b3 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 27 Nov 2023 21:51:47 +1100 Subject: pwc245 solution in python --- challenge-245/pokgopun/python/ch-1.py | 42 +++++++++++++++++++++++++ challenge-245/pokgopun/python/ch-2.py | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-245/pokgopun/python/ch-1.py create mode 100644 challenge-245/pokgopun/python/ch-2.py diff --git a/challenge-245/pokgopun/python/ch-1.py b/challenge-245/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..566c3310b1 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-1.py @@ -0,0 +1,42 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +""" + +Task 1: Sort Language + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given two array of languages and its popularity. + + Write a script to sort the language based on popularity. + +Example 1 + +Input: @lang = ('perl', 'c', 'python') + @popularity = (2, 1, 3) +Output: ('c', 'perl', 'python') + +Example 2 + +Input: @lang = ('c++', 'haskell', 'java') + @popularity = (1, 3, 2) +Output: ('c++', 'java', 'haskell') + +Task 2: Largest of Three +""" +### solution by pokgopun@gmail.com + +def sortL(tup1,tup2: tuple): + return tuple( + tup[0] for tup in + sorted( + map(lambda e1,e2: (e1,e2), tup1, tup2), + key=lambda tup: tup[1] + ) + ) + +for (inpt1,inpt2), otpt in { + (('perl', 'c', 'python'), (2, 1, 3)): ('c', 'perl', 'python'), + (('c++', 'haskell', 'java'), (1, 3, 2)): ('c++', 'java', 'haskell'), + }.items(): + print(otpt==sortL(inpt1, inpt2)) diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d689e65c04 --- /dev/null +++ b/challenge-245/pokgopun/python/ch-2.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +""" + +Task 2: Largest of Three + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers >= 0. + + Write a script to return the largest number formed by concatenating + some of the given integers in any order which is also multiple of 3. + Return -1 if none found. + +Example 1 + +Input: @digits = (8, 1, 9) +Output: 981 + +981 % 3 == 0 + +Example 2 + +Input: @digits = (8, 6, 7, 1, 0) +Output: 8760 + +Example 3 + +Input: @digits = (1) +Output: -1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 3rd December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import permutations + +def lot(tup: tuple): + for n in range(len(tup),0,-1): + for x in permutations( + sorted(tup,reverse=True), n + ): + y = int( + "".join( str(e) for e in x ) + ) + if y % 3 == 0: return y + return -1 + +for inpt, otpt in { + (8, 1, 9): 981, + (8, 6, 7, 1, 0): 8760, + (1,): -1, + }.items(): + print(otpt==lot(inpt)) -- cgit From 7afc1f8c9df147bf7e09970a11fd6600cbc4098f Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 28 Nov 2023 13:26:31 +1100 Subject: pwc245 solution in go --- challenge-245/pokgopun/go/ch-1.go | 60 +++++++++++++++ challenge-245/pokgopun/go/ch-2.go | 150 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 challenge-245/pokgopun/go/ch-1.go create mode 100644 challenge-245/pokgopun/go/ch-2.go diff --git a/challenge-245/pokgopun/go/ch-1.go b/challenge-245/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f4f1351345 --- /dev/null +++ b/challenge-245/pokgopun/go/ch-1.go @@ -0,0 +1,60 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +/*# + +Task 1: Sort Language + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given two array of languages and its popularity. + + Write a script to sort the language based on popularity. + +Example 1 + +Input: @lang = ('perl', 'c', 'python') + @popularity = (2, 1, 3) +Output: ('c', 'perl', 'python') + +Example 2 + +Input: @lang = ('c++', 'haskell', 'java') + @popularity = (1, 3, 2) +Output: ('c++', 'java', 'haskell') + +Task 2: Largest of Three +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "cmp" + "fmt" + "reflect" + "slices" +) + +type ranks []int + +type words []string + +func (w words) sort(r ranks) words { + slices.SortFunc(w, func(a, b string) int { + return cmp.Compare(r[slices.Index(w, a)], r[slices.Index(w, b)]) + }) + return w +} + +func main() { + for _, data := range []struct { + input1 words + input2 ranks + output words + }{ + {words{"perl", "c", "python"}, ranks{2, 1, 3}, words{"c", "perl", "python"}}, + {words{"c++", "haskell", "java"}, ranks{1, 3, 2}, words{"c++", "java", "haskell"}}, + } { + fmt.Println(reflect.DeepEqual(data.input1.sort(data.input2), data.output)) + } +} diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..74527e790b --- /dev/null +++ b/challenge-245/pokgopun/go/ch-2.go @@ -0,0 +1,150 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/ +/*# + +Task 2: Largest of Three + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers >= 0. + + Write a script to return the largest number formed by concatenating + some of the given integers in any order which is also multiple of 3. + Return -1 if none found. + +Example 1 + +Input: @digits = (8, 1, 9) +Output: 981 + +981 % 3 == 0 + +Example 2 + +Input: @digits = (8, 6, 7, 1, 0) +Output: 8760 + +Example 3 + +Input: @digits = (1) +Output: -1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 3rd December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "fmt" + "reflect" + "slices" +) + +func main() { + for _, data := range []struct { + input digits + output int + }{ + {digits{8, 1, 9}, 981}, + {digits{8, 6, 7, 1, 0}, 8760}, + {digits{1}, -1}, + } { + fmt.Println(reflect.DeepEqual(data.output, data.input.lot())) + } +} + +type digits []int + +func (d digits) concat() int { + r := d[0] + var n, t int + for _, v := range d[1:] { + n, t = v, 10 + for n > 10 { + t *= 10 + n /= 10 + } + r = r*t + v + } + return r +} + +func (d digits) lot() int { + slices.Sort(d) + slices.Reverse(d) + l := len(d) + for i := l; i > 0; i-- { + res, closer := d.permute(i) + for v := range res { + r := v.concat() + if r%3 == 0 { + closer() + return r + } + } + } + return -1 +} + +// transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations +func (d digits) permute(r int) (res chan digits, closer func()) { + res = make(chan digits) + done := make(chan struct{}) + n := len(d) + idx := make([]int, n) + for i := range idx { + idx[i] = i + } + cyc := make([]int, r) + for i := range cyc { + cyc[i] = n - i + } + go func() { + r0 := make(digits, r) + copy(r0, d[:r]) + select { + case <-done: + close(res) + return + case res <- r0: + } + for { + for i := r - 1; i >= 0; i-- { + cyc[i]-- + if cyc[i] == 0 { + idx = append(idx, idx[i]) + idx = slices.Delete(idx, i, i+1) + cyc[i] = n - i + } else { + j := cyc[i] + idx[i], idx[n-j] = idx[n-j], idx[i] + rn := make(digits, r) + for i, v := range idx[:r] { + rn[i] = d[v] + } + select { + case <-done: + close(res) + return + case res <- rn: + } + break + } + if i == 0 { + close(res) + return + } + } + } + close(res) + }() + return res, func() { + close(done) + } +} -- cgit From 2b1cc6e13bda42ad4aab75a822317e6de553e508 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 28 Nov 2023 21:01:49 +1100 Subject: pwc245 solution - updated task2 for go/python to refect the change in requirement from @digits to @ints --- challenge-245/pokgopun/go/ch-2.go | 45 ++++++++++++++++++++--------------- challenge-245/pokgopun/python/ch-2.py | 39 ++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go index 74527e790b..b707bf3662 100644 --- a/challenge-245/pokgopun/go/ch-2.go +++ b/challenge-245/pokgopun/go/ch-2.go @@ -14,19 +14,19 @@ Submitted by: [46]Mohammad S Anwar Example 1 -Input: @digits = (8, 1, 9) +Input: @ints = (8, 1, 9) Output: 981 981 % 3 == 0 Example 2 -Input: @digits = (8, 6, 7, 1, 0) +Input: @ints = (8, 6, 7, 1, 0) Output: 8760 Example 3 -Input: @digits = (1) +Input: @ints = (1) Output: -1 __________________________________________________________________ @@ -48,20 +48,26 @@ import ( func main() { for _, data := range []struct { - input digits + input ints output int }{ - {digits{8, 1, 9}, 981}, - {digits{8, 6, 7, 1, 0}, 8760}, - {digits{1}, -1}, + {ints{8, 1, 9}, 981}, + {ints{8, 6, 7, 1, 0}, 8760}, + {ints{1}, -1}, + {ints{0, 0, 0}, 0}, + {ints{4, 8, 911}, 9114}, + {ints{8, 85, 0}, 8850}, + {ints{8, 89, 2}, 8982}, + {ints{8, 76, 0}, 8760}, + {ints{8, 94, 0}, 9480}, } { fmt.Println(reflect.DeepEqual(data.output, data.input.lot())) } } -type digits []int +type ints []int -func (d digits) concat() int { +func (d ints) concat() int { r := d[0] var n, t int for _, v := range d[1:] { @@ -75,26 +81,27 @@ func (d digits) concat() int { return r } -func (d digits) lot() int { - slices.Sort(d) - slices.Reverse(d) +func (d ints) lot() int { + max := -1 l := len(d) for i := l; i > 0; i-- { res, closer := d.permute(i) for v := range res { r := v.concat() if r%3 == 0 { - closer() - return r + if r > max { + max = r + } } } + closer() } - return -1 + return max } // transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations -func (d digits) permute(r int) (res chan digits, closer func()) { - res = make(chan digits) +func (d ints) permute(r int) (res chan ints, closer func()) { + res = make(chan ints) done := make(chan struct{}) n := len(d) idx := make([]int, n) @@ -106,7 +113,7 @@ func (d digits) permute(r int) (res chan digits, closer func()) { cyc[i] = n - i } go func() { - r0 := make(digits, r) + r0 := make(ints, r) copy(r0, d[:r]) select { case <-done: @@ -124,7 +131,7 @@ func (d digits) permute(r int) (res chan digits, closer func()) { } else { j := cyc[i] idx[i], idx[n-j] = idx[n-j], idx[i] - rn := make(digits, r) + rn := make(ints, r) for i, v := range idx[:r] { rn[i] = d[v] } diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py index d689e65c04..8256bbcfa6 100644 --- a/challenge-245/pokgopun/python/ch-2.py +++ b/challenge-245/pokgopun/python/ch-2.py @@ -14,19 +14,19 @@ Submitted by: [46]Mohammad S Anwar Example 1 -Input: @digits = (8, 1, 9) +Input: @ints = (8, 1, 9) Output: 981 981 % 3 == 0 Example 2 -Input: @digits = (8, 6, 7, 1, 0) +Input: @ints = (8, 6, 7, 1, 0) Output: 8760 Example 3 -Input: @digits = (1) +Input: @ints = (1) Output: -1 __________________________________________________________________ @@ -38,22 +38,37 @@ SO WHAT DO YOU THINK ? """ ### solution by pokgopun@gmail.com -from itertools import permutations +from itertools import permutations, chain def lot(tup: tuple): - for n in range(len(tup),0,-1): - for x in permutations( - sorted(tup,reverse=True), n - ): - y = int( - "".join( str(e) for e in x ) + return max( + chain.from_iterable( + ( + ( i for i in + ( + int("".join(x)) for x in + ( + (str(e) for e in y) for y in + chain.from_iterable( + permutations(tup, n) for n in range(1,len(tup)+1) + ) + ) + ) if i % 3 == 0 + ), + (-1,), ) - if y % 3 == 0: return y - return -1 + ) + ) for inpt, otpt in { (8, 1, 9): 981, (8, 6, 7, 1, 0): 8760, (1,): -1, + (0,0,0): 0, + (4, 8, 911): 9114, + (8, 85, 0): 8850, + (8, 89, 2): 8982, + (8, 76, 0): 8760, + (8, 94, 0): 9480, }.items(): print(otpt==lot(inpt)) -- cgit From f5a9f5585542ee68199517ca9bdf474223c4b1dc Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Tue, 28 Nov 2023 11:27:29 +0100 Subject: PWC-245: Perl ch-1 --- challenge-245/spadacciniweb/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 challenge-245/spadacciniweb/perl/ch-1.pl diff --git a/challenge-245/spadacciniweb/perl/ch-1.pl b/challenge-245/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..358cd2004a --- /dev/null +++ b/challenge-245/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Task 1: Sort Language +# Submitted by: Mohammad S Anwar +# +# You are given two array of languages and its popularity. +# Write a script to sort the language based on popularity. +# +# Example 1 +# Input: @lang = ('perl', 'c', 'python') +# @popularity = (2, 1, 3) +# Output: ('c', 'perl', 'python') +# +# Example 2 +# Input: @lang = ('c++', 'haskell', 'java') +# @popularity = (1, 3, 2) +# Output: ('c++', 'java', 'haskell') + + +use strict; +use warnings; +use Data::Dumper; + + +my @lang = ('perl', 'c', 'python'); +my @popularity = (2, 1, 3); +order_lang(\@lang, \@popularity); + +@lang = ('c++', 'haskell', 'java'); +@popularity = (1, 3, 2); +order_lang(\@lang, \@popularity); + +@lang = ('c++', 'haskell', 'java', 'perl', 'c', 'python'); +@popularity = (1, 3, 2, 2, 1, 3); +order_lang(\@lang, \@popularity); + +exit 0; + +sub order_lang { + my $lang = shift; + my $popularity = shift; + + my %lang; + foreach my $i (0..$#lang) { + push @{ $lang{ $popularity[$i] } }, $lang[$i]; + } + + printf "language based on popularity: (%s)\n", + join ', ', map { join ', ', @{ $lang{$_} } } + sort { $a <=> $b } keys %lang; +} -- cgit From c21050f1f14eda7b8dca16bdeb8186cb65255260 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 28 Nov 2023 22:45:43 +1100 Subject: pwc245_task2 - add comments and minor modification to apply the modulas propert:"If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N )" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenge-245/pokgopun/go/ch-2.go | 12 ++++++++++-- challenge-245/pokgopun/python/ch-2.py | 18 +++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go index b707bf3662..fafc8e1a09 100644 --- a/challenge-245/pokgopun/go/ch-2.go +++ b/challenge-245/pokgopun/go/ch-2.go @@ -67,6 +67,14 @@ func main() { type ints []int +func (d ints) sum() int { + s := 0 + for _, v := range d { + s += v + } + return s +} + func (d ints) concat() int { r := d[0] var n, t int @@ -87,8 +95,8 @@ func (d ints) lot() int { for i := l; i > 0; i-- { res, closer := d.permute(i) for v := range res { - r := v.concat() - if r%3 == 0 { + if v.sum()%3 == 0 { // If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N ), we can filter lots of them out before concat + r := v.concat() if r > max { max = r } diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py index 8256bbcfa6..362cb4b6d9 100644 --- a/challenge-245/pokgopun/python/ch-2.py +++ b/challenge-245/pokgopun/python/ch-2.py @@ -40,22 +40,26 @@ SO WHAT DO YOU THINK ? from itertools import permutations, chain +### this function looks a bit messy as it tries to operate on iterators alone to saving memory resources, avoiding storing any of them to intermediate list/tuple def lot(tup: tuple): - return max( + return max( ### max() can avoid an error from null permute iterator as it operates on chain.from_iterables that include default (-1,) chain.from_iterable( ( ( i for i in ( - int("".join(x)) for x in + int("".join(j)) for j in ### concat and convert int strings to an int ( - (str(e) for e in y) for y in - chain.from_iterable( - permutations(tup, n) for n in range(1,len(tup)+1) + ( str(z) for z in y) for y in ### int-to-string conversion for ints so we can further concat and convert them to an int + ( + x for x in + chain.from_iterable( ### need permutations(tup,n) loop instead of permutations(tup) alone to avoid a bug in itertools/permutations + permutations(tup, n) for n in range(1,len(tup)+1) + ) if sum(x) % 3 == 0 ### If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N ) ) ) - ) if i % 3 == 0 + ) ), - (-1,), + (-1,), ### add (-1,) to chain.from_iterable to avoid max() error from null iterator from permutations (i.e. when nothing qualifies sum % 3 == 0 ) ) ) -- cgit From b60cb77fe463f9c97a8170868a2ff874554d5f41 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 28 Nov 2023 11:53:44 +0000 Subject: - Added solutions by PokGoPun. - Added solutions by Luca Ferrari. - Added solutions by Humberto Massa. - Added solutions by Niels van Dijke. - Added solutions by Peter Campbell Smith. - Added solutions by Dave Jacoby. - Added solutions by Thomas Kohler. - Added solutions by Peter Meszaros. - Added solutions by E. Choroba. - Added solutions by David Ferrone. - Added solutions by W. Luis Mochan. - Added solutions by Mariano Spadaccini. --- challenge-245/clifton-wood/raku/ch-1.sh | 1 + challenge-245/clifton-wood/raku/ch-2.sh | 1 + challenge-245/eric-cheung/python/ch-1.py | 12 + challenge-245/eric-cheung/python/ch-2.py | 8 + challenge-245/perlboy1967/perl/ch-1.pl | 39 + challenge-245/perlboy1967/perl/ch-2.pl | 49 + challenge-245/perlboy1967/perl/ch1.pl | 39 - challenge-245/perlboy1967/perl/ch2.pl | 49 - challenge-245/ulrich-rieke/cpp/ch-1.cpp | 47 + challenge-245/ulrich-rieke/haskell/ch-1.hs | 15 + challenge-245/ulrich-rieke/haskell/ch-2.hs | 30 + challenge-245/ulrich-rieke/perl/ch-1.pl | 20 + challenge-245/ulrich-rieke/perl/ch-2.pl | 57 + challenge-245/ulrich-rieke/raku/ch-1.raku | 15 + challenge-245/ulrich-rieke/raku/ch-2.raku | 55 + challenge-245/ulrich-rieke/rust/ch-1.rs | 25 + challenge-245/ulrich-rieke/rust/ch-2.rs | 45 + challenge-245/ziameraj16/java/SortLanguage.java | 17 + stats/pwc-challenge-244.json | 715 ++++++++++ stats/pwc-current.json | 578 +-------- stats/pwc-language-breakdown-summary.json | 74 +- stats/pwc-language-breakdown.json | 1575 ++++++++++++----------- stats/pwc-leaders.json | 416 +++--- stats/pwc-summary-1-30.json | 40 +- stats/pwc-summary-121-150.json | 50 +- stats/pwc-summary-151-180.json | 46 +- stats/pwc-summary-181-210.json | 22 +- stats/pwc-summary-211-240.json | 58 +- stats/pwc-summary-241-270.json | 32 +- stats/pwc-summary-271-300.json | 116 +- stats/pwc-summary-301-330.json | 20 +- stats/pwc-summary-31-60.json | 34 +- stats/pwc-summary-61-90.json | 112 +- stats/pwc-summary-91-120.json | 108 +- stats/pwc-summary.json | 68 +- 35 files changed, 2611 insertions(+), 1977 deletions(-) create mode 100644 challenge-245/clifton-wood/raku/ch-1.sh create mode 100644 challenge-245/clifton-wood/raku/ch-2.sh create mode 100755 challenge-245/eric-cheung/python/ch-1.py create mode 100755 challenge-245/eric-cheung/python/ch-2.py create mode 100755 challenge-245/perlboy1967/perl/ch-1.pl create mode 100755 challenge-245/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-245/perlboy1967/perl/ch1.pl