diff options
| author | rir <rirans@comcast.net> | 2025-10-09 18:59:18 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-09 18:59:18 -0400 |
| commit | 4dd5d03f9d485dc76573e5157f0a90752c196949 (patch) | |
| tree | 68539f59b6dc799f00c17fa1028ab4a61fb8908d | |
| parent | 7521a0db8b6b8f5863ef52a15479ab8c1133042b (diff) | |
| parent | 999bf3d54e92961967b26985abb48d20bfc9faf5 (diff) | |
| download | perlweeklychallenge-club-4dd5d03f9d485dc76573e5157f0a90752c196949.tar.gz perlweeklychallenge-club-4dd5d03f9d485dc76573e5157f0a90752c196949.tar.bz2 perlweeklychallenge-club-4dd5d03f9d485dc76573e5157f0a90752c196949.zip | |
Merge branch 'manwar:master' into work
55 files changed, 1602 insertions, 72 deletions
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({ /<lower>/ }).sort; + my @digits = $str.comb.grep({ /<digit>/ }).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 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"); 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 an |
