From 880fa7e6b98f0cdf298655b839026b0de6d0a317 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 19 May 2025 08:50:34 +0200 Subject: Solutions Week 322 in Raku by @ash --- challenge-322/ash/raku/ch-1.raku | 14 ++++++++++++++ challenge-322/ash/raku/ch-2.raku | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 challenge-322/ash/raku/ch-1.raku create mode 100644 challenge-322/ash/raku/ch-2.raku diff --git a/challenge-322/ash/raku/ch-1.raku b/challenge-322/ash/raku/ch-1.raku new file mode 100644 index 0000000000..a54b060bfe --- /dev/null +++ b/challenge-322/ash/raku/ch-1.raku @@ -0,0 +1,14 @@ +# Task 1 of the Weekly Challenge 322 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/#TASK1 + +say split-groups('ABC-D-E-F', 3); # ABC-DEF +say split-groups('A-BC-D-E', 2); # A-BC-DE +say split-groups('-A-B-CD-E', 4); # A-BCDE + +sub split-groups($str is copy, $i) { + $str ~~ s:g/\W//; + + $str ~~ s/ (\w) (\w**{$i} ('-' | $)) /$0-$1/ while $str ~~ /^\w**{$i + 1}/; + + return $str; +} diff --git a/challenge-322/ash/raku/ch-2.raku b/challenge-322/ash/raku/ch-2.raku new file mode 100644 index 0000000000..5d15aee2f6 --- /dev/null +++ b/challenge-322/ash/raku/ch-2.raku @@ -0,0 +1,12 @@ +# Task 2 of the Weekly Challenge 322 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/#TASK2 + +say rank 55, 22, 44, 33; # (4 1 3 2) +say rank 10, 10, 10; # (1 1 1) +say rank 5, 1, 1, 4, 3; # (4, 1, 1, 3, 2) + +sub rank(*@ints) { + my %rank = @ints.sort.unique.kv.reverse; + + return @ints.map({%rank{$_} + 1}); +} -- cgit From c9942108fa3fe7a7449691f81e12c69ffc3b1520 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 19 May 2025 08:59:47 +0200 Subject: Add solutions to 322: String Format & Rank Array by E. Choroba --- challenge-322/e-choroba/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-322/e-choroba/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 challenge-322/e-choroba/perl/ch-1.pl create mode 100755 challenge-322/e-choroba/perl/ch-2.pl diff --git a/challenge-322/e-choroba/perl/ch-1.pl b/challenge-322/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..78137a1355 --- /dev/null +++ b/challenge-322/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub string_format($str, $i) { + $str =~ tr/-//d; + my $mod = length($str) % $i; + $str =~ s/(?<=^.{$mod})/-/ if $mod && $mod != length $str; + + $str =~ /-/g; # Set pos to the dash (if any). + return $str =~ s/\G(.{$i})(?=.)/$1-/rg; # Separate groups of size $i. +} + +use Test::More tests => 3 + 2; + +is string_format('ABC-D-E-F', 3), 'ABC-DEF', 'Example 1'; +is string_format('A-BC-D-E', 2), 'A-BC-DE', 'Example 2'; +is string_format('-A-B-CD-E', 4), 'A-BCDE', 'Example 3'; + +is string_format('A-B', 15), 'AB', 'Single group'; +is string_format("", 2), "", 'Empty string'; diff --git a/challenge-322/e-choroba/perl/ch-2.pl b/challenge-322/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..3ee6ffa9cc --- /dev/null +++ b/challenge-322/e-choroba/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ uniq }; + +sub rank_array(@ints) { + my @uniq = sort { $a <=> $b } uniq(@ints); + my %rank; + @rank{@uniq} = 1 .. @uniq; + return @rank{@ints} +} + +use Test2::V0; +plan(3); + +is [rank_array(55, 22, 44, 33)], [4, 1, 3, 2], 'Example 1'; +is [rank_array(10, 10, 10)], [1, 1, 1], 'Example 2'; +is [rank_array(5, 1, 1, 4, 3)], [4, 1, 1, 3, 2], 'Example 3'; -- cgit From 0af1650632b5c41634fddec6d29bf87731adc85e Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 May 2025 07:46:45 +0000 Subject: w322 - Task 1 & 2 --- challenge-322/perlboy1967/perl/ch1.pl | 46 +++++++++++++++++++++++++++++++++++ challenge-322/perlboy1967/perl/ch2.pl | 39 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 challenge-322/perlboy1967/perl/ch1.pl create mode 100755 challenge-322/perlboy1967/perl/ch2.pl diff --git a/challenge-322/perlboy1967/perl/ch1.pl b/challenge-322/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..d945105fd2 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch1.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: String Format +Submitted by: Mohammad Sajid Anwar + +You are given a string and a positive integer. + +Write a script to format the string, removing any dashes, in groups of +size given by the integer. The first group can be smaller than the integer +but should have at least one character. Groups should be separated by dashes. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +sub stringFormat ($str,$len) { + my @r; + + # Get first part + $str =~ s/([^-]{1,$len})//; + push(@r,$1); + + # Get the rest + $str =~ s/-//g; + 1 while (push(@r,substr($str,0,$len,'')) && $str); + + return join('-',@r); +} + +is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); +is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); +is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); +is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..5c24353d26 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Rank Array +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return an array of the ranks of each element: the lowest +value has rank 1, next lowest rank 2, etc. If two elements are the same +then they share the same rank. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::MoreUtils qw(uniq); + +sub rankArray (@ints) { + my ($i,%idx) = (1); + $idx{$_} = $i++ for uniq sort @ints; + return map { $idx{$_} } @ints; +} + +is([rankArray(55,22,44,33)],[4,1,3,2],'Example 1'); +is([rankArray(10,10,10)],[1,1,1],'Example 2'); +is([rankArray(5,1,1,4,3)],[4,1,1,3,2],'Example 3'); + +done_testing; -- cgit From aba6b7ef791aa95ffe5fea741a6e49aa970fd633 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 May 2025 07:50:07 +0000 Subject: Task 2 - add "<=>" sorting --- challenge-322/perlboy1967/perl/ch2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl index 5c24353d26..5e46d4cf12 100755 --- a/challenge-322/perlboy1967/perl/ch2.pl +++ b/challenge-322/perlboy1967/perl/ch2.pl @@ -28,7 +28,7 @@ use List::MoreUtils qw(uniq); sub rankArray (@ints) { my ($i,%idx) = (1); - $idx{$_} = $i++ for uniq sort @ints; + $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; return map { $idx{$_} } @ints; } -- cgit From f46dbe5fc9ae4aaa455c80857a7b684bbd1cd4ae Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 19 May 2025 16:08:27 +0800 Subject: challenge 322, raku solutions --- challenge-322/feng-chang/raku/ch-1.raku | 5 +++++ challenge-322/feng-chang/raku/ch-2.raku | 6 ++++++ challenge-322/feng-chang/raku/test.raku | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100755 challenge-322/feng-chang/raku/ch-1.raku create mode 100755 challenge-322/feng-chang/raku/ch-2.raku create mode 100755 challenge-322/feng-chang/raku/test.raku diff --git a/challenge-322/feng-chang/raku/ch-1.raku b/challenge-322/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..7d7e7d5457 --- /dev/null +++ b/challenge-322/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s, UInt:D $size); + +put $s.comb.grep(* ne '-').reverse.rotor($size, :partial).map(*.reverse.join).reverse.join('-'); diff --git a/challenge-322/feng-chang/raku/ch-2.raku b/challenge-322/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..536708961e --- /dev/null +++ b/challenge-322/feng-chang/raku/ch-2.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my @ints_ = @ints.sort.unique; +put @ints.map({ @ints_.first($_, :k) + 1 }); diff --git a/challenge-322/feng-chang/raku/test.raku b/challenge-322/feng-chang/raku/test.raku new file mode 100755 index 0000000000..98ba491db7 --- /dev/null +++ b/challenge-322/feng-chang/raku/test.raku @@ -0,0 +1,24 @@ +#!/bin/env raku + +# The Weekly Challenge 322 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, String Format +pwc-test './ch-1.raku', 'ABC-D-E-F', 3, 'ABC-DEF', 'String Format: $str="ABC-D-E-F",$i=3 => ABC-DEF'; +pwc-test './ch-1.raku', 'A-BC-D-E', 2, 'A-BC-DE', 'String Format: $str="A-BC-D-E", $i=2 => A-BC-DE'; +pwc-test './ch-1.raku', '--', '-A-B-CD-E', 4, 'A-BCDE', 'String Format: $str="-A-B-CD-E",$i=4 => A-BCDE'; + +# Task 2, Rank Array +pwc-test './ch-2.raku', <55 22 44 33>, '4 1 3 2', 'Rank Array: (55,22,44,33) => (4,1,3,2)'; +pwc-test './ch-2.raku', <10 10 10>, '1 1 1', 'Rank Array: (10,10,10) => (1,1,1)'; +pwc-test './ch-2.raku', <5 1 1 4 3>, '4 1 1 3 2', 'Rank Array: (5,1,1,4,3) => (4,1,1,3,2)'; -- cgit From 5293de05c015da9c14ae1f3542ac271b34db81fa Mon Sep 17 00:00:00 2001 From: Matthias Muth Date: Mon, 19 May 2025 10:29:55 +0200 Subject: Challenge 321 Task 1 and 2 solutions in Perl by Matthias Muth - Update --- challenge-321/matthias-muth/README.md | 83 +++++++++++++++++++++++++++----- challenge-321/matthias-muth/perl/ch-2.pl | 14 +++--- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/challenge-321/matthias-muth/README.md b/challenge-321/matthias-muth/README.md index 45ded22b4b..a00f1d2dc0 100644 --- a/challenge-321/matthias-muth/README.md +++ b/challenge-321/matthias-muth/README.md @@ -54,20 +54,33 @@ I think that the easiest way to do this is to first sort the array numerically: @nums = sort { $a <=> $b } @nums; ``` -Then, we have the minimum in the first entry, and the maximum in the last one.
-It's easy to get and remove those two from the array at the same time: we can use `shift` to get and remove the first one (the minimum) , and `pop` to do the same for the last one (the maximum).
+Then, we have the minimum in the first entry +and the maximum in the last one.
+It's easy to get and remove those two from the array at the same time: +we can use `shift` to get and remove the first one (the minimum), +and `pop` to do the same for the last one (the maximum).
So the average is this: ```perl ( shift( @nums ) + pop( @nums ) ) / 2 ``` -We will be doing this in a loop, as long as we still have at least two numbers (for the average) in the array. +We will be doing this in a loop, +as long as we still have at least two numbers +(for calculating their average) in the array. But how do we count the *distinct* averages? -Someone [said](https://perldoc.perl.org/perlfaq4#How-can-I-remove-duplicate-elements-from-a-list-or-array?) 'When you think the words "unique" or "duplicated", think "hash keys"'.
"Unique" and "distinct" are very often used interchangeably, even though they don't mean exactly the same.
-But in any case, a hash helps us to find the number of *distinct* values of averages:
Whenever we have computed an average value, we create a hash entry with that value as a key, for example by assigning a value of `1` to it: +Someone +[said](https://perldoc.perl.org/perlfaq4#How-can-I-remove-duplicate-elements-from-a-list-or-array?) +'When you think the words "unique" or "duplicated", think "hash keys"'.
+"Unique" and "distinct" are very often used interchangeably, +even though they don't mean exactly the same thing.
+But no matter what, +a hash helps us to find the number of *distinct* values of averages:
+Whenever we have computed an average value, +we create a hash entry with that value as a key, +for example by assigning a value of `1` to it: ```perl my %distinct_values; @@ -76,9 +89,11 @@ But in any case, a hash helps us to find the number of *distinct* values of aver } ``` -When we are done, the number of keys in the hash is the number of distinct values we are looking for. We can get the number of keys by using the hash in scalar context. +When we are done, +the number of keys in the hash is the number of distinct values we are looking for. +We can get the number of keys by using the hash in scalar context. -Then this is a possible solution: +Which makes this my solution: ```perl use v5.36; @@ -128,11 +143,19 @@ sub distinct_average( @nums ) { Tricky! -Treating the `#` character as 'backspace' means that the character preceding the `#` as well as the `#` itself can be removed from the string. +Treating the `#` character as 'backspace' means that the character preceding the `#` as well as the `#` itself can be removed from the string. -My first solution, just doing a global regex substitution (`s/.\#//g`) for both strings, did not work.
The reason is in Example 2 (`"ab##"`):
-The `b#` will be found and removed, and what is left is `"a#"`, so we should expect to remove that `a#` as well.
-But the position at which the regex looks for the next occurrence of the `/.\#/` pattern is where the `b#` was found, which is _behind_ the `a`.
That's why the `a#` will *not* be removed. +My first solution, +just doing a global regex substitution (`s/.\#//g`) for both strings, +did not work.
+The reason is in Example 2 (`"ab##"`):
+The `b#` will be found and removed, +and what is left is `"a#"`, +so we should expect to remove that `a#` as well.
+But the position at which the regex looks for the next +occurrence of the `/.\#/` pattern is where the `b#` was found, +which is _behind_ the `a`.
+That's why the `a#` will *not* be removed. The solution is to *repeat* the substitution, until we don't find any `/.\#/` anymore. @@ -140,7 +163,7 @@ We can use the substitution itself as the controlling expression of a `while` lo We apply that substitution loop to both strings, then we return the result of the comparison of the processed strings. -In the end it looks like this: +In the end it looks like this: ```perl sub backspace_compare( $str1, $str2 ) { @@ -151,4 +174,40 @@ sub backspace_compare( $str1, $str2 ) { } ``` +**UPDATE:** + +Niels van Dijke's +[posted solution](https://www.facebook.com/groups/theweeklychallengegroup/permalink/1357372518846815/), +using `s/[^#]#//` for the substitution, +made me think what can happen with the more simple `s/.#//g` in my solution +(still using it repeatedly).
+And in fact my solution failed for an example like `"abc###"`: +it would remove the `c#` and then the `##` in the first go, +leaving `"ab"`, and ending the loop. + +I also learned that it's not necessary to escape the `#` in a regular expression +(at least as long as we don't use `/x`). + +So it has to be either this: +```perl + do {} while s/.#//; # No '/g'. +``` +which restarts the search from the beginning after everysubstitution, +or this, using Niels' pattern: +```perl + do {} while s/[^#]#//g; +``` +to keep the 'mini-optimization' of using `/g` +to at least replace all possible non-overlapping sequences before restarting.
+And that's my updated solution: + +```perl +sub backspace_compare( $str1, $str2 ) { + for ( $str1, $str2 ) { + do {} while s/[^#]#//g; + } + return $str1 eq $str2; +} +``` + #### **Thank you for the challenge!** diff --git a/challenge-321/matthias-muth/perl/ch-2.pl b/challenge-321/matthias-muth/perl/ch-2.pl index b1a77bd11b..bb0502f8cd 100755 --- a/challenge-321/matthias-muth/perl/ch-2.pl +++ b/challenge-321/matthias-muth/perl/ch-2.pl @@ -10,9 +10,9 @@ use v5.36; -sub backspace_compare( $str1, $str2 ) { +sub backspace_compare_1( $str1, $str2 ) { for ( $str1, $str2 ) { - while ( s/.\#//g ) { + while ( s/[^#]#//g ) { # Everything is in the loop condition. } } @@ -21,14 +21,14 @@ sub backspace_compare( $str1, $str2 ) { sub backspace_compare( $str1, $str2 ) { for ( $str1, $str2 ) { - do {} while s/.\#//g; + do {} while s/[^#]#//g; } return $str1 eq $str2; } -sub backspace_compare( $str1, $str2 ) { - do {} while $str1 =~ s/.\#//g; - do {} while $str2 =~ s/.\#//g; +sub backspace_compare_compact( $str1, $str2 ) { + do {} while $str1 =~ s/[^#]#//g; + do {} while $str2 =~ s/[^#]#//g; return $str1 eq $str2; } @@ -40,5 +40,7 @@ is backspace_compare( "ab##", "a#b#" ), T, 'Example 2: backspace_compare( "ab##", "a#b#" ) is true'; is backspace_compare( "a#b", "c" ), F, 'Example 3: backspace_compare( "a#b", "c" ) is false'; +is backspace_compare( "abc###", "" ), T, + 'Added Test 1: backspace_compare( "abc###", "" ) is true'; done_testing; -- cgit From 6fee2ba14176b54a3109202a1334dac2ea3b49d6 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 19 May 2025 17:13:45 +0800 Subject: fix --- challenge-322/feng-chang/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-322/feng-chang/raku/ch-1.raku b/challenge-322/feng-chang/raku/ch-1.raku index 7d7e7d5457..4f8f275faa 100755 --- a/challenge-322/feng-chang/raku/ch-1.raku +++ b/challenge-322/feng-chang/raku/ch-1.raku @@ -2,4 +2,4 @@ unit sub MAIN(Str:D $s, UInt:D $size); -put $s.comb.grep(* ne '-').reverse.rotor($size, :partial).map(*.reverse.join).reverse.join('-'); +put $s.comb(/<-[-]>/).reverse.rotor($size, :partial).map(*.reverse.join).reverse.join('-'); -- cgit From e69cf5c18c93538495bd4f040febc0f0fe9e3440 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 19 May 2025 10:16:48 +0000 Subject: Challenge 322 Solutions (Raku) --- challenge-322/mark-anderson/raku/ch-1.raku | 21 +++++++++++++++++++++ challenge-322/mark-anderson/raku/ch-2.raku | 14 ++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-322/mark-anderson/raku/ch-1.raku create mode 100644 challenge-322/mark-anderson/raku/ch-2.raku diff --git a/challenge-322/mark-anderson/raku/ch-1.raku b/challenge-322/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..d40f05735a --- /dev/null +++ b/challenge-322/mark-anderson/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku +use Test; + +is format-string("ABC-D-E-F", 3), "ABC-DEF"; +is format-string("A-BC-D-E", 2), "A-BC-DE"; +is format-string("-A-B-CD-E", 4), "A-BCDE"; + +sub format-string($str is copy, $int) +{ + $str ~~ s:g/"-"//; + my $m = "." x ($str.chars mod $int or $int); + my $i = "." x $int; + S:g/> (<$i>)/-$0/ given $str +} + +sub format-string-v2($str, $int) +{ + my @chars = $str.comb(//); + my $list = flat (@chars mod $int or Empty), ($int, $int...*); + @chars.rotor($list)>>.join.join("-") +} diff --git a/challenge-322/mark-anderson/raku/ch-2.raku b/challenge-322/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..b0df999fce --- /dev/null +++ b/challenge-322/mark-anderson/raku/ch-2.raku @@ -0,0 +1,14 @@ +#/usr/bin/env raku +use Test; + +is-deeply rank-array(55,22,44,33), (4,1,3,2); +is-deeply rank-array(10,10,10), (1,1,1); +is-deeply rank-array(5,1,1,4,3), (4,1,1,3,2); + +sub rank-array(+@ints) +{ + .{@ints} given [-Inf, |@ints].unique + .sort + .antipairs + .Map +} -- cgit From b2c1df23b6752082dc150b267b7c32942cab289b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 May 2025 13:40:54 +0200 Subject: PWC 322 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 PL/Java done Task 2 PL/Java done Task 1 Python done Task 2 Python done --- challenge-322/luca-ferrari/blog-1.txt | 1 + challenge-322/luca-ferrari/blog-10.txt | 1 + challenge-322/luca-ferrari/blog-2.txt | 1 + challenge-322/luca-ferrari/blog-3.txt | 1 + challenge-322/luca-ferrari/blog-4.txt | 1 + challenge-322/luca-ferrari/blog-5.txt | 1 + challenge-322/luca-ferrari/blog-6.txt | 1 + challenge-322/luca-ferrari/blog-7.txt | 1 + challenge-322/luca-ferrari/blog-8.txt | 1 + challenge-322/luca-ferrari/blog-9.txt | 1 + challenge-322/luca-ferrari/pljava/pom.xml | 6 +- .../luca-ferrari/pljava/src/main/java/Task1.java | 77 ++++++++++++++++++++ .../luca-ferrari/pljava/src/main/java/Task2.java | 81 ++++++++++++++++++++++ challenge-322/luca-ferrari/plperl/ch-1.plperl | 27 ++++++++ challenge-322/luca-ferrari/plperl/ch-2.plperl | 33 +++++++++ challenge-322/luca-ferrari/plpgsql/ch-1.sql | 30 ++++++++ challenge-322/luca-ferrari/plpgsql/ch-2.sql | 34 +++++++++ challenge-322/luca-ferrari/python/ch-1.py | 39 +++++++++++ challenge-322/luca-ferrari/python/ch-2.py | 34 +++++++++ challenge-322/luca-ferrari/raku/ch-1.raku | 12 ++++ challenge-322/luca-ferrari/raku/ch-2.raku | 17 +++++ 21 files changed, 397 insertions(+), 3 deletions(-) create mode 100644 challenge-322/luca-ferrari/blog-1.txt create mode 100644 challenge-322/luca-ferrari/blog-10.txt create mode 100644 challenge-322/luca-ferrari/blog-2.txt create mode 100644 challenge-322/luca-ferrari/blog-3.txt create mode 100644 challenge-322/luca-ferrari/blog-4.txt create mode 100644 challenge-322/luca-ferrari/blog-5.txt create mode 100644 challenge-322/luca-ferrari/blog-6.txt create mode 100644 challenge-322/luca-ferrari/blog-7.txt create mode 100644 challenge-322/luca-ferrari/blog-8.txt create mode 100644 challenge-322/luca-ferrari/blog-9.txt create mode 100644 challenge-322/luca-ferrari/pljava/src/main/java/Task1.java create mode 100644 challenge-322/luca-ferrari/pljava/src/main/java/Task2.java create mode 100644 challenge-322/luca-ferrari/plperl/ch-1.plperl create mode 100644 challenge-322/luca-ferrari/plperl/ch-2.plperl create mode 100644 challenge-322/luca-ferrari/plpgsql/ch-1.sql create mode 100644 challenge-322/luca-ferrari/plpgsql/ch-2.sql create mode 100644 challenge-322/luca-ferrari/python/ch-1.py create mode 100644 challenge-322/luca-ferrari/python/ch-2.py create mode 100644 challenge-322/luca-ferrari/raku/ch-1.raku create mode 100644 challenge-322/luca-ferrari/raku/ch-2.raku diff --git a/challenge-322/luca-ferrari/blog-1.txt b/challenge-322/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..18b8dc411f --- /dev/null +++ b/challenge-322/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1 diff --git a/challenge-322/luca-ferrari/blog-10.txt b/challenge-322/luca-ferrari/blog-10.txt new file mode 100644 index 0000000000..1a42faab32 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-10.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2pljava diff --git a/challenge-322/luca-ferrari/blog-2.txt b/challenge-322/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..3614d5d753 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2 diff --git a/challenge-322/luca-ferrari/blog-3.txt b/challenge-322/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..95f69cfb89 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1plperl diff --git a/challenge-322/luca-ferrari/blog-4.txt b/challenge-322/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..584e3c712a --- /dev/null +++ b/challenge-322/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2plperl diff --git a/challenge-322/luca-ferrari/blog-5.txt b/challenge-322/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..02f83e5adb --- /dev/null +++ b/challenge-322/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1plpgsql diff --git a/challenge-322/luca-ferrari/blog-6.txt b/challenge-322/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..8aaf0598a9 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2plpgsql diff --git a/challenge-322/luca-ferrari/blog-7.txt b/challenge-322/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..c3cc83cf7b --- /dev/null +++ b/challenge-322/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1python diff --git a/challenge-322/luca-ferrari/blog-8.txt b/challenge-322/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..f4247582c1 --- /dev/null +++ b/challenge-322/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task2python diff --git a/challenge-322/luca-ferrari/blog-9.txt b/challenge-322/luca-ferrari/blog-9.txt new file mode 100644 index 0000000000..d59d53b97c --- /dev/null +++ b/challenge-322/luca-ferrari/blog-9.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2025/05/19/PerlWeeklyChallenge322.html#task1pljava diff --git a/challenge-322/luca-ferrari/pljava/pom.xml b/challenge-322/luca-ferrari/pljava/pom.xml index 3a9f67f2be..8afd8c47a8 100644 --- a/challenge-322/luca-ferrari/pljava/pom.xml +++ b/challenge-322/luca-ferrari/pljava/pom.xml @@ -7,14 +7,14 @@ PWC - PWC321 + PWC322 1 - Perl Weekly Challenge 321 with package PWC321 - Implementation of the tasks in PL/Java for PWC 321 + Perl Weekly Challenge 322 with package PWC322 + Implementation of the tasks in PL/Java for PWC 322 US-ASCII diff --git a/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java new file mode 100644 index 0000000000..c2d3ed7dcb --- /dev/null +++ b/challenge-322/luca-ferrari/pljava/src/main/java/Task1.java @@ -0,0 +1,77 @@ + + + +package PWC322; + +/** + * PL/Java implementation for PWC 322 + * 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/PWC322-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', true ); + select sqlj.set_classpath( 'public', 'PWC322' ); + + select pwc322.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', 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 Task1 { + + private final static Logger logger = Logger.getAnonymousLogger(); + + @Function( schema = "pwc322", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static final String task1_pljava( String string, int size ) throws SQLException { + logger.log( Level.INFO, "Entering pwc322.task1_pljava" ); + + List pieces = new LinkedList(); + + for ( String s : string.split( "-" ) ) { + for ( String q : s.split( "" ) ) + pieces.add( q ); + } + + String result = ""; + + for ( int i = 0; i < pieces.size(); ) { + for ( int j = 0; j < size; j++ ) { + if ( i >= pieces.size() ) + break; + + result += pieces.get( i++ ); + } + + if ( i < pieces.size() ) + result += "-"; + } + + return result; + } +} diff --git a/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java new file mode 100644 index 0000000000..b237865f31 --- /dev/null +++ b/challenge-322/luca-ferrari/pljava/src/main/java/Task2.java @@ -0,0 +1,81 @@ + + + +package PWC322; + +/** + * PL/Java implementation for PWC 322 + * 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/PWC322-1.jar luca@rachel:/tmp + + + * To install into PostgreSQL execute: + + select sqlj.install_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', true ); + select sqlj.set_classpath( 'public', 'PWC322' ); + + select pwc322.task2_pljava(); + + and then to redeploy: + + select sqlj.replace_jar( 'file:///tmp/PWC322-1.jar', 'PWC322', 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 = "pwc322", + onNullInput = RETURNS_NULL, + effects = IMMUTABLE ) + public static int[] task2_pljava( int[] numbers ) throws SQLException { + logger.log( Level.INFO, "Entering pwc322.task2_pljava" ); + + List sorted = new LinkedList(); + + for ( int i : numbers ) + sorted.add( i ); + + Collections.sort( sorted ); + + int index = 1; + int c = 0; + int[] result = new int[ numbers.length ]; + + for ( int current : numbers ) { + index = 1; + + for ( int seeking : sorted ) { + if ( seeking == current ) + break; + else + index++; + } + + result[ c ] = index; + c++; + } + + return result; + } +} diff --git a/challenge-322/luca-ferrari/plperl/ch-1.plperl b/challenge-322/luca-ferrari/plperl/ch-1.plperl new file mode 100644 index 0000000000..5b9c2f024b --- /dev/null +++ b/challenge-322/luca-ferrari/plperl/ch-1.plperl @@ -0,0 +1,27 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task1_plperl( text, int) +RETURNS text +AS $CODE$ + + my ( $string, $size ) = @_; + die "Size should be less then the length of the string" if ( length( $string ) < $size ); + + my @chars = split //, reverse join( '', split( '-', $string ) ); + my $string; + while ( @chars ) { + $string .= shift @chars for 1 .. $size; + $string .= '-' if ( @chars ); + } + + return reverse $string; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-322/luca-ferrari/plperl/ch-2.plperl b/challenge-322/luca-ferrari/plperl/ch-2.plperl new file mode 100644 index 0000000000..af0df79895 --- /dev/null +++ b/challenge-322/luca-ferrari/plperl/ch-2.plperl @@ -0,0 +1,33 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task2_plperl( int[] ) +RETURNS SETOF int +AS $CODE$ + + my ( $numbers ) = @_; + + my @result; + + for my $current ( $numbers->@* ) { + my $index = 1; + for ( sort { $a <=> $b } $numbers->@* ) { + if ( $_ == $current ) { + push @result, $index; + last; + } + + $index++; + } + } + + return [ @result ]; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-322/luca-ferrari/plpgsql/ch-1.sql b/challenge-322/luca-ferrari/plpgsql/ch-1.sql new file mode 100644 index 0000000000..815feb0ef2 --- /dev/null +++ b/challenge-322/luca-ferrari/plpgsql/ch-1.sql @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task1_plpgsql( s text, l int ) +RETURNS text +AS $CODE$ +DECLARE + q_match text; + q_result text; +BEGIN + q_match := '(\w{' || l || '})'; + + q_match := $$ SELECT reverse( regexp_replace( reverse( regexp_replace( '$$ || s || $$', '-', '', 'g' ) ), '$$ || q_match || $$', '\1-', 'g' ) )$$; + + raise info '[%]', q_match; + EXECUTE q_match + INTO q_result; + + + RETURN q_result; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-322/luca-ferrari/plpgsql/ch-2.sql b/challenge-322/luca-ferrari/plpgsql/ch-2.sql new file mode 100644 index 0000000000..fb7f7558b9 --- /dev/null +++ b/challenge-322/luca-ferrari/plpgsql/ch-2.sql @@ -0,0 +1,34 @@ +-- +-- Perl Weekly Challenge 322 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc322; + +CREATE OR REPLACE FUNCTION +pwc322.task2_plpgsql( n int[] ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current int; + r int; +BEGIN + + FOR current IN SELECT x FROM unnest( n ) x LOOP + WITH sorting AS ( + SELECT row_number() over ( order by y ) as sorted, y + FROM unnest( n ) y + ) + SELECT sorted + INTO r + FROM sorting + WHERE y = current; + + RETURN NEXT r; + + END LOOP; + +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-322/luca-ferrari/python/ch-1.py b/challenge-322/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..22e45f3117 --- /dev/null +++ b/challenge-322/luca-ferrari/python/ch-1.py @@ -0,0 +1,39 @@ +#!python + +# +# Perl Weekly Challenge 322 +# Task 1 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_1( args ): + string = args[ 0 ] + size = int( args[ 1 ] ) + chars = [] + result = "" + + for c in string[::-1] : + if c == '-': + continue + + chars.append( c ) + + counter = 1 + for c in chars: + result += c + counter += 1 + + if counter % size == 0: + result += '-' + + return result[::-1] + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_1( sys.argv[ 1: ] ) ) diff --git a/challenge-322/luca-ferrari/python/ch-2.py b/challenge-322/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..5915cc602b --- /dev/null +++ b/challenge-322/luca-ferrari/python/ch-2.py @@ -0,0 +1,34 @@ +#!python + +# +# Perl Weekly Challenge 322 +# Task 2 +# +# See +# + +import sys + +# task implementation +# the return value will be printed +def task_2( args ): + numbers = list( map( int, args ) ) + sorted_numbers = list( map( int, args ) ) + sorted_numbers.sort() + result = [] + + for c in numbers: + index = 1 + for x in sorted_numbers: + if x == c: + result.append( index ) + else: + index += 1 + + + return result + + +# invoke the main without the command itself +if __name__ == '__main__': + print( task_2( sys.argv[ 1: ] ) ) diff --git a/challenge-322/luca-ferrari/raku/ch-1.raku b/challenge-322/luca-ferrari/raku/ch-1.raku new file mode 100644 index 0000000000..f18e4e7323 --- /dev/null +++ b/challenge-322/luca-ferrari/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!raku + +# +# Perl Weekly Challenge 322 +# Task 1 +# +# See +# + +sub MAIN( Str $string, Int $size where { $size <= $string.chars } ) { + $string.split( '-' ).join.flip.comb( :skip-empty ).rotor( $size, :partial ).map( *.join ).join( '-' ).flip.say; +} diff --git a/challenge-322/luca-ferrari/raku/ch-2.raku b/challenge-322/luca-ferrari/raku/ch-2.raku new file mode 100644 index 0000000000..8b2ab701b0 --- /dev/null +++ b/challenge-322/luca-ferrari/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!raku + +# +# Perl Weekly Challenge 322 +# Task 2 +# +# See +# + +sub MAIN( *@numbers where { @numbers.grep( * ~~ Int ).elems == @numbers.elems } ) { + my @result; + for @numbers -> $current { + @result.push: @numbers.sort.grep( * ~~ $current, :k ).first + 1; + } + + @result.say; +} -- cgit From 8490e84f6f6ad3a7c47c3dd80239dd8852577ac5 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 19 May 2025 09:46:00 -0400 Subject: Week 322 --- challenge-322/zapwai/perl/ch-1.pl | 24 +++++++++++++++++++++++ challenge-322/zapwai/perl/ch-2.pl | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-322/zapwai/perl/ch-1.pl create mode 100644 challenge-322/zapwai/perl/ch-2.pl diff --git a/challenge-322/zapwai/perl/ch-1.pl b/challenge-322/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..50be8f0a37 --- /dev/null +++ b/challenge-322/zapwai/perl/ch-1.pl @@ -0,0 +1,24 @@ +use feature qw( say signatures ); +sub proc($str, $i) { + say "Input: \$str = $str, \$i = $i"; + my @letters = grep { /\w/ } split '', $str; + my $rem = @letters % $i; + my $q = int( @letters / $i ); + my $output; + for (1 .. $rem) { + $output .= $letters[$_-1] ; + } + for my $j (0 .. $q - 1) { + $output .= '-' unless ($rem == 0 && $j == 0); + for my $k (0 .. $i - 1) { + $output .= $letters[$i*$j + $k + $rem]; + } + } + say "Output: $output"; +} +my $str = "ABC-D-E-F"; my $i = 3; +proc($str, $i); +$str = "A-BC-D-E"; $i = 2; +proc($str, $i); +$str = "-A-B-CD-E"; $i = 4; +proc($str, $i); diff --git a/challenge-322/zapwai/perl/ch-2.pl b/challenge-322/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..08e3ec7fc9 --- /dev/null +++ b/challenge-322/zapwai/perl/ch-2.pl @@ -0,0 +1,40 @@ +use v5.38; + +# When a rank is matched we shift the remaining ranks down +sub shift_ranks($r, $i) { + for my $j ($i .. $#$r) { + $$r[$j]--; + } +} + +sub proc(@ints) { + say "Input: \@ints = @ints"; + my @o = sort {$a <=> $b} @ints; + my @r = (1 .. @o); + for my $i (0 .. $#o - 1) { + if ($o[$i+1] == $o[$i]) { + $r[$i+1] = $r[$i]; + unless ($i+2 > $#o) { + if ($r[$i+2] - $r[$i+1] > 1) { + shift_ranks(\@r, $i + 2); + } + } + } + } + my @out; + for my $int (@ints) { + for my $i (0 .. $#o) { + if ($o[$i] == $int) { + push @out, $r[$i]; + last; + } + } + } + say "Output: @out"; +} +my @ints = (55, 22, 44, 33); +proc(@ints); +@ints = (10, 10, 10); +proc(@ints); +@ints = (5, 1, 1, 4, 3); +proc(@ints); -- cgit From 174dc182320b7103cc835481f798c0284af14d81 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 19 May 2025 15:43:03 +0000 Subject: Challenge 322 Solutions (Raku) --- challenge-322/mark-anderson/raku/ch-1.raku | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/challenge-322/mark-anderson/raku/ch-1.raku b/challenge-322/mark-anderson/raku/ch-1.raku index d40f05735a..d5fe719531 100644 --- a/challenge-322/mark-anderson/raku/ch-1.raku +++ b/challenge-322/mark-anderson/raku/ch-1.raku @@ -5,7 +5,22 @@ is format-string("ABC-D-E-F", 3), "ABC-DEF"; is format-string("A-BC-D-E", 2), "A-BC-DE"; is format-string("-A-B-CD-E", 4), "A-BCDE"; -sub format-string($str is copy, $int) +sub format-string($str, $int) +{ + # This is the version I like but it doesn't always work. + # The problem is with the {$int}. If it's hardcoded with + # the appropriate int then it works. Not sure what I'm + # doing wrong. 🤔 🤷 + + $str ~~ /^ + ( \-? \-? ) ** {0..$int} + ([ \-? \-? ] ** {$int} )* + $/; + + (flat $/[0]>>.join, $/[1]>>>>.join).join("-") +} + +sub format-string-v2($str is copy, $int) { $str ~~ s:g/"-"//; my $m = "." x ($str.chars mod $int or $int); @@ -13,7 +28,7 @@ sub format-string($str is copy, $int) S:g/> (<$i>)/-$0/ given $str } -sub format-string-v2($str, $int) +sub format-string-v3($str, $int) { my @chars = $str.comb(//); my $list = flat (@chars mod $int or Empty), ($int, $int...*); -- cgit From 032834a88dcad0af8ec93dbcfafc068e4a192864 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 19 May 2025 17:13:51 +0100 Subject: add solutions week 322 in python --- challenge-322/steven-wilson/python/ch-1.py | 28 ++++++++++++++++++++++++++++ challenge-322/steven-wilson/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-322/steven-wilson/python/ch-1.py create mode 100644 challenge-322/steven-wilson/python/ch-2.py diff --git a/challenge-322/steven-wilson/python/ch-1.py b/challenge-322/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..1aa1d9b9c7 --- /dev/null +++ b/challenge-322/steven-wilson/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +from more_itertools import chunked + + +def string_format(string, size): + """ Given a string and a positive integer, format the string, removing any + dashes, in groups of size given by the integer. The first group can be + smaller than the integer but should have at least one character. Groups + should be separated by dashes. + + >>> string_format("ABC-D-E-F", 3) + 'ABC-DEF' + >>> string_format("A-BC-D-E", 2) + 'A-BC-DE' + >>> string_format("-A-B-CD-E", 4) + 'A-BCDE' + """ + characters = [c for c in reversed(string) if c.isalpha()] + chunks = chunked(characters, size) + grouped = ["".join(a) for a in chunks] + return "-".join(grouped)[::-1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-322/steven-wilson/python/ch-2.py b/challenge-322/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..bd0b969657 --- /dev/null +++ b/challenge-322/steven-wilson/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + + +def rank_array(integers): + """ Given an array of integers, return an array of the ranks of each + element: the lowest value has rank 1, next lowest rank 2, etc. If two + elements are the same then they share the same rank. + + >>> rank_array([55, 22, 44, 33]) + (4, 1, 3, 2) + >>> rank_array([10, 10, 10]) + (1, 1, 1) + >>> rank_array([5, 1, 1, 4, 3]) + (4, 1, 1, 3, 2) + """ + integers_sorted = sorted(integers) + rank_dict = {} + rank = 1 + for i in integers_sorted: + if i not in rank_dict: + rank_dict[i] = rank + rank += 1 + return tuple(rank_dict[i] for i in integers) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit From f785ffd9a3b62b8374bcf39ada3291d9f5d54247 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 19 May 2025 17:23:37 +0100 Subject: small improvement --- challenge-322/steven-wilson/python/ch-2.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/challenge-322/steven-wilson/python/ch-2.py b/challenge-322/steven-wilson/python/ch-2.py index bd0b969657..6cc71ebada 100644 --- a/challenge-322/steven-wilson/python/ch-2.py +++ b/challenge-322/steven-wilson/python/ch-2.py @@ -13,13 +13,10 @@ def rank_array(integers): >>> rank_array([5, 1, 1, 4, 3]) (4, 1, 1, 3, 2) """ - integers_sorted = sorted(integers) + integers_sorted = sorted(set(integers)) rank_dict = {} - rank = 1 - for i in integers_sorted: - if i not in rank_dict: - rank_dict[i] = rank - rank += 1 + for n, i in enumerate(integers_sorted, start=1): + rank_dict[i] = n return tuple(rank_dict[i] for i in integers) -- cgit From c85410fc5c5ae9e6795c08154b0add2a3f4f4847 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 19 May 2025 10:32:56 -0600 Subject: Solve PWC322 --- challenge-322/wlmb/blog.txt | 1 + challenge-322/wlmb/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-322/wlmb/perl/ch-2.pl | 15 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 challenge-322/wlmb/blog.txt create mode 100755 challenge-322/wlmb/perl/ch-1.pl create mode 100755 challenge-322/wlmb/perl/ch-2.pl diff --git a/challenge-322/wlmb/blog.txt b/challenge-322/wlmb/blog.txt new file mode 100644 index 0000000000..fa97454e49 --- /dev/null +++ b/challenge-322/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2025/05/19/PWC322/ diff --git a/challenge-322/wlmb/perl/ch-1.pl b/challenge-322/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..863511df78 --- /dev/null +++ b/challenge-322/wlmb/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +# Perl weekly challenge 322 +# Task 1: String Format +# +# See https://wlmb.github.io/2025/05/19/PWC322/#task-1-string-format +use v5.36; +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 S1 I1 S2 I2... + to edit string Sn making dahsed joined substrings of size not larger than In + by removing dashes from the original string. + FIN +for my($string, $size)(@ARGV){ + my $processing=""; + my @edited; + for(reverse split "-",$string){ + unshift(@edited, $processing), $processing="" if(length($processing)+length)>$size; + $processing = $_.$processing; + } + say "$string, $size -> ", join "-", $processing, @edited; +} diff --git a/challenge-322/wlmb/perl/ch-2.pl b/challenge-322/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..f53115ba56 --- /dev/null +++ b/challenge-322/wlmb/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +# Perl weekly challenge 322 +# Task 2: Rank Array +# +# See https://wlmb.github.io/2025/05/19/PWC322/#task-2-rank-array +use v5.36; +use List::Util qw(uniq); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 N2... + to find the ranks Ri of the numbers Ni + FIN +my %rank; +my $current=0; +$rank{$_}=++$current for sort {$a<=>$b} uniq @ARGV; +say "@ARGV -> @rank{@ARGV}"; -- cgit From f2d24f326f6f2fe80abd077de489f47eae549ca3 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 19 May 2025 11:32:40 -0600 Subject: Add alternative interpretation --- challenge-322/wlmb/perl/ch-1b.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 challenge-322/wlmb/perl/ch-1b.pl diff --git a/challenge-322/wlmb/perl/ch-1b.pl b/challenge-322/wlmb/perl/ch-1b.pl new file mode 100755 index 0000000000..c76911faba --- /dev/null +++ b/challenge-322/wlmb/perl/ch-1b.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 322 +# Task 1: String Format +# +# See https://wlmb.github.io/2025/05/19/PWC322/#task-1-string-format +use v5.36; +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 S1 I1 S2 I2... + to edit string Sn making dashed joined substrings of size In + ignoring any dashes in the original string. + FIN +for my($string, $size)(@ARGV){ + my $reversed=reverse $string; # reverse + $reversed=~tr/-//d; # remove all dashes + $reversed=~s/(.{$size})/$1-/g; # add a dash every three chars + $reversed=~s/-$//; # remove final dash if any + my $out = reverse $reversed; + say"$string, size -> $out" +} -- cgit From 799aaa577a09c9a4f2d0271c5497c6ed0a839095 Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Mon, 19 May 2025 16:05:33 -0400 Subject: challenge 322 idiomatic perl solutions by ysth --- challenge-322/ysth/perl/README.md | 1 + challenge-322/ysth/perl/ch-1.pl | 6 ++++++ challenge-322/ysth/perl/ch-2.pl | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 challenge-322/ysth/perl/README.md create mode 100644 challenge-322/ysth/perl/ch-1.pl create mode 100644 challenge-322/ysth/perl/ch-2.pl diff --git a/challenge-322/ysth/perl/README.md b/challenge-322/ysth/perl/README.md new file mode 100644 index 0000000000..60d86e8401 --- /dev/null +++ b/challenge-322/ysth/perl/README.md @@ -0,0 +1 @@ +# Solutions by Yitzchak Scott-Thoennes diff --git a/challenge-322/ysth/perl/ch-1.pl b/challenge-322/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..2857d95de6 --- /dev/null +++ b/challenge-322/ysth/perl/ch-1.pl @@ -0,0 +1,6 @@ +use 5.036; + +my ($string, $group_size) = @ARGV; + +# yes, you can do this without the double reverse, but not as efficiently +say scalar reverse((reverse $string =~ y/-//dr) =~ s/.{$group_size}\K(?!\z)/-/sgr) diff --git a/challenge-322/ysth/perl/ch-2.pl b/challenge-322/ysth/perl/ch-2.pl new file mode 100644 index 0000000000..306e14aaeb --- /dev/null +++ b/challenge-322/ysth/perl/ch-2.pl @@ -0,0 +1,7 @@ +use 5.036; +use List::Util 'uniq'; + +my @integers = @ARGV; + +my @ordered = sort { $a <=> $b } uniq @integers; +say join ' ', { map(($ordered[$_-1], $_), 1..@ordered) }->@{@integers}; -- cgit From 68f9929d37899a229ef9a247770e4032899e6639 Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Mon, 19 May 2025 18:00:02 -0400 Subject: challenge 322 perlish python solutions by ysth --- challenge-322/ysth/python/ch-1.py | 18 ++++++++++++++++++ challenge-322/ysth/python/ch-2.py | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 challenge-322/ysth/python/ch-1.py create mode 100644 challenge-322/ysth/python/ch-2.py diff --git a/challenge-322/ysth/python/ch-1.py b/challenge-322/ysth/python/ch-1.py new file mode 100644 index 0000000000..739058f480 --- /dev/null +++ b/challenge-322/ysth/python/ch-1.py @@ -0,0 +1,18 @@ +import itertools +import sys + +string = sys.argv[1] +group_size = int(sys.argv[2]) + +print( + "-".join( + reversed( + [ + "".join(reversed(group)) + for group in itertools.batched( + reversed(string.replace("-", "")), group_size + ) + ] + ) + ) +) diff --git a/challenge-322/ysth/python/ch-2.py b/challenge-322/ysth/python/ch-2.py new file mode 100644 index 0000000000..75301f87a5 --- /dev/null +++ b/challenge-322/ysth/python/ch-2.py @@ -0,0 +1,17 @@ +import sys + +integers = [int(value) for value in sys.argv[1:]] + +print( + " ".join( + list( + map( + { + value: str(i + 1) + for i, value in enumerate(sorted(set(integers))) + }.__getitem__, + integers, + ) + ) + ) +) -- cgit From be4730a3a5f224a5f1c6fa57f72fbfa65f1b21e5 Mon Sep 17 00:00:00 2001 From: Conor Hoekstra Date: Mon, 19 May 2025 20:29:25 -0400 Subject: :sparkles: Week 322 in BQN --- challenge-322/conor-hoekstra/ch-1.bqn | 14 ++++++++++++++ challenge-322/conor-hoekstra/ch-2.bqn | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-322/conor-hoekstra/ch-1.bqn create mode 100644 challenge-322/conor-hoekstra/ch-2.bqn diff --git a/challenge-322/conor-hoekstra/ch-1.bqn b/challenge-322/conor-hoekstra/ch-1.bqn new file mode 100644 index 0000000000..3af00f74a2 --- /dev/null +++ b/challenge-322/conor-hoekstra/ch-1.bqn @@ -0,0 +1,14 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/322-1.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" +s ⇐ •Import "/home/cph/bqn-code/lib/string.bqn" +fn ⇐ •Import "/home/cph/bqn-code/lib/fun.bqn" + +Tf ← '-'s.Join fn.Chunk⟜∾ # tail format +Sf ← { n𝕊x:{ n>≠⊑𝕩 ? (⊑𝕩)∾"-"∾n Tf 1↓𝕩 ; n Tf 𝕩 } '-'s.Split x } # string format + +# Tests +u.UnitTest (3 Sf "ABC-D-E-F") ≡ "ABC-DEF" +u.UnitTest (2 Sf "A-BC-D-E") ≡ "A-BC-DE" +u.UnitTest (4 Sf "-A-B-CD-E") ≡ "A-BCDE" diff --git a/challenge-322/conor-hoekstra/ch-2.bqn b/challenge-322/conor-hoekstra/ch-2.bqn new file mode 100644 index 0000000000..b981bdfb46 --- /dev/null +++ b/challenge-322/conor-hoekstra/ch-2.bqn @@ -0,0 +1,15 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/322-2.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +RankArray ← { 1+𝕩⊐˜∧⍷𝕩 } # explicit +RankArray2 ← 1+∧∘⍷⊐⊢ # tacit + +# Tests +u.UnitTest (RankArray 55‿22‿44‿33) ≡ 4‿1‿3‿2 +u.UnitTest (RankArray 10‿10‿10) ≡ 1‿1‿1 +u.UnitTest (RankArray 5‿1‿1‿4‿3) ≡ 4‿1‿1‿3‿2 +u.UnitTest (RankArray2 55‿22‿44‿33) ≡ 4‿1‿3‿2 +u.UnitTest (RankArray2 10‿10‿10) ≡ 1‿1‿1 +u.UnitTest (RankArray2 5‿1‿1‿4‿3) ≡ 4‿1‿1‿3‿2 -- cgit From 16dce2351be4f85df717afaae3b5353f5aa6be68 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Tue, 20 May 2025 10:14:19 +0800 Subject: add ch-2a.raku --- challenge-322/feng-chang/raku/ch-2a.raku | 5 +++++ challenge-322/feng-chang/raku/test.raku | 4 ++++ 2 files changed, 9 insertions(+) create mode 100755 challenge-322/feng-chang/raku/ch-2a.raku diff --git a/challenge-322/feng-chang/raku/ch-2a.raku b/challenge-322/feng-chang/raku/ch-2a.raku new file mode 100755 index 0000000000..fb6f231ab3 --- /dev/null +++ b/challenge-322/feng-chang/raku/ch-2a.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +put @ints.sort.unique.antipairs.Hash{ @ints } »+» 1; diff --git a/challenge-322/feng-chang/raku/test.raku b/challenge-322/feng-chang/raku/test.raku index 98ba491db7..feb9a3d165 100755 --- a/challenge-322/feng-chang/raku/test.raku +++ b/challenge-322/feng-chang/raku/test.raku @@ -22,3 +22,7 @@ pwc-test './ch-1.raku', '--', '-A-B-CD-E', 4, 'A-BCDE', 'String Format: $str="- pwc-test './ch-2.raku', <55 22 44 33>, '4 1 3 2', 'Rank Array: (55,22,44,33) => (4,1,3,2)'; pwc-test './ch-2.raku', <10 10 10>, '1 1 1', 'Rank Array: (10,10,10) => (1,1,1)'; pwc-test './ch-2.raku', <5 1 1 4 3>, '4 1 1 3 2', 'Rank Array: (5,1,1,4,3) => (4,1,1,3,2)'; + +pwc-test './ch-2a.raku', <55 22 44 33>, '4 1 3 2', 'Rank Array: (55,22,44,33) => (4,1,3,2)'; +pwc-test './ch-2a.raku', <10 10 10>, '1 1 1', 'Rank Array: (10,10,10) => (1,1,1)'; +pwc-test './ch-2a.raku', <5 1 1 4 3>, '4 1 1 3 2', 'Rank Array: (5,1,1,4,3) => (4,1,1,3,2)'; -- cgit From e51ae043517be5d93aa80deb9e8e67bf599ceecc Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 20 May 2025 09:21:46 +0100 Subject: - Added solutions by Eric Cheung. - Added solutions by Ulrich Rieke. - Added solutions by Andrew Shitov. - Added solutions by E. Choroba. - Added solutions by Niels van Dijke. - Added solutions by Feng Chang. - Added solutions by Matthias Muth. - Added solutions by Mark Anderson. - Added solutions by Luca Ferrari. - Added solutions by David Ferrone. - Added solutions by Steven Wilson. - Added solutions by Conor Hoekstra. - Added solutions by W. Luis Mochan. - Added solutions by Yitzchak Scott-Thoennes. --- challenge-322/eric-cheung/python/ch-1.py | 28 ++ challenge-322/eric-cheung/python/ch-2.py | 12 + challenge-322/perlboy1967/perl/ch-1.pl | 46 +++ challenge-322/perlboy1967/perl/ch-2.pl | 39 ++ challenge-322/perlboy1967/perl/ch1.pl | 46 --- challenge-322/perlboy1967/perl/ch2.pl | 39 -- challenge-322/ulrich-rieke/cpp/ch-1.cpp | 44 +++ challenge-322/ulrich-rieke/cpp/ch-2.cpp | 44 +++ challenge-322/ulrich-rieke/haskell/ch-1.hs | 20 + challenge-322/ulrich-rieke/haskell/ch-2.hs | 17 + challenge-322/ulrich-rieke/perl/ch-1.pl | 27 ++ challenge-322/ulrich-rieke/perl/ch-2.pl | 19 + challenge-322/ulrich-rieke/raku/ch-1.raku | 23 ++ challenge-322/ulrich-rieke/raku/ch-2.raku | 12 + challenge-322/ulrich-rieke/rust/ch-1.rs | 27 ++ challenge-322/ulrich-rieke/rust/ch-2.rs | 20 + members.json | 1 + stats/pwc-challenge-321.json | 567 +++++++++++++++++++++++++++++ stats/pwc-current.json | 378 +------------------ stats/pwc-language-breakdown-2019.json | 2 +- stats/pwc-language-breakdown-2020.json | 2 +- stats/pwc-language-breakdown-2021.json | 2 +- stats/pwc-language-breakdown-2022.json | 2 +- stats/pwc-language-breakdown-2023.json | 2 +- stats/pwc-language-breakdown-2024.json | 2 +- stats/pwc-language-breakdown-2025.json | 25 +- stats/pwc-language-breakdown-summary.json | 8 +- stats/pwc-leaders.json | 64 ++-- stats/pwc-summary-1-30.json | 4 +- stats/pwc-summary-121-150.json | 2 +- stats/pwc-summary-151-180.json | 8 +- stats/pwc-summary-181-210.json | 4 +- stats/pwc-summary-211-240.json | 2 +- stats/pwc-summary-241-270.json | 2 +- stats/pwc-summary-271-300.json | 2 +- stats/pwc-summary-301-330.json | 14 +- stats/pwc-summary-31-60.json | 2 +- stats/pwc-summary-61-90.json | 8 +- stats/pwc-summary-91-120.json | 2 +- stats/pwc-summary.json | 30 +- stats/pwc-yearly-language-summary.json | 10 +- 41 files changed, 1064 insertions(+), 544 deletions(-) create mode 100755 challenge-322/eric-cheung/python/ch-1.py create mode 100755 challenge-322/eric-cheung/python/ch-2.py create mode 100755 challenge-322/perlboy1967/perl/ch-1.pl create mode 100755 challenge-322/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-322/perlboy1967/perl/ch1.pl delete mode 100755 challenge-322/perlboy1967/perl/ch2.pl create mode 100755 challenge-322/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-322/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-322/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-322/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-322/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-322/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-322/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-322/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-322/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-322/ulrich-rieke/rust/ch-2.rs create mode 100644 stats/pwc-challenge-321.json diff --git a/challenge-322/eric-cheung/python/ch-1.py b/challenge-322/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..ca4df0cc2d --- /dev/null +++ b/challenge-322/eric-cheung/python/ch-1.py @@ -0,0 +1,28 @@ + +## Example 1 +## strInput = "ABC-D-E-F" +## nInput = 3 + +## Example 2 +## strInput = "A-BC-D-E" +## nInput = 2 + +## Example 3 +strInput = "-A-B-CD-E" +nInput = 4 + +arrSplit = strInput.split("-") + +arrOutput = [] +strTemp = arrSplit[-1] + +for strLoop in arrSplit[:-1][::-1]: + if len(strTemp) == nInput: + arrOutput.insert(0, strTemp) + strTemp = strLoop + else: + strTemp = strLoop + strTemp + +arrOutput.insert(0, strTemp) + +print ("-".join(arrOutput)) diff --git a/challenge-322/eric-cheung/python/ch-2.py b/challenge-322/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..bc975d09a7 --- /dev/null +++ b/challenge-322/eric-cheung/python/ch-2.py @@ -0,0 +1,12 @@ + +## arrInts = [55, 22, 44, 33] ## Example 1 +## arrInts = [10, 10, 10] ## Example 2 +arrInts = [5, 1, 1, 4, 3] ## Example 3 + +arrSet = sorted(set(arrInts)) + +## print (arrSet) + +arrOutput = [arrSet.index(nLoop) + 1 for nLoop in arrInts] + +print (arrOutput) diff --git a/challenge-322/perlboy1967/perl/ch-1.pl b/challenge-322/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..d945105fd2 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: String Format +Submitted by: Mohammad Sajid Anwar + +You are given a string and a positive integer. + +Write a script to format the string, removing any dashes, in groups of +size given by the integer. The first group can be smaller than the integer +but should have at least one character. Groups should be separated by dashes. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +sub stringFormat ($str,$len) { + my @r; + + # Get first part + $str =~ s/([^-]{1,$len})//; + push(@r,$1); + + # Get the rest + $str =~ s/-//g; + 1 while (push(@r,substr($str,0,$len,'')) && $str); + + return join('-',@r); +} + +is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); +is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); +is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); +is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch-2.pl b/challenge-322/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..5e46d4cf12 --- /dev/null +++ b/challenge-322/perlboy1967/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 322 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Rank Array +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return an array of the ranks of each element: the lowest +value has rank 1, next lowest rank 2, etc. If two elements are the same +then they share the same rank. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::MoreUtils qw(uniq); + +sub rankArray (@ints) { + my ($i,%idx) = (1); + $idx{$_} = $i++ for uniq sort { $a <=> $b } @ints; + return map { $idx{$_} } @ints; +} + +is([rankArray(55,22,44,33)],[4,1,3,2],'Example 1'); +is([rankArray(10,10,10)],[1,1,1],'Example 2'); +is([rankArray(5,1,1,4,3)],[4,1,1,3,2],'Example 3'); + +done_testing; diff --git a/challenge-322/perlboy1967/perl/ch1.pl b/challenge-322/perlboy1967/perl/ch1.pl deleted file mode 100755 index d945105fd2..0000000000 --- a/challenge-322/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 322 -L - -Author: Niels 'PerlBoy' van Dijke - -Task 1: String Format -Submitted by: Mohammad Sajid Anwar - -You are given a string and a positive integer. - -Write a script to format the string, removing any dashes, in groups of -size given by the integer. The first group can be smaller than the integer -but should have at least one character. Groups should be separated by dashes. - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); -use Test2::V0 qw(-no_srand); -no warnings qw(experimental::signatures); - -sub stringFormat ($str,$len) { - my @r; - - # Get first part - $str =~ s/([^-]{1,$len})//; - push(@r,$1); - - # Get the rest - $str =~ s/-//g; - 1 while (push(@r,substr($str,0,$len,'')) && $str); - - return join('-',@r); -} - -is(stringFormat('ABC-D-E-F',3),'ABC-DEF','Example 1'); -is(stringFormat('A-BC-D-E',2),'A-BC-DE','Example 2'); -is(stringFormat('-A-B-CD-E',4),'A-BCDE','Example 3'); -is(stringFormat('ABC-DE-F',2),'AB-CD-EF','Own example'); - -done_testing; diff --git a/challenge-322/perlboy1967/perl/ch2.pl b/challenge-322/perlboy1967/perl/ch2.pl deleted file mode 100755 index 5e46d4cf12..0000000000 --- a/challenge-322/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challe