diff options
85 files changed, 5684 insertions, 1773 deletions
diff --git a/challenge-012/massa/README b/challenge-012/massa/README new file mode 100644 index 0000000000..a7c2685bb7 --- /dev/null +++ b/challenge-012/massa/README @@ -0,0 +1 @@ +Solution by Massa 👽 Humberto diff --git a/challenge-012/massa/raku/ch-1.raku b/challenge-012/massa/raku/ch-1.raku new file mode 100644 index 0000000000..433e7afe08 --- /dev/null +++ b/challenge-012/massa/raku/ch-1.raku @@ -0,0 +1,48 @@ +#! /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: Euclid Number + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +The numbers formed by adding one to the products of the smallest primes are +called the Euclid Numbers (see wiki). Write a script that finds the smallest +Euclid Number that is not prime. This challenge was proposed by Laurent +Rosenfeld.. + +=head3 Result + + Output: 30031 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION() { + ^∞ ==> grep &is-prime ==> produce &[*] ==> map {$_+1} ==> first {!.is-prime} +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ output => 30031 }, + ; + + SOLUTION().&is: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-012/massa/raku/ch-2.raku b/challenge-012/massa/raku/ch-2.raku new file mode 100644 index 0000000000..8e8fc20f7a --- /dev/null +++ b/challenge-012/massa/raku/ch-2.raku @@ -0,0 +1,60 @@ +#! /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: Merge items + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given two 2-D array of positive integers, $items1 and $items2 where +element is pair of (item_id, item_quantity). + +Write a script to return the merged items. + +=head3 Example 1: + + Input: «/a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e» + Output: "/a/b" + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub SOLUTION(*@paths is copy) { + my @parts = map { last unless [eq] $_[]; $_[0] }, [Z] (@paths ==> map {$*SPEC.splitdir: .IO}); + $*SPEC.catdir: @parts +} + +multi MAIN (Bool :$test!) { + use Testo; + + my @tests = + %{ input => «/a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e», + output => "/a/b" }, + %{ input => « + /etc/apt/listchanges.conf.d /etc/apt/sources.list /etc/apt/sources.list.d + /etc/apt/sources.list.dpkg-backup /etc/apt/sources.list.save + /etc/apt/sources.list~ /etc/apt/apt.conf.d/20listchanges + /etc/apt/sources.list.d/google-chrome.list + /etc/apt/sources.list.d/google-chrome.list.save + /etc/apt/sources.list.d/nala-sources.list /etc/apt/sources.list.d/nala.list + /etc/apt/sources.list.d/steam.list.save », + output => "/etc/apt" }, + ; + + SOLUTION(|.<input>).&is: .<output>, .<text> for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-263/ash/perl/ch-2.pl b/challenge-263/ash/perl/ch-2.pl new file mode 100644 index 0000000000..82804b51e6 --- /dev/null +++ b/challenge-263/ash/perl/ch-2.pl @@ -0,0 +1,31 @@ +# Solution to Task 2 of The Weekly Challenge 263 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK2 + +# $ perl ch-2.pl +# $VAR1 = [ +# [ +# 1, +# 4 +# ], +# [ +# 2, +# 3 +# ], +# [ +# 3, +# 2 +# ] +# ]; + +use v5.20; +use Data::Dumper; + +my $items1 = [[1, 1], [2, 1], [3, 2]]; +my $items2 = [[2, 2], [1, 3]]; + +my %values; +$values{$_->[0]} += $_->[1] for @$items1, @$items2; + +my @result = map {[$_ + 0, $values{$_}]} sort {$a <=> $b} keys %values; + +say Dumper(\@result); diff --git a/challenge-263/ash/raku/ch-2.raku b/challenge-263/ash/raku/ch-2.raku new file mode 100644 index 0000000000..1df1d470c0 --- /dev/null +++ b/challenge-263/ash/raku/ch-2.raku @@ -0,0 +1,16 @@ +# Solution to Task 2 of The Weekly Challenge 263 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK2 + +# $ raku ch-2.raku +# [(1 4) (2 3) (3 2)] + +my @items1 = [[1, 1], [2, 1], [3, 2]]; +my @items2 = [[2, 2], [1, 3]]; + +my %values; +for (@items1, @items2).flat -> ($k, $v) { + %values{$k} += $v; +} + +my @result = (%values.keys.sort: {$^a <=> $^b}).map: {$_, %values{$_}}; +say @result; diff --git a/challenge-263/athanasius/perl/ch-1.pl b/challenge-263/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..b14c9f23a2 --- /dev/null +++ b/challenge-263/athanasius/perl/ch-1.pl @@ -0,0 +1,189 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 263 +========================= + +TASK #1 +------- +*Target Index* + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and a target element $k. + +Write a script to return the list of indices in the sorted array where the +element is same as the given target element. + +Example 1 + + Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 + Output: (1, 2) + + Sorted array: (1, 2, 2, 3, 4, 5) + Target indices: (1, 2) as $ints[1] = 2 and $k[2] = 2 + +Example 2 + + Input: @ints = (1, 2, 4, 3, 5), $k = 6 + Output: () + + No element in the given array matching the given target. + +Example 3 + + Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 + Output: (4) + + Sorted array: (1, 2, 2, 3, 4, 5) + Target index: (4) as $ints[4] = 4 + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Interface +--------- +1. If no command-line arguments are given, the test suite is run. Otherwise: +2. The divisor $k is given on the command-line as a named argument, followed by + a (possibly empty) unnamed list of integers. +3. If any integer in the list (i.e., following $k) on the command-line is + negative, the first such integer must be preceded by "--" to indicate that + what follows does not contain command-line flags. + +=cut +#=============================================================================== + +use v5.32.1; # Enables strictures +use warnings; +use Const::Fast; +use Getopt::Long; +use Regexp::Common qw( number ); +use Test::More; + +const my $USAGE => <<END; +Usage: + perl $0 [-k[=Int]] [<ints> ...] + perl $0 + + -k[=Int] The target element + [<ints> ...] A list of integers +END + +#------------------------------------------------------------------------------- +BEGIN +#------------------------------------------------------------------------------- +{ + $| = 1; + print "\nChallenge 263, Task #1: Target Index (Perl)\n\n"; +} + +#=============================================================================== +MAIN: +#=============================================================================== +{ + if (scalar @ARGV == 0) + { + run_tests(); + } + else + { + my ($ints, $k) = parse_command_line(); + + printf "Input: \@ints = (%s), \$k = %d\n", join( ', ', @$ints ), $k; + + my $indices = find_target_indices( $ints, $k ); + + printf "Output: (%s)\n", join ', ', @$indices; + } +} + +#------------------------------------------------------------------------------- +sub find_target_indices +#------------------------------------------------------------------------------- +{ + my ($ints, $k) = @_; + my @sorted = sort { $a <=> $b } @$ints; + my @indices; + + for my $i (0 .. $#sorted) + { + push @indices, $i if $sorted[ $i ] == $k; + } + + return \@indices; +} + +#------------------------------------------------------------------------------- +sub parse_command_line +#------------------------------------------------------------------------------- +{ + my $k; + + GetOptions + ( + 'k=i' => \$k, + ) or error( 'Invalid command line argument(s)' ); + + defined $k or error( '$k is missing' ); + + for (@ARGV, $k) + { + / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] ); + } + + return (\@ARGV, $k); +} + +#------------------------------------------------------------------------------- +sub run_tests +#------------------------------------------------------------------------------- +{ + print "Running the test suite\n"; + + while (my $line = <DATA>) + { + chomp $line; + + my ($test_name, $ints_str, $k, $expected_str) = split / \| /x, $line; + + for ($test_name, $ints_str, $k, $expected_str) + { + s/ ^ \s+ //x; + s/ \s+ $ //x; + } + + my @ints = split / \s+ /x, $ints_str; + my @expected = split / \s+ /x, $expected_str; + my $indices = find_target_indices( \@ints, $k ); + + is_deeply $indices, \@expected, $test_name; + } + + done_testing; +} + +#------------------------------------------------------------------------------- +sub error +#------------------------------------------------------------------------------- +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +################################################################################ + +__DATA__ +Example 1| 1 5 3 2 4 2| 2|1 2 +Example 2| 1 2 4 3 5 | 6| +Example 3| 5 3 2 4 2 1| 4|4 +Negatives|-1 -2 -3 0 4 -3|-3|0 1 diff --git a/challenge-263/athanasius/perl/ch-2.pl b/challenge-263/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..356dcfca17 --- /dev/null +++ b/challenge-263/athanasius/perl/ch-2.pl @@ -0,0 +1,240 @@ +#!perl + +################################################################################ +=comment + +Perl Weekly Challenge 263 +========================= + +TASK #2 +------- +*Merge Items* + +Submitted by: Mohammad Sajid Anwar + +You are given two 2-D array of positive integers, $items1 and $items2 where +element is pair of (item_id, item_quantity). + +Write a script to return the merged items. + +Example 1 + + Input: $items1 = [ [1,1], [2,1], [3,2] ] + $items2 = [ [2,2], [1,3] ] + Output: [ [1,4], [2,3], [3,2] ] + + Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) + Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) + Item id (3) appears 1 time: [3,2] + +Example 2 + + Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] + $items2 = [ [3,1], [1,3] ] + Output: [ [1,8], [2,3], [3,3] ] + +Example 3 + + Input: $items1 = [ [1,1], [2,2], [3,3] ] + $items2 = [ [2,3], [2,4] ] + Output: [ [1,1], [2,9], [3,3] ] + +=cut +################################################################################ + +#--------------------------------------# +# Copyright © 2024 PerlMonk Athanasius # +#--------------------------------------# + +#=============================================================================== +=comment + +Assumption +---------- +The "positive" integers include zero. + +Interface +--------- |
