From e13398f14ec1478982b05d6b0675c1556a129a84 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Tue, 7 Oct 2025 17:29:49 +0200 Subject: Add ch-1 and ch-2 in Perl --- challenge-342/spadacciniweb/perl/ch-1.pl | 70 +++++++++++++++++++++++++ challenge-342/spadacciniweb/perl/ch-2.pl | 88 ++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 challenge-342/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-342/spadacciniweb/perl/ch-2.pl diff --git a/challenge-342/spadacciniweb/perl/ch-1.pl b/challenge-342/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..534ec5f519 --- /dev/null +++ b/challenge-342/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +# Task 1: Balance String +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string made up of lowercase English letters and digits only. +# Write a script to format the give string where no letter is followed by another letter and no digit is followed by another digit. If there are multiple valid rearrangements, always return the lexicographically smallest one. Return empty string if it is impossible to format the string. +# +# Example 1 +# Input: $str = "a0b1c2" +# Output: "0a1b2c" +# +# Example 2 +# Input: $str = "abc12" +# Output: "a1b2c" +# +# Example 3 +# Input: $str = "0a2b1c3" +# Output: "0a1b2c3" +# +# Example 4 +# Input: $str = "1a23" +# Output: "" +# +# Example 5 +# Input: $str = "ab123" +# Output: "1a2b3" + +use strict; +use warnings; + +my $str = "a0b1c2"; +balance_string($str); + +$str = "abc12"; +balance_string($str); + +$str = "0a2b1c3"; +balance_string($str); + +$str = "1a23"; +balance_string($str); + +$str = "ab123"; +balance_string($str); + +exit 0; + +sub balance_string { + my $str = shift; + + my @letters = sort grep /[a-z]/, split //, $str; + my @figures = sort grep /\d/, split //, $str; + + my @balance_string = (); + if (abs((scalar @letters) - (scalar @figures)) <= 1 ) { + if (scalar @letters > scalar @figures) { + push @balance_string, shift @letters; + } + foreach (1 .. scalar @letters) { + push @balance_string, shift @figures; + push @balance_string, shift @letters; + } + push @balance_string, shift @figures + if scalar @figures; + } + + printf "'%s' -> '%s'\n", $str, + join '', @balance_string; +} diff --git a/challenge-342/spadacciniweb/perl/ch-2.pl b/challenge-342/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..ea57646594 --- /dev/null +++ b/challenge-342/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl + +# Task 2: Max Score +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, containing 0 and 1 only. +# Write a script to return the max score after splitting the string into two non-empty substrings. The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. +# +# Example 1 +# Input: $str = "0011" +# Output: 4 +# +# 1: left = "0", right = "011" => 1 + 2 => 3 +# 2: left = "00", right = "11" => 2 + 2 => 4 +# 3: left = "001", right = "1" => 2 + 1 => 3 +# +# Example 2 +# Input: $str = "0000" +# Output: 3 +# +# 1: left = "0", right = "000" => 1 + 0 => 1 +# 2: left = "00", right = "00" => 2 + 0 => 2 +# 3: left = "000", right = "0" => 3 + 0 => 3 +# +# Example 3 +# Input: $str = "1111" +# Output: 3 +# +# 1: left = "1", right = "111" => 0 + 3 => 3 +# 2: left = "11", right = "11" => 0 + 2 => 2 +# 3: left = "111", right = "1" => 0 + 1 => 1 +# +# Example 4 +# Input: $str = "0101" +# Output: 3 +# +# 1: left = "0", right = "101" => 1 + 2 => 3 +# 2: left = "01", right = "01" => 1 + 1 => 2 +# 3: left = "010", right = "1" => 2 + 1 => 3 +# +# Example 5 +# Input: $str = "011101" +# Output: 5 +# +# 1: left = "0", right = "11101" => 1 + 4 => 5 +# 2: left = "01", right = "1101" => 1 + 3 => 4 +# 3: left = "011", right = "101" => 1 + 2 => 3 +# 4: left = "0111", right = "01" => 1 + 1 => 2 +# 5: left = "01110", right = "1" => 2 + 1 => 3 + +use strict; +use warnings; + +my $str = "0011"; +max_score($str); + +$str = "0000"; +max_score($str); + +$str = "1111"; +max_score($str); + +$str = "0101"; +max_score($str); + +$str = "011101"; +max_score($str); + +exit 0; + +sub max_score { + my $str = shift; + + my $max_score = 0; + if (length($str) > 2) { + foreach my $i (1 .. length($str)-1) { + my $left = substr $str, 0, $i; + my $right = substr $str, $i; + my $score = (scalar map { $_ == 0 ? 1 : () } split //, $left) + + + (scalar map { $_ == 1 ? 1 : () } split //, $right); + $max_score = $score + if $score > $max_score; + } + } + + printf "'%s' -> %d\n", $str, $max_score; +} -- cgit From 7d50d5bfc29183579944c4832c7aae93abe661d1 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Tue, 7 Oct 2025 18:39:18 -0400 Subject: Challenge 342 by Jaldhar H. Vyas. --- challenge-342/jaldhar-h-vyas/blog.txt | 1 + challenge-342/jaldhar-h-vyas/perl/ch-1.pl | 38 +++++++++++++++++++++++++++++ challenge-342/jaldhar-h-vyas/perl/ch-2.pl | 16 ++++++++++++ challenge-342/jaldhar-h-vyas/raku/ch-1.raku | 38 +++++++++++++++++++++++++++++ challenge-342/jaldhar-h-vyas/raku/ch-2.raku | 18 ++++++++++++++ 5 files changed, 111 insertions(+) create mode 100644 challenge-342/jaldhar-h-vyas/blog.txt create mode 100755 challenge-342/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-342/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-342/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-342/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-342/jaldhar-h-vyas/blog.txt b/challenge-342/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..2a7aedaa97 --- /dev/null +++ b/challenge-342/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2025/10/perl_weekly_challenge_week_342.html diff --git a/challenge-342/jaldhar-h-vyas/perl/ch-1.pl b/challenge-342/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..a86c807a6c --- /dev/null +++ b/challenge-342/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use 5.038; +use warnings; + +my $str = shift; + +my @letters = sort grep { /[[:lower:]]/ } split //, $str; +my @digits = sort grep { /[[:digit:]]/ } split //, $str; +my $digitFirst = scalar @digits >= scalar @letters; + +if (abs(scalar @letters - scalar @digits) > 1) { + say ""; + exit; +} + +my $result = ""; +my $lindex = 0; +my $dindex = 0; + +while ($lindex < scalar @letters || $dindex < scalar @digits) { + if ($digitFirst) { + if ($dindex < scalar @digits) { + $result .= $digits[$dindex++]; + } + if ($lindex < scalar @letters) { + $result .= $letters[$lindex++]; + } + } else { + if ($lindex < scalar @letters) { + $result .= $letters[$lindex++]; + } + if ($dindex < scalar @digits) { + $result .= $digits[$dindex++]; + } + } +} + +say $result; \ No newline at end of file diff --git a/challenge-342/jaldhar-h-vyas/perl/ch-2.pl b/challenge-342/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..24e60a49f8 --- /dev/null +++ b/challenge-342/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use 5.038; +use warnings; + +my $str = shift; +my $max = -"Inf"; + +for my $i (1 .. (length $str) - 1) { + my $score = substr($str, 0, $i) =~ tr/0/0/ + substr($str, $i) =~ tr/1/1/; + + if ($score > $max) { + $max = $score; + } +} + +say $max; diff --git a/challenge-342/jaldhar-h-vyas/raku/ch-1.raku b/challenge-342/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..041c4c6ff0 --- /dev/null +++ b/challenge-342/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,38 @@ +#!/usr/bin/env raku + +sub MAIN( + Str $str +) { + my @letters = $str.comb.grep({ // }).sort; + my @digits = $str.comb.grep({ // }).sort; + my $digitFirst = @digits.elems >= @letters.elems; + + if (@letters.elems - @digits.elems).abs > 1 { + say ""; + exit; + } + + my $result = ""; + my $lindex = 0; + my $dindex = 0; + + while $lindex < @letters.elems || $dindex < @digits.elems { + if $digitFirst { + if $dindex < @digits.elems { + $result ~= @digits[$dindex++]; + } + if $lindex < @letters.elems { + $result ~= @letters[$lindex++]; + } + } else { + if $lindex < @letters.elems { + $result ~= @letters[$lindex++]; + } + if $dindex < @digits.elems { + $result ~= @digits[$dindex++]; + } + } + } + + say $result; +} \ No newline at end of file diff --git a/challenge-342/jaldhar-h-vyas/raku/ch-2.raku b/challenge-342/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..426ce43972 --- /dev/null +++ b/challenge-342/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!/usr/bin/raku + +sub MAIN( + $str, +) { + my $max = -∞; + + for (1 ..^ $str.chars) -> $i { + my $score = $str.substr(0, $i).match(/(0)/, :g).elems + + $str.substr($i).match(/(1)/, :g).elems; + + if ($score > $max) { + $max = $score; + } + } + + say $max; +} \ No newline at end of file -- cgit From 0e3f6c6f6a31a1fd65048d54d3087cc49f1ad62c Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 8 Oct 2025 00:12:09 -0400 Subject: Challenge 342 solutions by Packy Anderson * Raku that is finally Raku-ish * Perl * Python that kinda looks like Raku * Elixir that is starting to look like Elixir 1 blog post --- challenge-342/packy-anderson/README.md | 2 +- challenge-342/packy-anderson/blog.txt | 1 + challenge-342/packy-anderson/elixir/ch-1.exs | 53 ++++++++++++++++++++++++++++ challenge-342/packy-anderson/elixir/ch-2.exs | 46 ++++++++++++++++++++++++ challenge-342/packy-anderson/perl/ch-1.pl | 46 ++++++++++++++++++++++++ challenge-342/packy-anderson/perl/ch-2.pl | 47 ++++++++++++++++++++++++ challenge-342/packy-anderson/python/ch-1.py | 42 ++++++++++++++++++++++ challenge-342/packy-anderson/python/ch-2.py | 40 +++++++++++++++++++++ challenge-342/packy-anderson/raku/ch-1.raku | 46 ++++++++++++++++++++++++ challenge-342/packy-anderson/raku/ch-2.raku | 47 ++++++++++++++++++++++++ 10 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 challenge-342/packy-anderson/blog.txt create mode 100755 challenge-342/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-342/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-342/packy-anderson/perl/ch-1.pl create mode 100755 challenge-342/packy-anderson/perl/ch-2.pl create mode 100755 challenge-342/packy-anderson/python/ch-1.py create mode 100755 challenge-342/packy-anderson/python/ch-2.py create mode 100755 challenge-342/packy-anderson/raku/ch-1.raku create mode 100755 challenge-342/packy-anderson/raku/ch-2.raku diff --git a/challenge-342/packy-anderson/README.md b/challenge-342/packy-anderson/README.md index 704729b966..29039bec47 100644 --- a/challenge-342/packy-anderson/README.md +++ b/challenge-342/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Something just BROKE](https://packy.dardan.com/b/bw) +[Perl Weekly Challenge: Balanced Adagio for Strings](https://packy.dardan.com/b/cQ) diff --git a/challenge-342/packy-anderson/blog.txt b/challenge-342/packy-anderson/blog.txt new file mode 100644 index 0000000000..980befd960 --- /dev/null +++ b/challenge-342/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/cQ \ No newline at end of file diff --git a/challenge-342/packy-anderson/elixir/ch-1.exs b/challenge-342/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..ce9f483389 --- /dev/null +++ b/challenge-342/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,53 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def my_zip([], [], str), do: str + + def my_zip([a], [], str), do: str <> a + + def my_zip([a | longer], [b | shorter], str) do + str = str <> a <> b + my_zip(longer, shorter, str) + end + + def balance_str(str) do + letters = String.graphemes(str) + |> Enum.filter(fn c -> c =~ ~r/[a-z]/ end) + |> Enum.sort + numbers = String.graphemes(str) + |> Enum.filter(fn c -> c =~ ~r/\d/ end) + |> Enum.sort + + numlen = length(numbers) + letlen = length(letters) + + cond do + abs(numlen - letlen) > 1 -> + "" + letlen > numlen -> + my_zip(letters, numbers, "") + true -> + my_zip(numbers, letters, "") + end + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + IO.puts("Output: \"#{balance_str(str)}\"") + end +end + +IO.puts("Example 1:") +PWC.solution("a0b1c2") + +IO.puts("\nExample 2:") +PWC.solution("abc12") + +IO.puts("\nExample 3:") +PWC.solution("0a2b1c3") + +IO.puts("\nExample 4:") +PWC.solution("1a23") + +IO.puts("\nExample 5:") +PWC.solution("ab123") diff --git a/challenge-342/packy-anderson/elixir/ch-2.exs b/challenge-342/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..86548ca40e --- /dev/null +++ b/challenge-342/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,46 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def max_score(_, len, split, _, max, explain) + when split >= len, do: {max, explain} + + def max_score(str, len, split, count, max, explain) do + left = String.slice(str, 0, split) + right = String.slice(str, split, len) + zeros = left |> String.graphemes + |> Enum.filter(fn c -> c == "0" end) |> length + ones = right|> String.graphemes + |> Enum.filter(fn c -> c == "1" end) |> length + sum = zeros + ones + explain = explain + <> "\n#{count}: left = \"#{left}\", right = \"#{right}\"" + <> " => #{zeros} + #{ones} => #{sum}" + max = max(max, sum) + max_score(str, len, split+1, count+1, max, explain) + end + + def max_score(str) do + max_score(str, String.length(str), 1, 1, 0, "") + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + {max, explain} = max_score(str) + IO.puts("Output: #{max}\n#{explain}") + end +end + +IO.puts("Example 1:") +PWC.solution("0011") + +IO.puts("\nExample 2:") +PWC.solution("0000") + +IO.puts("\nExample 3:") +PWC.solution("1111") + +IO.puts("\nExample 4:") +PWC.solution("0101") + +IO.puts("\nExample 5:") +PWC.solution("011101") diff --git a/challenge-342/packy-anderson/perl/ch-1.pl b/challenge-342/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..61ec661daf --- /dev/null +++ b/challenge-342/packy-anderson/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl +use v5.40; + +sub my_zip($longer, $shorter, $str) { + $str .= shift(@$longer) . shift(@$shorter); + if (@$shorter) { + return my_zip($longer, $shorter, $str); + } + elsif (@$longer) { + $str .= shift(@$longer); + } + return $str; +} + +sub balance_str($str) { + my @letters = sort grep { /[a-z]/ } split //, $str; + my @numbers = sort grep { /\d/ } split //, $str; + + return "" if abs(@letters - @numbers) > 1; + if (@letters > @numbers) { + return my_zip(\@letters, \@numbers, ""); + } + else { + return my_zip(\@numbers, \@letters, ""); + } +} + +sub solution($str) { + say 'Input: $str = "' . $str . '"'; + say 'Output: "' . balance_str($str) . '"'; +} + +say "Example 1:"; +solution("a0b1c2"); + +say "\nExample 2:"; +solution("abc12"); + +say "\nExample 3:"; +solution("0a2b1c3"); + +say "\nExample 4:"; +solution("1a23"); + +say "\nExample 5:"; +solution("ab123"); diff --git a/challenge-342/packy-anderson/perl/ch-2.pl b/challenge-342/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..7b0b115636 --- /dev/null +++ b/challenge-342/packy-anderson/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( max ); + +sub max_score2($str, $split, $count, + $max, $explain) { + my $left = substr($str, 0, $split); + my $right = substr($str, $split); + my $zeros = scalar(grep { /0/ } split //, $left); + my $ones = scalar(grep { /1/ } split //, $right); + my $sum = $zeros + $ones; + $explain .= qq{\n$count: left = "$left", right = "$right"} + . qq{ => $zeros + $ones => $sum}; + $max = max($max, $sum); + if (length($right) == 1) { + # there's no more to split! + return ($max, $explain); + } + return max_score2($str, $split+1, $count+1, $max, $explain); +} + +sub max_score($str) { + max_score2($str, 1, 1, 0, ""); +} + +sub solution($str) { + say 'Input: $str = "' . $str . '"'; + my ($max, $explain) = max_score($str); + say 'Output: ' . $max; + say "$explain\n"; +} + +say "Example 1:"; +solution("0011"); + +say "\nExample 2:"; +solution("0000"); + +say "\nExample 3:"; +solution("1111"); + +say "\nExample 4:"; +solution("0101"); + +say "\nExample 5:"; +solution("011101"); diff --git a/challenge-342/packy-anderson/python/ch-1.py b/challenge-342/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..dd4aa74262 --- /dev/null +++ b/challenge-342/packy-anderson/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +def my_zip(longer, shorter, mystr): + mystr += longer.pop(0) + shorter.pop(0) + if len(shorter) > 0: + return my_zip(longer, shorter, mystr) + elif len(longer) > 0: + return mystr + longer.pop(0) + return mystr + +def balance_str(mystr): + letters = sorted([ + c for c in list(mystr) if not c.isnumeric() + ]) + numbers = sorted([ + c for c in list(mystr) if c.isnumeric() + ]) + if abs(len(letters) - len(numbers)) > 1: + return "" + if len(letters) > len(numbers): + return my_zip(letters, numbers, "") + else: + return my_zip(numbers, letters, "") + +def solution(mystr): + print(f'Input: $str = "{mystr}"') + print(f'Output: "{balance_str(mystr)}"') + +print('Example 1:') +solution("a0b1c2") + +print('\nExample 2:') +solution("abc12") + +print('\nExample 3:') +solution("0a2b1c3") + +print('\nExample 4:') +solution("1a23") + +print('\nExample 5:') +solution("ab123") diff --git a/challenge-342/packy-anderson/python/ch-2.py b/challenge-342/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..af29198889 --- /dev/null +++ b/challenge-342/packy-anderson/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +def max_score2(mystr, split, count, maxval, explain): + left = mystr[0:split] + right = mystr[split:] + zeros = len([ c for c in list(left) if c == "0" ]) + ones = len([ c for c in list(right) if c == "1" ]) + mysum = zeros + ones + explain += ( + f'\n{count}: left = "{left}", right = "{right}"' + + f' => {zeros} + {ones} => {mysum}' + ) + maxval = max(maxval, mysum) + if len(right) == 1: + # there's no more to split! + return maxval, explain + return max_score2(mystr, split+1, count+1, maxval, explain) + +def max_score(mystr): + return max_score2(mystr, 1, 1, 0, "") + +def solution(mystr): + print(f'Input: $str = "{mystr}"') + maxval, explain = max_score(mystr) + print(f'Output: {maxval}\n{explain}') + +print('Example 1:') +solution("0011") + +print('\nExample 2:') +solution("0000") + +print('\nExample 3:') +solution("1111") + +print('\nExample 4:') +solution("0101") + +print('\nExample 5:') +solution("011101") diff --git a/challenge-342/packy-anderson/raku/ch-1.raku b/challenge-342/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..962fac8837 --- /dev/null +++ b/challenge-342/packy-anderson/raku/ch-1.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env raku +use v6; + +multi my_zip([], [], $str) { $str } + +multi my_zip(@longer, [], $str) { + $str ~ @longer.shift +} + +multi my_zip(@longer, @shorter, $str is copy) { + $str ~= @longer.shift ~ @shorter.shift; + my_zip(@longer, @shorter, $str); +} + +sub balance_str($str) { + my @letters = $str.comb(/<[a..z]>/).sort; + my @numbers = $str.comb(/\d/).sort; + + return "" if abs(@letters - @numbers) > 1; + if (@letters > @numbers) { + return my_zip(@letters, @numbers, ""); + } + else { + return my_zip(@numbers, @letters, ""); + } +} + +sub solution($str) { + say 'Input: $str = "' ~ $str ~ '"'; + say 'Output: "' ~ balance_str($str) ~ '"'; +} + +say "Example 1:"; +solution("a0b1c2"); + +say "\nExample 2:"; +solution("abc12"); + +say "\nExample 3:"; +solution("0a2b1c3"); + +say "\nExample 4:"; +solution("1a23"); + +say "\nExample 5:"; +solution("ab123"); diff --git a/challenge-342/packy-anderson/raku/ch-2.raku b/challenge-342/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..cf49065664 --- /dev/null +++ b/challenge-342/packy-anderson/raku/ch-2.raku @@ -0,0 +1,47 @@ +#!/usr/bin/env raku +use v6; + +multi max_score($str, $split where $split >= $str.chars, $count, + $max, $explain) { + # there's no more to split! + return ($max, $explain); +} + +multi max_score($str, $split, $count, + $max is copy, $explain is copy) { + my $left = $str.substr(0, $split); + my $right = $str.substr($split); + my $zeros = $left.comb(/0/).elems; + my $ones = $right.comb(/1/).elems; + my $sum = $zeros + $ones; + $explain ~= qq{\n$count: left = "$left", right = "$right"} + ~ qq{ => $zeros + $ones => $sum}; + $max = max($max, $sum); + return max_score($str, $split+1, $count+1, $max, $explain); +} + +multi max_score($str) { + max_score($str, 1, 1, 0, ""); +} + +sub solution($str) { + say 'Input: $str = "' ~ $str ~ '"'; + my ($max, $explain) = max_score($str); + say 'Output: ' ~ $max; + say "$explain\n"; +} + +say "Example 1:"; +solution("0011"); + +say "\nExample 2:"; +solution("0000"); + +say "\nExample 3:"; +solution("1111"); + +say "\nExample 4:"; +solution("0101"); + +say "\nExample 5:"; +solution("011101"); -- cgit From 17439607e4350aa10c2b6d7eb6c357077ff6f795 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Wed, 8 Oct 2025 19:43:20 +0200 Subject: Create ch-1.pl --- challenge-342/wanderdoc/perl/ch-1.pl | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 challenge-342/wanderdoc/perl/ch-1.pl diff --git a/challenge-342/wanderdoc/perl/ch-1.pl b/challenge-342/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..ce0d957091 --- /dev/null +++ b/challenge-342/wanderdoc/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string made up of lowercase English letters and digits only. +Write a script to format the give string where no letter is followed by another letter and no digit is followed by another digit. If there are multiple valid rearrangements, always return the lexicographically smallest one. Return empty string if it is impossible to format the string. + +Example 1 + +Input: $str = "a0b1c2" +Output: "0a1b2c" + + +Example 2 + +Input: $str = "abc12" +Output: "a1b2c" + + +Example 3 + +Input: $str = "0a2b1c3" +Output: "0a1b2c3" + + +Example 4 + +Input: $str = "1a23" +Output: "" + + +Example 5 + +Input: $str = "ab123" +Output: "1a2b3" +=cut + + + +use List::MoreUtils qw(pairwise); +use Test2::V0 -no_srand => 1; + +is(balance_string('a0b1c2'), '0a1b2c', 'Example 1'); +is(balance_string('abc12'), 'a1b2c', 'Example 2'); +is(balance_string('0a2b1c3'), '0a1b2c3', 'Example 3'); +is(balance_string('1a23'), '', 'Example 4'); +is(balance_string('ab123'), '1a2b3', 'Example 5'); + +done_testing(); + +sub balance_string +{ + my $str = $_[0]; + my (@ltr, @num); + for my $chr ( split(//, $str) ) + { + if ($chr =~ /[a-z]/) + { + push @ltr, $chr; + } + elsif ( $chr =~ /[0-9]/ ) + { + push @num, $chr; + } + } + my $count_nums = scalar(@num); + my $count_ltrs = scalar(@ltr); + return '' if (abs($count_ltrs - $count_nums) > 1); + @ltr = sort { $a cmp $b } @ltr; + @num = sort { $a <=> $b } @num; + my @output; + if ( $count_nums >= $count_ltrs) + { + @output = pairwise { $a . ($b||'') } @num, @ltr; + } + else + { + @output = pairwise { $a . ($b||'') } @ltr, @num; + } + return join('', @output); +} -- cgit From 10102afcf768fa417030efc73f0679d221ae6a01 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Wed, 8 Oct 2025 19:44:07 +0200 Subject: Create ch-2.pl --- challenge-342/wanderdoc/perl/ch-2.pl | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 challenge-342/wanderdoc/perl/ch-2.pl diff --git a/challenge-342/wanderdoc/perl/ch-2.pl b/challenge-342/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..13ce98fcc4 --- /dev/null +++ b/challenge-342/wanderdoc/perl/ch-2.pl @@ -0,0 +1,86 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string, $str, containing 0 and 1 only. +Write a script to return the max score after splitting the string into two non-empty substrings. The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. + +Example 1 + +Input: $str = "0011" +Output: 4 + +1: left = "0", right = "011" => 1 + 2 => 3 +2: left = "00", right = "11" => 2 + 2 => 4 +3: left = "001", right = "1" => 2 + 1 => 3 + + +Example 2 + +Input: $str = "0000" +Output: 3 + +1: left = "0", right = "000" => 1 + 0 => 1 +2: left = "00", right = "00" => 2 + 0 => 2 +3: left = "000", right = "0" => 3 + 0 => 3 + + +Example 3 + +Input: $str = "1111" +Output: 3 + +1: left = "1", right = "111" => 0 + 3 => 3 +2: left = "11", right = "11" => 0 + 2 => 2 +3: left = "111", right = "1" => 0 + 1 => 1 + + +Example 4 + +Input: $str = "0101" +Output: 3 + +1: left = "0", right = "101" => 1 + 2 => 3 +2: left = "01", right = "01" => 1 + 1 => 2 +3: left = "010", right = "1" => 2 + 1 => 3 + + +Example 5 + +Input: $str = "011101" +Output: 5 + +1: left = "0", right = "11101" => 1 + 4 => 5 +2: left = "01", right = "1101" => 1 + 3 => 4 +3: left = "011", right = "101" => 1 + 2 => 3 +4: left = "0111", right = "01" => 1 + 1 => 2 +5: left = "01110", right = "1" => 2 + 1 => 3 +=cut + +use Test2::V0 -no_srand => 1; +is(max_score('0011'), 4, 'Example 1'); +is(max_score('0000'), 3, 'Example 2'); +is(max_score('1111'), 3, 'Example 3'); +is(max_score('0101'), 3, 'Example 4'); +is(max_score('011101'), 5, 'Example 5'); + +done_testing(); + +sub max_score +{ + my $str = $_[0]; + if ( $str !~ /0/ or $str !~ /1/ ) + { + return length($str) - 1; + } + my $max_score = 0; + for my $idx ( 1 .. length($str) - 1 ) + { + my $zeros = substr($str, 0, $idx) =~ tr/0/0/; + my $ones = substr($str, $idx) =~ tr/1/1/; + my $score = $zeros + $ones; + $max_score = $max_score >= $score ? $max_score : $score; + } + return $max_score; +} -- cgit From fbcde5801edd9c80f3fad7b0e69ae0aa3b823322 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Wed, 8 Oct 2025 20:15:05 +0100 Subject: - Added solutions by Packy Anderson. - Added solutions by Jaldhar H. Vyas. - Added solutions by Wanderdoc. --- stats/pwc-current.json | 63 ++++++++++++++++++++++++++++++- 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 | 10 ++--- stats/pwc-language-breakdown-summary.json | 8 ++-- stats/pwc-leaders.json | 22 +++++------ stats/pwc-summary-1-30.json | 2 +- stats/pwc-summary-121-150.json | 2 +- stats/pwc-summary-151-180.json | 2 +- stats/pwc-summary-181-210.json | 2 +- stats/pwc-summary-211-240.json | 8 ++-- stats/pwc-summary-241-270.json | 2 +- stats/pwc-summary-271-300.json | 2 +- stats/pwc-summary-301-330.json | 4 +- stats/pwc-summary-31-60.json | 4 +- stats/pwc-summary-61-90.json | 2 +- stats/pwc-summary-91-120.json | 8 ++-- stats/pwc-summary.json | 18 ++++----- stats/pwc-yearly-language-summary.json | 10 ++--- 23 files changed, 121 insertions(+), 60 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 41fc30a9a6..2feddecabc 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -68,6 +68,24 @@ "id" : "Feng Chang", "name" : "Feng Chang" }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, { "data" : [ [ @@ -102,6 +120,24 @@ "id" : "Mohammad Sajid Anwar", "name" : "Mohammad Sajid Anwar" }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" + }, { "data" : [ [ @@ -177,6 +213,16 @@ ], "id" : "W. Luis Mochan", "name" : "W. Luis Mochan" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Wanderdoc", + "name" : "Wanderdoc" } ] }, @@ -226,6 +272,11 @@ "name" : "Feng Chang", "y" : 2 }, + { + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "y" : 5 + }, { "drilldown" : "Lubos Kolouch", "name" : "Lubos Kolouch", @@ -241,6 +292,11 @@ "name" : "Mohammad Sajid Anwar", "y" : 2 }, + { + "drilldown" : "Packy Anderson", + "name" : "Packy Anderson", + "y" : 5 + }, { "drilldown" : "Peter Campbell Smith", "name" : "Peter Campbell Smith", @@ -270,13 +326,18 @@ "drilldown" : "W. Luis Mochan", "name" : "W. Luis Mochan", "y" : 3 + }, + { + "drilldown" : "Wanderdoc", + "name" : "Wanderdoc", + "y" : 2 } ], "name" : "The Weekly Challenge - 342" } ], "subtitle" : { - "text" : "[Champions: 15] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 18] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge - 342" diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index 0f648c5b6f..60fe0856f3 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -970,7 +970,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2020.json b/stats/pwc-language-breakdown-2020.json index 3ceb738698..7ee7ae50f7 100644 --- a/stats/pwc-language-breakdown-2020.json +++ b/stats/pwc-language-breakdown-2020.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2021.json b/stats/pwc-language-breakdown-2021.json index 3a41049b89..471fb0bd34 100644 --- a/stats/pwc-language-breakdown-2021.json +++ b/stats/pwc-language-breakdown-2021.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2022.json b/stats/pwc-language-breakdown-2022.json index cbf8954d7b..12ff9425b6 100644 --- a/stats/pwc-language-breakdown-2022.json +++ b/stats/pwc-language-breakdown-2022.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2023.json b/stats/pwc-language-breakdown-2023.json index d8cdb45fb6..6b58300775 100644 --- a/stats/pwc-language-breakdown-2023.json +++ b/stats/pwc-language-breakdown-2023.json @@ -1200,7 +1200,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2024.json b/stats/pwc-language-breakdown-2024.json index ce8d29b3ef..aa95c800ed 100644 --- a/stats/pwc-language-breakdown-2024.json +++ b/stats/pwc-language-breakdown-2024.json @@ -1246,7 +1246,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2025.json b/stats/pwc-language-breakdown-2025.json index 7826f0a495..a41e36b582 100644 --- a/stats/pwc-language-breakdown-2025.json +++ b/stats/pwc-language-breakdown-2025.json @@ -8,15 +8,15 @@ "data" : [ [ "Perl", - 23 + 29 ], [ "Raku", - 9 + 13 ], [ "Blog", - 5 + 7 ] ], "id" : "342", @@ -745,7 +745,7 @@ { "drilldown" : "342", "name" : "342", - "y" : 37 + "y" : 49 }, { "drilldown" : "341", @@ -947,7 +947,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index cd70de781a..c3a00baf78 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -10,15 +10,15 @@ "data" : [ [ "Perl", - 17619 + 17625 ], [ "Raku", - 9785 + 9789 ], [ "Blog", - 6317 + 6319 ] ], "dataLabels" : { @@ -37,7 +37,7 @@ } ], "subtitle" : { - "text" : "Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Contributions [2019 - 2025]" diff --git a/stats/pwc-leaders.json b/stats/pwc-leaders.json index 09fd34a82c..287d124c25 100644 --- a/stats/pwc-leaders.json +++ b/stats/pwc-leaders.json @@ -22,15 +22,15 @@ "data" : [ [ "Perl", - 632 + 634 ], [ "Raku", - 632 + 634 ], [ "Blog", - 316 + 317 ] ], "id" : "Jaldhar H. Vyas", @@ -374,15 +374,15 @@ "data" : [ [ "Perl", - 186 + 188 ], [ "Raku", - 186 + 188 ], [ "Blog", - 94 + 95 ] ], "id" : "Packy Anderson", @@ -512,7 +512,7 @@ "data" : [ [ "Perl", - 348 + 350 ], [ "Blog", @@ -802,7 +802,7 @@ { "drilldown" : "Jaldhar H. Vyas", "name" : "2: Jaldhar H. Vyas", - "y" : 3160 + "y" : 3170 }, { "drilldown" : "Roger Bell_West", @@ -912,7 +912,7 @@ { "drilldown" : "Packy Anderson", "name" : "24: Packy Anderson", - "y" : 932 + "y" : 942 }, { "drilldown" : "Cheok-Yin Fung", @@ -957,7 +957,7 @@ { "drilldown" : "Wanderdoc", "name" : "33: Wanderdoc", - "y" : 700 + "y" : 704 }, { "drilldown" : "Matthias Muth", @@ -1049,7 +1049,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "Team Leaders (TOP 50)" diff --git a/stats/pwc-summary-1-30.json b/stats/pwc-summary-1-30.json index be05d629ad..b1968cb7fa 100644 --- a/stats/pwc-summary-1-30.json +++ b/stats/pwc-summary-1-30.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-121-150.json b/stats/pwc-summary-121-150.json index 4bbb74c025..eef901ba95 100644 --- a/stats/pwc-summary-121-150.json +++ b/stats/pwc-summary-121-150.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-151-180.json b/stats/pwc-summary-151-180.json index 312458448c..13a7803edf 100644 --- a/stats/pwc-summary-151-180.json +++ b/stats/pwc-summary-151-180.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-181-210.json b/stats/pwc-summary-181-210.json index cb7db6e7e1..e992986c27 100644 --- a/stats/pwc-summary-181-210.json +++ b/stats/pwc-summary-181-210.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-211-240.json b/stats/pwc-summary-211-240.json index 3413b2e568..3693feca44 100644 --- a/stats/pwc-summary-211-240.json +++ b/stats/pwc-summary-211-240.json @@ -17,7 +17,7 @@ 0, 0, 8, - 186, + 188, 18, 594, 3, @@ -52,7 +52,7 @@ 0, 28, 0, - 186, + 188, 0, 0, 0, @@ -87,7 +87,7 @@ 0, 0, 0, - 94, + 95, 0, 0, 1, @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-241-270.json b/stats/pwc-summary-241-270.json index 2021b53904..f1637b3418 100644 --- a/stats/pwc-summary-241-270.json +++ b/stats/pwc-summary-241-270.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-271-300.json b/stats/pwc-summary-271-300.json index 442d2a990b..2d1e5c45c3 100644 --- a/stats/pwc-summary-271-300.json +++ b/stats/pwc-summary-271-300.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-301-330.json b/stats/pwc-summary-301-330.json index c08cfb32b1..5056cb872d 100644 --- a/stats/pwc-summary-301-330.json +++ b/stats/pwc-summary-301-330.json @@ -30,7 +30,7 @@ 4, 502, 87, - 348, + 350, 1, 3, 3, @@ -109,7 +109,7 @@ } ], "subtitle" : { - "text" : "[Champions: 28] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 28] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-31-60.json b/stats/pwc-summary-31-60.json index c9fdb1cc1c..eec4ac1618 100644 --- a/stats/pwc-summary-31-60.json +++ b/stats/pwc-summary-31-60.json @@ -69,8 +69,8 @@ 9, 0, 0, - 17, 2, + 17, 0, 194, 0, @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-61-90.json b/stats/pwc-summary-61-90.json index a33af7b6ad..61b0c39452 100644 --- a/stats/pwc-summary-61-90.json +++ b/stats/pwc-summary-61-90.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-91-120.json b/stats/pwc-summary-91-120.json index 9186fffd77..e881c203fb 100644 --- a/stats/pwc-summary-91-120.json +++ b/stats/pwc-summary-91-120.json @@ -34,7 +34,7 @@ 0, 12, 6, - 632, + 634, 1, 2, 306, @@ -69,7 +69,7 @@ 0, 0, 0, - 632, + 634, 0, 3, 10, @@ -104,7 +104,7 @@ 0, 0, 0, - 316, + 317, 0, 0, 125, @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 30] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary.json b/stats/pwc-summary.json index 86540937f5..f5cc5686ef 100644 --- a/stats/pwc-summary.json +++ b/stats/pwc-summary.json @@ -124,7 +124,7 @@ 0, 6, 5, - 316, + 317, 1, 1, 153, @@ -227,7 +227,7 @@ 0, 0, 5, - 94, + 95, 9, 297, 2, @@ -330,7 +330,7 @@ 2, 251, 44, - 184, + 185, 1, 3, 3, @@ -397,8 +397,8 @@ 6, 0, 0, - 1, 9, + 1, 0, 104, 0, @@ -457,7 +457,7 @@ 0, 0, 0, - 316, + 317, 0, 2, 5, @@ -560,7 +560,7 @@ 0, 20, 0, - 94, + 95, 0, 0, 0, @@ -790,7 +790,7 @@ 0, 0, 0, - 316, + 317, 0, 0, 125, @@ -893,7 +893,7 @@ 0, 0, 0, - 94, + 95, 0, 0, 1, @@ -1009,7 +1009,7 @@ } ], "subtitle" : { - "text" : "[Champions: 328] Last updated at 2025-10-07 19:41:06 GMT" + "text" : "[Champions: 328] Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-yearly-language-summary.json b/stats/pwc-yearly-language-summary.json index 5132a147a1..82ef6c3a03 100644 --- a/stats/pwc-yearly-language-summary.json +++ b/stats/pwc-yearly-language-summary.json @@ -8,15 +8,15 @@ "data" : [ [ "Perl", - 1842 + 1848 ], [ "Raku", - 909 + 913 ], [ "Blog", - 727 + 729 ] ], "id" : "2025", @@ -151,7 +151,7 @@ { "drilldown" : "2025", "name" : "2025", - "y" : 3478 + "y" : 3490 }, { "drilldown" : "2024", @@ -188,7 +188,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-07 19:41:06 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-10-08 19:14:08 GMT" }, "title" : { "text" : "The Weekly Challenge Language" -- cgit From 237e8825539cf440e122a6737bff6b54eae66cf8 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 9 Oct 2025 09:23:20 +0200 Subject: Add ch-1 and ch-2 in Python --- challenge-342/spadacciniweb/python/ch-1.py | 62 ++++++++++++++++++++++++ challenge-342/spadacciniweb/python/ch-2.py | 76 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 challenge-342/spadacciniweb/python/ch-1.py create mode 100644 challenge-342/spadacciniweb/python/ch-2.py diff --git a/challenge-342/spadacciniweb/python/ch-1.py b/challenge-342/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..59db40fdc9 --- /dev/null +++ b/challenge-342/spadacciniweb/python/ch-1.py @@ -0,0 +1,62 @@ +# Task 1: Balance String +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string made up of lowercase English letters and digits only. +# Write a script to format the give string where no letter is followed by another letter and no digit is followed by another digit. If there are multiple valid rearrangements, always return the lexicographically smallest one. Return empty string if it is impossible to format the string. +# +# Example 1 +# Input: $str = "a0b1c2" +# Output: "0a1b2c" +# +# Example 2 +# Input: $str = "abc12" +# Output: "a1b2c" +# +# Example 3 +# Input: $str = "0a2b1c3" +# Output: "0a1b2c3" +# +# Example 4 +# Input: $str = "1a23" +# Output: "" +# +# Example 5 +# Input: $str = "ab123" +# Output: "1a2b3" + +def balance_string(str): + letters = sorted( [x for x in str if x.isalpha()] ) + figures = sorted( [x for x in str if x.isnumeric()] ) + + balance_string = '' + if abs( len(letters) - len(figures) ) <= 1: + if len(letters) > len(figures): + balance_string += letters[0] + letters = letters[1:] + + for i in range(0, len(letters)): + balance_string += figures[0] + figures = figures[1:] + balance_string += letters[0] + letters = letters[1:] + + if len(figures): + balance_string += figures[0] + + print("'%s' -> '%s'" % ( str, balance_string )) + +if __name__ == "__main__": + str = "a0b1c2" + balance_string(str) + + str = "abc12"; + balance_string(str) + + str = "0a2b1c3" + balance_string(str) + + str = "1a23" + balance_string(str) + + str = "ab123" + balance_string(str) diff --git a/challenge-342/spadacciniweb/python/ch-2.py b/challenge-342/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..65c0f85a9d --- /dev/null +++ b/challenge-342/spadacciniweb/python/ch-2.py @@ -0,0 +1,76 @@ +# Task 2: Max Score +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, containing 0 and 1 only. +# Write a script to return the max score after splitting the string into two non-empty substrings. The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. +# +# Example 1 +# Input: $str = "0011" +# Output: 4 +# +# 1: left = "0", right = "011" => 1 + 2 => 3 +# 2: left = "00", right = "11" => 2 + 2 => 4 +# 3: left = "001", right = "1" => 2 + 1 => 3 +# +# Example 2 +# Input: $str = "0000" +# Output: 3 +# +# 1: left = "0", right = "000" => 1 + 0 => 1 +# 2: left = "00", right = "00" => 2 + 0 => 2 +# 3: left = "000", right = "0" => 3 + 0 => 3 +# +# Example 3 +# Input: $str = "1111" +# Output: 3 +# +# 1: left = "1", right = "111" => 0 + 3 => 3 +# 2: left = "11", right = "11" => 0 + 2 => 2 +# 3: left = "111", right = "1" => 0 + 1 => 1 +# +# Example 4 +# Input: $str = "0101" +# Output: 3 +# +# 1: left = "0", right = "101" => 1 + 2 => 3 +# 2: left = "01", right = "01" => 1 + 1 => 2 +# 3: left = "010", right = "1" => 2 + 1 => 3 +# +# Example 5 +# Input: $str = "011101" +# Output: 5 +# +# 1: left = "0", right = "11101" => 1 + 4 => 5 +# 2: left = "01", right = "1101" => 1 + 3 => 4 +# 3: left = "011", right = "101" => 1 + 2 => 3 +# 4: left = "0111", right = "01" => 1 + 1 => 2 +# 5: left = "01110", right = "1" => 2 + 1 => 3 + +def max_score(str): + max_score = 0 + if len(str) > 2: + for i in range(1, len(str)): + left = str[0:i] + right = str[i:] + score = len([x for x in left if x == "0"]) +\ + len([x for x in right if x == "1"]) + if score > max_score: + max_score = score + + print("'%s' -> %d" % (str, max_score) ) + +if __name__ == "__main__": + str = "0011" + max_score(str) + + str = "0000" + max_score(str) + + str = "1111" + max_score(str) + + str = "0101" + max_score(str) + + str = "011101" + max_score(str) -- cgit From 9f14a6dc43f47fbd0951630695c5415657ce3dd8 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 9 Oct 2025 12:08:01 +0200 Subject: Add ch-1 and ch-2 in Ruby --- challenge-342/spadacciniweb/ruby/ch-1.rb | 62 +++++++++++++++++++++++++ challenge-342/spadacciniweb/ruby/ch-2.rb | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 challenge-342/spadacciniweb/ruby/ch-1.rb create mode 100644 challenge-342/spadacciniweb/ruby/ch-2.rb diff --git a/challenge-342/spadacciniweb/ruby/ch-1.rb b/challenge-342/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..b456a5e8be --- /dev/null +++ b/challenge-342/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,62 @@ +# Task 1: Balance String +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string made up of lowercase English letters and digits only. +# Write a script to format the give string where no letter is followed by another letter and no digit is followed by another digit. If there are multiple valid rearrangements, always return the lexicographically smallest one. Return empty string if it is impossible to format the string. +# +# Example 1 +# Input: $str = "a0b1c2" +# Output: "0a1b2c" +# +# Example 2 +# Input: $str = "abc12" +# Output: "a1b2c" +# +# Example 3 +# Input: $str = "0a2b1c3" +# Output: "0a1b2c3" +# +# Example 4 +# Input: $str = "1a23" +# Output: "" +# +# Example 5 +# Input: $str = "ab123" +# Output: "1a2b3" + +def balance_string(str) + letters = str.chars.select { |s| /[a-zA-Z]/ =~ s }.sort + figures = str.chars.select { |s| /\d/ =~ s }.sort + balance_string = "" + + if (letters.length - figures.length).abs <= 1 + if letters.length > figures.length + balance_string += letters.shift + end + + (1..letters.length).each do |_| + balance_string += figures.shift + balance_string += letters.shift + end + if figures.length > 0 + balance_string += figures.shift + end + end + + printf "'%s' -> '%s'\n", str, balance_string +end + +str = "a0b1c2" +balance_string(str); + +str = "abc12" +balance_string(str) + +str = "0a2b1c3" +balance_string(str) + +str = "1a23" +balance_string(str) + +str = "ab123" +balance_string(str) diff --git a/challenge-342/spadacciniweb/ruby/ch-2.rb b/challenge-342/spadacciniweb/ruby/ch-2.rb new file mode 100644 index 0000000000..af7264da51 --- /dev/null +++ b/challenge-342/spadacciniweb/ruby/ch-2.rb @@ -0,0 +1,80 @@ +# Task 2: Max Score +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, containing 0 and 1 only. +# Write a script to return the max score after splitting the string into two non-empty substrings. The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring. +# +# Example 1 +# Input: $str = "0011" +# Output: 4 +# +# 1: left = "0", right = "011" => 1 + 2 => 3 +# 2: left = "00", right = "11" => 2 + 2 => 4 +# 3: left = "001", right = "1" => 2 + 1 => 3 +# +# Example 2 +# Input: $str = "0000" +# Output: 3 +# +# 1: left = "0", right = "000" => 1 + 0 => 1 +# 2: left = "00", right = "00" => 2 + 0 => 2 +# 3: left = "000", right = "0" => 3 + 0 => 3 +# +# Example 3 +# Input: $str = "1111" +# Output: 3 +# +# 1: left = "1", right = "111" => 0 + 3 => 3 +# 2: left = "11", right = "11" => 0 + 2 => 2 +# 3: left = "111", right = "1" => 0 + 1 => 1 +# +# Example 4 +# Input: $str = "0101" +# Output: 3 +# +# 1: left = "0", right = "101" => 1 + 2 => 3 +# 2: left = "01", right = "01" => 1 + 1 => 2 +# 3: left = "010", right = "1" => 2 + 1 => 3 +# +# Example 5 +# Input: $str = "011101" +# Output: 5 +# +# 1: left = "0", right = "11101" => 1 + 4 => 5 +# 2: left = "01", right = "1101" => 1 + 3 => 4 +# 3: left = "011", right = "101" => 1 + 2 => 3 +# 4: left = "0111", right = "01" => 1 + 1 => 2 +# 5: left = "01110", right = "1" => 2 + 1 => 3 + +def max_score(str) + max_score = 0 + + if str.length > 2 + (0 .. str.length-2).each do |i| + left = str[0..i] + right = str[i+1..] + score = left.split('').select { |x| x == "0" }.length + + right.split('').select { |x| x == "1" }.length + if score > max_score + max_score = score + end + end + end + + printf "'%s' -> %d\n", str, max_score +end + +str = "0011" +max_score(str) + +str = "0000" +max_score(str) + +str = "1111" +max_score(str) + +str = "0101" +max_score(str) + +str = "011101" +max_score(str) -- cgit From 09330900cb3dea7b22bd1ed3f3029ff15309072e Mon Sep 17 00:00:00 2001 From: Simon Green Date: Thu, 9 Oct 2025 23:26:19 +1000 Subject: sgreen solutions to challenge 342 --- challenge-342/sgreen/README.md | 4 ++-- challenge-342/sgreen/blog.txt | 1 + challenge-342/sgreen/perl/ch-1.pl | 35 +++++++++++++++++++++++++++++++ challenge-342/sgreen/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++++ challenge-342/sgreen/python/ch-1.py | 39 +++++++++++++++++++++++++++++++++++ challenge-342/sgreen/python/ch-2.py | 33 +++++++++++++++++++++++++++++ challenge-342/sgreen/python/test.py | 25 ++++++++++++++++++++++ 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 challenge-342/sgreen/blog.txt create mode 100755 challenge-342/sgreen/perl/ch-1.pl create mode 100755 challenge-342/sgreen/perl/ch-2.pl create mode 100755 challenge-342/sgreen/python/ch-1.py create mode 100755 challenge-342/sgreen/python/ch-2.py create mode 100755 challenge-342/sgreen/python/test.py diff --git a/challenge-342/sgreen/README.md b/challenge-342/sgreen/README.md index 89934768da..33d3a4920d 100644 --- a/challenge-342/sgreen/README.md +++ b/challenge-342/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 341 +# The Weekly Challenge 342 -Blog: [Reversing my broken keys](https://dev.to/simongreennet/weekly-challenge-reversing-my-broken-keys-313l) +Blog: [Balancing the Score](https://dev.to/simongreennet/weekly-challenge-balancing-the-score-38kd) diff --git a/challenge-342/sgreen/blog.txt b/challenge-342/sgreen/blog.txt new file mode 100644 index 0000000000..f77d5dc54e --- /dev/null +++ b/challenge-342/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-balancing-the-score-38kd \ No newline at end of file diff --git a/challenge-342/sgreen/perl/ch-1.pl b/challenge-342/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..5c79dbd8e7 --- /dev/null +++ b/challenge-342/sgreen/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($input_string) { + # Separate letters and digits + my @digits = sort { $a <=> $b } grep { /[0-9]/ } split //, $input_string; + my @letters = sort { lc($a) cmp lc($b) } grep { /[A-Za-z]/ } split //, $input_string; + + # If the difference in length is more than 1, there is no solution + if (abs(scalar(@digits) - scalar(@letters)) > 1) { + say '""'; + return; + } + + # If the lengths are unequal, add a blank to the shorter list + if (scalar(@digits) < scalar(@letters)) { + unshift @digits, ''; + } elsif (scalar(@letters) < scalar(@digits)) { + push @letters, ''; + } + + # Interleave the two lists, digits first + my $solution = ""; + foreach my $i (0 .. $#digits) { + $solution .= $digits[$i] . $letters[$i]; + } + + say '"', $solution, '"'; +} + +main($ARGV[0]); \ No newline at end of file diff --git a/challenge-342/sgreen/perl/ch-2.pl b/challenge-342/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..ae895ff605 --- /dev/null +++ b/challenge-342/sgreen/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub calculate_score ($input_string, $split_index) { + # Split the string at each position and count the 0s on the left and 1s + # on the right + my $left = substr($input_string, 0, $split_index); + my $right = substr($input_string, $split_index); + + my $count_0 = ($left =~ tr/0//); + my $count_1 = ($right =~ tr/1//); + + return $count_0 + $count_1; +} + +sub main ($input_string) { + # Check that the input contains only ones and zeros + if ($input_string !~ /^[01]+$/) { + die "Input must contain only '0' and '1' characters\n"; + } + + my $current_max = 0; + + # Calculate the score for each possible split + foreach my $i (1 .. length($input_string)-1) { + my $score = calculate_score($input_string, $i); + + # If the score is higher than the current max, update it + if ($score > $current_max) { + $current_max = $score; + } + } + + say $current_max; +} + +main($ARGV[0]); \ No newline at end of file diff --git a/challenge-342/sgreen/python/ch-1.py b/challenge-342/sgreen/python/ch-1.py new file mode 100755 index 0000000000..db96019de8 --- /dev/null +++ b/challenge-342/sgreen/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def balance_string(input_string: str) -> str: + # Separate letters and digits + digits = sorted(re.findall(r"\d", input_string)) + letters = sorted( + re.findall(r"[A-Za-z]", input_string), + key=str.lower + ) + + # If the difference in length is more than 1, there is no solution + if abs(len(digits) - len(letters)) > 1: + return "" + + # If the lengths are unequal, add a blank to the shorter list + if len(digits) < len(letters): + digits.insert(0, "") + elif len(letters) < len(digits): + letters.append("") + + # Interleave the two lists, digits first + solution = "" + for i in range(len(digits)): + solution += digits[i] + letters[i] + + return solution + + +def main(): + result = balance_string(sys.argv[1]) + print('"' + result + '"') + + +if __name__ == "__main__": + main() diff --git a/challenge-342/sgreen/python/ch-2.py b/challenge-342/sgreen/python/ch-2.py new file mode 100755 index 0000000000..add5c37753 --- /dev/null +++ b/challenge-342/sgreen/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def max_score(input_string: str) -> int: + # Check that the input contains only ones and zeros + if not re.match(r'^[01]+$', input_string): + raise ValueError("Input must contain only '0' and '1' characters") + + current_max = 0 + + # Split the string at each position and count the 0s on the left and 1s + # on the right + for i in range(1, len(input_string)): + left = input_string[:i].count('0') + right = input_string[i:].count('1') + + # If the score is higher than the current max, update it + if left + right > current_max: + current_max = left + right + + return current_max + + +def main(): + result = max_score(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-342/sgreen/python/test.py b/challenge-342/sgreen/python/test.py new file mode 100755 index 0000000000..debb821112 --- /dev/null +++ b/challenge-342/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.balance_string("a0b1c2"), "0a1b2c") + self.assertEqual(ch_1.balance_string("abc12"), "a1b2c") + self.assertEqual(ch_1.balance_string("0a2b1c3"), "0a1b2c3") + self.assertEqual(ch_1.balance_string("1a23"), "") + self.assertEqual(ch_1.balance