From 6363919707478d0f04dc8bd0793d013389e3d4dc Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 2 Nov 2025 12:35:07 +0100 Subject: Add Perl and Python solutions for challenge 331 --- challenge-331/lubos-kolouch/perl/ch-1.pl | 39 ++++++++++++++++++ challenge-331/lubos-kolouch/perl/ch-2.pl | 65 ++++++++++++++++++++++++++++++ challenge-331/lubos-kolouch/python/ch-1.py | 41 +++++++++++++++++++ challenge-331/lubos-kolouch/python/ch-2.py | 61 ++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 challenge-331/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-331/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-331/lubos-kolouch/python/ch-1.py create mode 100644 challenge-331/lubos-kolouch/python/ch-2.py diff --git a/challenge-331/lubos-kolouch/perl/ch-1.pl b/challenge-331/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..64a049ad6c --- /dev/null +++ b/challenge-331/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(signatures state); +no warnings qw(experimental::signatures); +use Type::Params qw(compile); +use Types::Standard qw(Str); +use Test::More; + +=pod + +=head1 PURPOSE + +Return the length of the final word in a string, ignoring any leading or +trailing whitespace. Words are sequences of non-whitespace characters. + +=head1 ALGORITHM + +We validate the argument with L to ensure a defined string. The +string is scanned for word tokens using a regular expression; if at least one +word is found we report the length of the final token, otherwise we return zero. + +=cut + +## no critic (Subroutines::ProhibitSubroutinePrototypes) +sub last_word_length ($text) { + state $check = compile(Str); + my ($str) = $check->($text); + + my @words = $str =~ /(\S+)/g; + return @words ? length( $words[-1] ) : 0; +} + +# Examples supplied by the specification. +is( last_word_length('The Weekly Challenge'), 9, 'Example 1' ); +is( last_word_length(' Hello World '), 5, 'Example 2' ); +is( last_word_length('Let\'s begin the fun'), 3, 'Example 3' ); + +done_testing(); diff --git a/challenge-331/lubos-kolouch/perl/ch-2.pl b/challenge-331/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..0f3f198525 --- /dev/null +++ b/challenge-331/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(signatures state); +no warnings qw(experimental::signatures); +use Type::Params qw(compile); +use Types::Standard qw(Str); +use Test::More; + +=pod + +=head1 PURPOSE + +Decide whether two strings are buddy strings, i.e. whether exactly one swap of +two characters in C<$source> can produce C<$target>. Identical strings are +considered buddies if any character occurs more than once. + +=head1 ALGORITHM + +Inputs are validated with L. If lengths differ we immediately +return false. Otherwise we collect positions where the characters differ; more +than two mismatches disqualify the pair. Two mismatches form buddies when the +characters cross-match, while zero mismatches form buddies only if the string +contains a duplicate character. + +=cut + +## no critic (Subroutines::ProhibitSubroutinePrototypes) +sub are_buddy_strings ( $source, $target ) { + state $check = compile( Str, Str ); + my ( $src, $tgt ) = $check->( $source, $target ); + + return 0 if length($src) != length($tgt); + + my @diff; + for my $idx ( 0 .. length($src) - 1 ) { + my $s_char = substr $src, $idx, 1; + my $t_char = substr $tgt, $idx, 1; + next if $s_char eq $t_char; + + push @diff, [ $s_char, $t_char ]; + return 0 if @diff > 2; + } + + if ( @diff == 0 ) { + my %seen; + for my $char ( split //, $src ) { + return 1 if ++$seen{$char} > 1; + } + return 0; + } + + return + @diff == 2 + && $diff[0][0] eq $diff[1][1] + && $diff[0][1] eq $diff[1][0]; +} + +# Examples supplied by the specification. +is( are_buddy_strings( 'fuck', 'fcuk' ), 1, 'Example 1' ); +is( are_buddy_strings( 'love', 'love' ), 0, 'Example 2' ); +is( are_buddy_strings( 'fodo', 'food' ), 1, 'Example 3' ); +is( are_buddy_strings( 'feed', 'feed' ), 1, 'Example 4' ); + +done_testing(); diff --git a/challenge-331/lubos-kolouch/python/ch-1.py b/challenge-331/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..f4206c36fa --- /dev/null +++ b/challenge-331/lubos-kolouch/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +""" +Task 1: Last Word + +Determine the length of the final word within a string, trimming any +whitespace that may precede or follow the words. Words are contiguous +sequences of non-whitespace characters. +""" +from __future__ import annotations + +import unittest + + +def last_word_length(text: str) -> int: + """ + Return the length of the last word found in `text`. + + Args: + text: Input string that may contain leading/trailing whitespace. + + Returns: + Length of the final word if present, otherwise zero. + """ + words: list[str] = text.split() + return len(words[-1]) if words else 0 + + +class LastWordTests(unittest.TestCase): + + def test_example_1(self) -> None: + self.assertEqual(last_word_length("The Weekly Challenge"), 9) + + def test_example_2(self) -> None: + self.assertEqual(last_word_length(" Hello World "), 5) + + def test_example_3(self) -> None: + self.assertEqual(last_word_length("Let's begin the fun"), 3) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-331/lubos-kolouch/python/ch-2.py b/challenge-331/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..802c11154e --- /dev/null +++ b/challenge-331/lubos-kolouch/python/ch-2.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +""" +Task 2: Buddy Strings + +Given two strings of equal length, determine whether swapping a single pair of +characters in the source string can produce the target string. Identical +strings are considered buddies when any character appears at least twice. +""" +from __future__ import annotations + +import unittest + + +def are_buddy_strings(source: str, target: str) -> bool: + """ + Decide whether `source` and `target` form a pair of buddy strings. + + Args: + source: Original string. + target: Desired string after at most one swap. + + Returns: + True when a single swap (or a swap of identical characters) can make + the strings equal, otherwise False. + """ + if len(source) != len(target): + return False + + if source == target: + # Identical strings are buddies if any character repeats. + return len(set(source)) < len(source) + + differences: list[tuple[str, + str]] = [(s_char, t_char) + for s_char, t_char in zip(source, target) + if s_char != t_char] + + if len(differences) != 2: + return False + + (first_source, first_target), (second_source, second_target) = differences + return first_source == second_target and first_target == second_source + + +class BuddyStringTests(unittest.TestCase): + + def test_example_1(self) -> None: + self.assertTrue(are_buddy_strings("fuck", "fcuk")) + + def test_example_2(self) -> None: + self.assertFalse(are_buddy_strings("love", "love")) + + def test_example_3(self) -> None: + self.assertTrue(are_buddy_strings("fodo", "food")) + + def test_example_4(self) -> None: + self.assertTrue(are_buddy_strings("feed", "feed")) + + +if __name__ == "__main__": + unittest.main() -- cgit From de10e43cde2640b2d0885697fea1b94b6ee43427 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 3 Nov 2025 11:37:00 +0000 Subject: Challenge 346 Solutions (Raku) --- challenge-346/mark-anderson/raku/ch-1.raku | 20 ++++++++ challenge-346/mark-anderson/raku/ch-2.raku | 81 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-346/mark-anderson/raku/ch-1.raku create mode 100644 challenge-346/mark-anderson/raku/ch-2.raku diff --git a/challenge-346/mark-anderson/raku/ch-1.raku b/challenge-346/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..3bb622d2ee --- /dev/null +++ b/challenge-346/mark-anderson/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku +use Test; + +is longest-parenthesis('(()())'), 6; +is longest-parenthesis(')()())'), 4; +is longest-parenthesis('(((()))()(((()'), 8; +is longest-parenthesis('))))((()('), 2; +is longest-parenthesis('()(()'), 2; + +# I think I want to use the <~~> recursive feature here but I +# don't really understand it. The following comes close but it +# doesn't quite give the output I want. + +# $str ~~ m:g/ '(' [ <-[()]>* <~~> <-[()]>* ]* ')' /; + +sub longest-parenthesis($str) +{ + my @m = $str ~~ m:g/ [ ('('+) (')'+) ]+ /; + max @m>>.chars +} diff --git a/challenge-346/mark-anderson/raku/ch-2.raku b/challenge-346/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..60238a8f9d --- /dev/null +++ b/challenge-346/mark-anderson/raku/ch-2.raku @@ -0,0 +1,81 @@ +#!/usr/bin/env raku +use JSON::Fast; +use Test; + +is-deeply magic-expression("123", 6), ("1*2*3", "1+2+3"); +is-deeply magic-expression("105", 5), ("1*0+5", "10-5"); +is-deeply magic-expression("232", 8), ("2*3+2", "2+3*2"); +is-deeply magic-expression("1234", 10), ("1*2*3+4", "1+2+3+4"); +is-deeply magic-expression("1001", 2), ("1+0*0+1", "1+0+0+1", "1+0-0+1", "1-0*0+1", "1-0+0+1", "1-0-0+1"); + +# +# With help from https://andrewshitov.com/2018/10/31/creating-a-calculator-with-perl-6-grammars/ +# + +grammar Calculate +{ + rule TOP { } + rule add-sub { + % $=<[+-]> } + rule mult { + % '*' } + rule digits { + } +} + +class Actions +{ + method TOP($/) { make $.made } + method digits($/) { make $/ } + method mult($/) { make [*] $>>.made } + method add-sub($/) + { + my @ops = $>>.Str; + my @vals = $>>.made; + my $result; + + for @vals Z @ops.unshift('+') + { + if .tail eq '+' { $result += .head } + else { $result -= .head } + } + + make $result + } +} + +multi magic-expression($str where *.chars > 7, $target) +{ + note "This is a brute force solution: \$str must be 7 characters or less" +} + +multi magic-expression($str, $target) +{ + my @nums = $str.comb; + my (@partitions, @operators) := from-json $=finish; + + gather for @partitions[@nums-2]<> -> @p + { + my @r = @nums.rotor(@p)>>.join; + next if any @r ~~ /^0\d/; + + for @operators[@p-2]<> -> @o + { + my $exp = flat roundrobin @r, @o; + take [~] $exp if Calculate.parse($exp, actions => Actions).made == $target + } + } +} + +=finish +[ +[[[1,1]], +[[1,1,1],[1,2],[2,1]], +[[1,1,1,1],[1,1,2],[1,2,1],[2,1,1],[1,3],[3,1],[2,2]], +[[1,1,1,1,1],[1,1,1,2],[1,1,2,1],[1,2,1,1],[2,1,1,1],[1,1,3],[1,3,1],[3,1,1],[1,2,2],[2,1,2],[2,2,1],[1,4],[4,1],[2,3],[3,2]], +[[1,1,1,1,1,1],[1,1,1,1,2],[1,1,1,2,1],[1,1,2,1,1],[1,2,1,1,1],[2,1,1,1,1],[1,1,1,3],[1,1,3,1],[1,3,1,1],[3,1,1,1],[1,1,2,2],[1,2,1,2],[1,2,2,1],[2,1,1,2],[2,1,2,1],[2,2,1,1],[1,1,4],[1,4,1],[4,1,1],[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1],[1,5],[5,1],[2,2,2],[2,4],[4,2],[3,3]], +[[1,1,1,1,1,1,1],[1,1,1,1,1,2],[1,1,1,1,2,1],[1,1,1,2,1,1],[1,1,2,1,1,1],[1,2,1,1,1,1],[2,1,1,1,1,1],[1,1,1,1,3],[1,1,1,3,1],[1,1,3,1,1],[1,3,1,1,1],[3,1,1,1,1],[1,1,1,2,2],[1,1,2,1,2],[1,1,2,2,1],[1,2,1,1,2],[1,2,1,2,1],[1,2,2,1,1],[2,1,1,1,2],[2,1,1,2,1],[2,1,2,1,1],[2,2,1,1,1],[1,1,1,4],[1,1,4,1],[1,4,1,1],[4,1,1,1],[1,1,2,3],[1,1,3,2],[1,2,1,3],[1,2,3,1],[1,3,1,2],[1,3,2,1],[2,1,1,3],[2,1,3,1],[2,3,1,1],[3,1,1,2],[3,1,2,1],[3,2,1,1],[1,1,5],[1,5,1],[5,1,1],[1,2,2,2],[2,1,2,2],[2,2,1,2],[2,2,2,1],[1,2,4],[1,4,2],[2,1,4],[2,4,1],[4,1,2],[4,2,1],[1,3,3],[3,1,3],[3,3,1],[1,6],[6,1],[2,2,3],[2,3,2],[3,2,2],[2,5],[5,2],[3,4],[4,3]]], + +[[["*"],["+"],["-"]], +[["*","*"],["*","+"],["*","-"],["+","*"],["+","+"],["+","-"],["-","*"],["-","+"],["-","-"]], +[["*","*","*"],["*","*","+"],["*","*","-"],["*","+","*"],["*","+","+"],["*","+","-"],["*","-","*"],["*","-","+"],["*","-","-"],["+","*","*"],["+","*","+"],["+","*","-"],["+","+","*"],["+","+","+"],["+","+","-"],["+","-","*"],["+","-","+"],["+","-","-"],["-","*","*"],["-","*","+"],["-","*","-"],["-","+","*"],["-","+","+"],["-","+","-"],["-","-","*"],["-","-","+"],["-","-","-"]], +[["*","*","*","*"],["*","*","*","+"],["*","*","*","-"],["*","*","+","*"],["*","*","+","+"],["*","*","+","-"],["*","*","-","*"],["*","*","-","+"],["*","*","-","-"],["*","+","*","*"],["*","+","*","+"],["*","+","*","-"],["*","+","+","*"],["*","+","+","+"],["*","+","+","-"],["*","+","-","*"],["*","+","-","+"],["*","+","-","-"],["*","-","*","*"],["*","-","*","+"],["*","-","*","-"],["*","-","+","*"],["*","-","+","+"],["*","-","+","-"],["*","-","-","*"],["*","-","-","+"],["*","-","-","-"],["+","*","*","*"],["+","*","*","+"],["+","*","*","-"],["+","*","+","*"],["+","*","+","+"],["+","*","+","-"],["+","*","-","*"],["+","*","-","+"],["+","*","-","-"],["+","+","*","*"],["+","+","*","+"],["+","+","*","-"],["+","+","+","*"],["+","+","+","+"],["+","+","+","-"],["+","+","-","*"],["+","+","-","+"],["+","+","-","-"],["+","-","*","*"],["+","-","*","+"],["+","-","*","-"],["+","-","+","*"],["+","-","+","+"],["+","-","+","-"],["+","-","-","*"],["+","-","-","+"],["+","-","-","-"],["-","*","*","*"],["-","*","*","+"],["-","*","*","-"],["-","*","+","*"],["-","*","+","+"],["-","*","+","-"],["-","*","-","*"],["-","*","-","+"],["-","*","-","-"],["-","+","*","*"],["-","+","*","+"],["-","+","*","-"],["-","+","+","*"],["-","+","+","+"],["-","+","+","-"],["-","+","-","*"],["-","+","-","+"],["-","+","-","-"],["-","-","*","*"],["-","-","*","+"],["-","-","*","-"],["-","-","+","*"],["-","-","+","+"],["-","-","+","-"],["-","-","-","*"],["-","-","-","+"],["-","-","-","-"]], +[["*","*","*","*","*"],["*","*","*","*","+"],["*","*","*","*","-"],["*","*","*","+","*"],["*","*","*","+","+"],["*","*","*","+","-"],["*","*","*","-","*"],["*","*","*","-","+"],["*","*","*","-","-"],["*","*","+","*","*"],["*","*","+","*","+"],["*","*","+","*","-"],["*","*","+","+","*"],["*","*","+","+","+"],["*","*","+","+","-"],["*","*","+","-","*"],["*","*","+","-","+"],["*","*","+","-","-"],["*","*","-","*","*"],["*","*","-","*","+"],["*","*","-","*","-"],["*","*","-","+","*"],["*","*","-","+","+"],["*","*","-","+","-"],["*","*","-","-","*"],["*","*","-","-","+"],["*","*","-","-","-"],["*","+","*","*","*"],["*","+","*","*","+"],["*","+","*","*","-"],["*","+","*","+","*"],["*","+","*","+","+"],["*","+","*","+","-"],["*","+","*","-","*"],["*","+","*","-","+"],["*","+","*","-","-"],["*","+","+","*","*"],["*","+","+","*","+"],["*","+","+","*","-"],["*","+","+","+","*"],["*","+","+","+","+"],["*","+","+","+","-"],["*","+","+","-","*"],["*","+","+","-","+"],["*","+","+","-","-"],["*","+","-","*","*"],["*","+","-","*","+"],["*","+","-","*","-"],["*","+","-","+","*"],["*","+","-","+","+"],["*","+","-","+","-"],["*","+","-","-","*"],["*","+","-","-","+"],["*","+","-","-","-"],["*","-","*","*","*"],["*","-","*","*","+"],["*","-","*","*","-"],["*","-","*","+","*"],["*","-","*","+","+"],["*","-","*","+","-"],["*","-","*","-","*"],["*","-","*","-","+"],["*","-","*","-","-"],["*","-","+","*","*"],["*","-","+","*","+"],["*","-","+","*","-"],["*","-","+","+","*"],["*","-","+","+","+"],["*","-","+","+","-"],["*","-","+","-","*"],["*","-","+","-","+"],["*","-","+","-","-"],["*","-","-","*","*"],["*","-","-","*","+"],["*","-","-","*","-"],["*","-","-","+","*"],["*","-","-","+","+"],["*","-","-","+","-"],["*","-","-","-","*"],["*","-","-","-","+"],["*","-","-","-","-"],["+","*","*","*","*"],["+","*","*","*","+"],["+","*","*","*","-"],["+","*","*","+","*"],["+","*","*","+","+"],["+","*","*","+","-"],["+","*","*","-","*"],["+","*","*","-","+"],["+","*","*","-","-"],["+","*","+","*","*"],["+","*","+","*","+"],["+","*","+","*","-"],["+","*","+","+","*"],["+","*","+","+","+"],["+","*","+","+","-"],["+","*","+","-","*"],["+","*","+","-","+"],["+","*","+","-","-"],["+","*","-","*","*"],["+","*","-","*","+"],["+","*","-","*","-"],["+","*","-","+","*"],["+","*","-","+","+"],["+","*","-","+","-"],["+","*","-","-","*"],["+","*","-","-","+"],["+","*","-","-","-"],["+","+","*","*","*"],["+","+","*","*","+"],["+","+","*","*","-"],["+","+","*","+","*"],["+","+","*","+","+"],["+","+","*","+","-"],["+","+","*","-","*"],["+","+","*","-","+"],["+","+","*","-","-"],["+","+","+","*","*"],["+","+","+","*","+"],["+","+","+","*","-"],["+","+","+","+","*"],["+","+","+","+","+"],["+","+","+","+","-"],["+","+","+","-","*"],["+","+","+","-","+"],["+","+","+","-","-"],["+","+","-","*","*"],["+","+","-","*","+"],["+","+","-","*","-"],["+","+","-","+","*"],["+","+","-","+","+"],["+","+","-","+","-"],["+","+","-","-","*"],["+","+","-","-","+"],["+","+","-","-","-"],["+","-","*","*","*"],["+","-","*","*","+"],["+","-","*","*","-"],["+","-","*","+","*"],["+","-","*","+","+"],["+","-","*","+","-"],["+","-","*","-","*"],["+","-","*","-","+"],["+","-","*","-","-"],["+","-","+","*","*"],["+","-","+","*","+"],["+","-","+","*","-"],["+","-","+","+","*"],["+","-","+","+","+"],["+","-","+","+","-"],["+","-","+","-","*"],["+","-","+","-","+"],["+","-","+","-","-"],["+","-","-","*","*"],["+","-","-","*","+"],["+","-","-","*","-"],["+","-","-","+","*"],["+","-","-","+","+"],["+","-","-","+","-"],["+","-","-","-","*"],["+","-","-","-","+"],["+","-","-","-","-"],["-","*","*","*","*"],["-","*","*","*","+"],["-","*","*","*","-"],["-","*","*","+","*"],["-","*","*","+","+"],["-","*","*","+","-"],["-","*","*","-","*"],["-","*","*","-","+"],["-","*","*","-","-"],["-","*","+","*","*"],["-","*","+","*","+"],["-","*","+","*","-"],["-","*","+","+","*"],["-","*","+","+","+"],["-","*","+","+","-"],["-","*","+","-","*"],["-","*","+","-","+"],["-","*","+","-","-"],["-","*","-","*","*"],["-","*","-","*","+"],["-","*","-","*","-"],["-","*","-","+","*"],["-","*","-","+","+"],["-","*","-","+","-"],["-","*","-","-","*"],["-","*","-","-","+"],["-","*","-","-","-"],["-","+","*","*","*"],["-","+","*","*","+"],["-","+","*","*","-"],["-","+","*","+","*"],["-","+","*","+","+"],["-","+","*","+","-"],["-","+","*","-","*"],["-","+","*","-","+"],["-","+","*","-","-"],["-","+","+","*","*"],["-","+","+","*","+"],["-","+","+","*","-"],["-","+","+","+","*"],["-","+","+","+","+"],["-","+","+","+","-"],["-","+","+","-","*"],["-","+","+","-","+"],["-","+","+","-","-"],["-","+","-","*","*"],["-","+","-","*","+"],["-","+","-","*","-"],["-","+","-","+","*"],["-","+","-","+","+"],["-","+","-","+","-"],["-","+","-","-","*"],["-","+","-","-","+"],["-","+","-","-","-"],["-","-","*","*","*"],["-","-","*","*","+"],["-","-","*","*","-"],["-","-","*","+","*"],["-","-","*","+","+"],["-","-","*","+","-"],["-","-","*","-","*"],["-","-","*","-","+"],["-","-","*","-","-"],["-","-","+","*","*"],["-","-","+","*","+"],["-","-","+","*","-"],["-","-","+","+","*"],["-","-","+","+","+"],["-","-","+","+","-"],["-","-","+","-","*"],["-","-","+","-","+"],["-","-","+","-","-"],["-","-","-","*","*"],["-","-","-","*","+"],["-","-","-","*","-"],["-","-","-","+","*"],["-","-","-","+","+"],["-","-","-","+","-"],["-","-","-","-","*"],["-","-","-","-","+"],["-","-","-","-","-"]], +[["*","*","*","*","*","*"],["*","*","*","*","*","+"],["*","*","*","*","*","-"],["*","*","*","*","+","*"],["*","*","*","*","+","+"],["*","*","*","*","+","-"],["*","*","*","*","-","*"],["*","*","*","*","-","+"],["*","*","*","*","-","-"],["*","*","*","+","*","*"],["*","*","*","+","*","+"],["*","*","*","+","*","-"],["*","*","*","+","+","*"],["*","*","*","+","+","+"],["*","*","*","+","+","-"],["*","*","*","+","-","*"],["*","*","*","+","-","+"],["*","*","*","+","-","-"],["*","*","*","-","*","*"],["*","*","*","-","*","+"],["*","*","*","-","*","-"],["*","*","*","-","+","*"],["*","*","*","-","+","+"],["*","*","*","-","+","-"],["*","*","*","-","-","*"],["*","*","*","-","-","+"],["*","*","*","-","-","-"],["*","*","+","*","*","*"],["*","*","+","*","*","+"],["*","*","+","*","*","-"],["*","*","+","*","+","*"],["*","*","+","*","+","+"],["*","*","+","*","+","-"],["*","*","+","*","-","*"],["*","*","+","*","-","+"],["*","*","+","*","-","-"],["*","*","+","+","*","*"],["*","*","+","+","*","+"],["*","*","+","+","*","-"],["*","*","+","+","+","*"],["*","*","+","+","+","+"],["*","*","+","+","+","-"],["*","*","+","+","-","*"],["*","*","+","+","-","+"],["*","*","+","+","-","-"],["*","*","+","-","*","*"],["*","*","+","-","*","+"],["*","*","+","-","*","-"],["*","*","+","-","+","*"],["*","*","+","-","+","+"],["*","*","+","-","+","-"],["*","*","+","-","-","*"],["*","*","+","-","-","+"],["*","*","+","-","-","-"],["*","*","-","*","*","*"],["*","*","-","*","*","+"],["*","*","-","*","*","-"],["*","*","-","*","+","*"],["*","*","-","*","+","+"],["*","*","-","*","+","-"],["*","*","-","*","-","*"],["*","*","-","*","-","+"],["*","*","-","*","-","-"],["*","*","-","+","*","*"],["*","*","-","+","*","+"],["*","*","-","+","*","-"],["*","*","-","+","+","*"],["*","*","-","+","+","+"],["*","*","-","+","+","-"],["*","*","-","+","-","*"],["*","*","-","+","-","+"],["*","*","-","+","-","-"],["*","*","-","-","*","*"],["*","*","-","-","*","+"],["*","*","-","-","*","-"],["*","*","-","-","+","*"],["*","*","-","-","+","+"],["*","*","-","-","+","-"],["*","*","-","-","-","*"],["*","*","-","-","-","+"],["*","*","-","-","-","-"],["*","+","*","*","*","*"],["*","+","*","*","*","+"],["*","+","*","*","*","-"],["*","+","*","*","+","*"],["*","+","*","*","+","+"],["*","+","*","*","+","-"],["*","+","*","*","-","*"],["*","+","*","*","-","+"],["*","+","*","*","-","-"],["*","+","*","+","*","*"],["*","+","*","+","*","+"],["*","+","*","+","*","-"],["*","+","*","+","+","*"],["*","+","*","+","+","+"],["*","+","*","+","+","-"],["*","+","*","+","-","*"],["*","+","*","+","-","+"],["*","+","*","+","-","-"],["*","+","*","-","*","*"],["*","+","*","-","*","+"],["*","+","*","-","*","-"],["*","+","*","-","+","*"],["*","+","*","-","+","+"],["*","+","*","-","+","-"],["*","+","*","-","-","*"],["*","+","*","-","-","+"],["*","+","*","-","-","-"],["*","+","+","*","*","*"],["*","+","+","*","*","+"],["*","+","+","*","*","-"],["*","+","+","*","+","*"],["*","+","+","*","+","+"],["*","+","+","*","+","-"],["*","+","+","*","-","*"],["*","+","+","*","-","+"],["*","+","+","*","-","-"],["*","+","+","+","*","*"],["*","+","+","+","*","+"],["*","+","+","+","*","-"],["*","+","+","+","+","*"],["*","+","+","+","+","+"],["*","+","+","+","+","-"],["*","+","+","+","-","*"],["*","+","+","+","-","+"],["*","+","+","+","-","-"],["*","+","+","-","*","*"],["*","+","+","-","*","+"],["*","+","+","-","*","-"],["*","+","+","-","+","*"],["*","+","+","-","+","+"],["*","+","+","-","+","-"],["*","+","+","-","-","*"],["*","+","+","-","-","+"],["*","+","+","-","-","-"],["*","+","-","*","*","*"],["*","+","-","*","*","+"],["*","+","-","*","*","-"],["*","+","-","*","+","*"],["*","+","-","*","+","+"],["*","+","-","*","+","-"],["*","+","-","*","-","*"],["*","+","-","*","-","+"],["*","+","-","*","-","-"],["*","+","-","+","*","*"],["*","+","-","+","*","+"],["*","+","-","+","*","-"],["*","+","-","+","+","*"],["*","+","-","+","+","+"],["*","+","-","+","+","-"],["*","+","-","+","-","*"],["*","+","-","+","-","+"],["*","+","-","+","-","-"],["*","+","-","-","*","*"],["*","+","-","-","*","+"],["*","+","-","-","*","-"],["*","+","-","-","+","*"],["*","+","-","-","+","+"],["*","+","-","-","+","-"],["*","+","-","-","-","*"],["*","+","-","-","-","+"],["*","+","-","-","-","-"],["*","-","*","*","*","*"],["*","-","*","*","*","+"],["*","-","*","*","*","-"],["*","-","*","*","+","*"],["*","-","*","*","+","+"],["*","-","*","*","+","-"],["*","-","*","*","-","*"],["*","-","*","*","-","+"],["*","-","*","*","-","-"],["*","-","*","+","*","*"],["*","-","*","+","*","+"],["*","-","*","+","*","-"],["*","-","*","+","+","*"],["*","-","*","+","+","+"],["*","-","*","+","+","-"],["*","-","*","+","-","*"],["*","-","*","+","-","+"],["*","-","*","+","-","-"],["*","-","*","-","*","*"],["*","-","*","-","*","+"],["*","-","*","-","*","-"],["*","-","*","-","+","*"],["*","-","*","-","+","+"],["*","-","*","-","+","-"],["*","-","*","-","-","*"],["*","-","*","-","-","+"],["*","-","*","-","-","-"],["*","-","+","*","*","*"],["*","-","+","*","*","+"],["*","-","+","*","*","-"],["*","-","+","*","+","*"],["*","-","+","*","+","+"],["*","-","+","*","+","-"],["*","-","+","*","-","*"],["*","-","+","*","-","+"],["*","-","+","*","-","-"],["*","-","+","+","*","*"],["*","-","+","+","*","+"],["*","-","+","+","*","-"],["*","-","+","+","+","*"],["*","-","+","+","+","+"],["*","-","+","+","+","-"],["*","-","+","+","-","*"],["*","-","+","+","-","+"],["*","-","+","+","-","-"],["*","-","+","-","*","*"],["*","-","+","-","*","+"],["*","-","+","-","*","-"],["*","-","+","-","+","*"],["*","-","+","-","+","+"],["*","-","+","-","+","-"],["*","-","+","-","-","*"],["*","-","+","-","-","+"],["*","-","+","-","-","-"],["*","-","-","*","*","*"],["*","-","-","*","*","+"],["*","-","-","*","*","-"],["*","-","-","*","+","*"],["*","-","-","*","+","+"],["*","-","-","*","+","-"],["*","-","-","*","-","*"],["*","-","-","*","-","+"],["*","-","-","*","-","-"],["*","-","-","+","*","*"],["*","-","-","+","*","+"],["*","-","-","+","*","-"],["*","-","-","+","+","*"],["*","-","-","+","+","+"],["*","-","-","+","+","-"],["*","-","-","+","-","*"],["*","-","-","+","-","+"],["*","-","-","+","-","-"],["*","-","-","-","*","*"],["*","-","-","-","*","+"],["*","-","-","-","*","-"],["*","-","-","-","+","*"],["*","-","-","-","+","+"],["*","-","-","-","+","-"],["*","-","-","-","-","*"],["*","-","-","-","-","+"],["*","-","-","-","-","-"],["+","*","*","*","*","*"],["+","*","*","*","*","+"],["+","*","*","*","*","-"],["+","*","*","*","+","*"],["+","*","*","*","+","+"],["+","*","*","*","+","-"],["+","*","*","*","-","*"],["+","*","*","*","-","+"],["+","*","*","*","-","-"],["+","*","*","+","*","*"],["+","*","*","+","*","+"],["+","*","*","+","*","-"],["+","*","*","+","+","*"],["+","*","*","+","+","+"],["+","*","*","+","+","-"],["+","*","*","+","-","*"],["+","*","*","+","-","+"],["+","*","*","+","-","-"],["+","*","*","-","*","*"],["+","*","*","-","*","+"],["+","*","*","-","*","-"],["+","*","*","-","+","*"],["+","*","*","-","+","+"],["+","*","*","-","+","-"],["+","*","*","-","-","*"],["+","*","*","-","-","+"],["+","*","*","-","-","-"],["+","*","+","*","*","*"],["+","*","+","*","*","+"],["+","*","+","*","*","-"],["+","*","+","*","+","*"],["+","*","+","*","+","+"],["+","*","+","*","+","-"],["+","*","+","*","-","*"],["+","*","+","*","-","+"],["+","*","+","*","-","-"],["+","*","+","+","*","*"],["+","*","+","+","*","+"],["+","*","+","+","*","-"],["+","*","+","+","+","*"],["+","*","+","+","+","+"],["+","*","+","+","+","-"],["+","*","+","+","-","*"],["+","*","+","+","-","+"],["+","*","+","+","-","-"],["+","*","+","-","*","*"],["+","*","+","-","*","+"],["+","*","+","-","*","-"],["+","*","+","-","+","*"],["+","*","+","-","+","+"],["+","*","+","-","+","-"],["+","*","+","-","-","*"],["+","*","+","-","-","+"],["+","*","+","-","-","-"],["+","*","-","*","*","*"],["+","*","-","*","*","+"],["+","*","-","*","*","-"],["+","*","-","*","+","*"],["+","*","-","*","+","+"],["+","*","-","*","+","-"],["+","*","-","*","-","*"],["+","*","-","*","-","+"],["+","*","-","*","-","-"],["+","*","-","+","*","*"],["+","*","-","+","*","+"],["+","*","-","+","*","-"],["+","*","-","+","+","*"],["+","*","-","+","+","+"],["+","*","-","+","+","-"],["+","*","-","+","-","*"],["+","*","-","+","-","+"],["+","*","-","+","-","-"],["+","*","-","-","*","*"],["+","*","-","-","*","+"],["+","*","-","-","*","-"],["+","*","-","-","+","*"],["+","*","-","-","+","+"],["+","*","-","-","+","-"],["+","*","-","-","-","*"],["+","*","-","-","-","+"],["+","*","-","-","-","-"],["+","+","*","*","*","*"],["+","+","*","*","*","+"],["+","+","*","*","*","-"],["+","+","*","*","+","*"],["+","+","*","*","+","+"],["+","+","*","*","+","-"],["+","+","*","*","-","*"],["+","+","*","*","-","+"],["+","+","*","*","-","-"],["+","+","*","+","*","*"],["+","+","*","+","*","+"],["+","+","*","+","*","-"],["+","+","*","+","+","*"],["+","+","*","+","+","+"],["+","+","*","+","+","-"],["+","+","*","+","-","*"],["+","+","*","+","-","+"],["+","+","*","+","-","-"],["+","+","*","-","*","*"],["+","+","*","-","*","+"],["+","+","*","-","*","-"],["+","+","*","-","+","*"],["+","+","*","-","+","+"],["+","+","*","-","+","-"],["+","+","*","-","-","*"],["+","+","*","-","-","+"],["+","+","*","-","-","-"],["+","+","+","*","*","*"],["+","+","+","*","*","+"],["+","+","+","*","*","-"],["+","+","+","*","+","*"],["+","+","+","*","+","+"],["+","+","+","*","+","-"],["+","+","+","*","-","*"],["+","+","+","*","-","+"],["+","+","+","*","-","-"],["+","+","+","+","*","*"],["+","+","+","+","*","+"],["+","+","+","+","*","-"],["+","+","+","+","+","*"],["+","+","+","+","+","+"],["+","+","+","+","+","-"],["+","+","+","+","-","*"],["+","+","+","+","-","+"],["+","+","+","+","-","-"],["+","+","+","-","*","*"],["+","+","+","-","*","+"],["+","+","+","-","*","-"],["+","+","+","-","+","*"],["+","+","+","-","+","+"],["+","+","+","-","+","-"],["+","+","+","-","-","*"],["+","+","+","-","-","+"],["+","+","+","-","-","-"],["+","+","-","*","*","*"],["+","+","-","*","*","+"],["+","+","-","*","*","-"],["+","+","-","*","+","*"],["+","+","-","*","+","+"],["+","+","-","*","+","-"],["+","+","-","*","-","*"],["+","+","-","*","-","+"],["+","+","-","*","-","-"],["+","+","-","+","*","*"],["+","+","-","+","*","+"],["+","+","-","+","*","-"],["+","+","-","+","+","*"],["+","+","-","+","+","+"],["+","+","-","+","+","-"],["+","+","-","+","-","*"],["+","+","-","+","-","+"],["+","+","-","+","-","-"],["+","+","-","-","*","*"],["+","+","-","-","*","+"],["+","+","-","-","*","-"],["+","+","-","-","+","*"],["+","+","-","-","+","+"],["+","+","-","-","+","-"],["+","+","-","-","-","*"],["+","+","-","-","-","+"],["+","+","-","-","-","-"],["+","-","*","*","*","*"],["+","-","*","*","*","+"],["+","-","*","*","*","-"],["+","-","*","*","+","*"],["+","-","*","*","+","+"],["+","-","*","*","+","-"],["+","-","*","*","-","*"],["+","-","*","*","-","+"],["+","-","*","*","-","-"],["+","-","*","+","*","*"],["+","-","*","+","*","+"],["+","-","*","+","*","-"],["+","-","*","+","+","*"],["+","-","*","+","+","+"],["+","-","*","+","+","-"],["+","-","*","+","-","*"],["+","-","*","+","-","+"],["+","-","*","+","-","-"],["+","-","*","-","*","*"],["+","-","*","-","*","+"],["+","-","*","-","*","-"],["+","-","*","-","+","*"],["+","-","*","-","+","+"],["+","-","*","-","+","-"],["+","-","*","-","-","*"],["+","-","*","-","-","+"],["+","-","*","-","-","-"],["+","-","+","*","*","*"],["+","-","+","*","*","+"],["+","-","+","*","*","-"],["+","-","+","*","+","*"],["+","-","+","*","+","+"],["+","-","+","*","+","-"],["+","-","+","*","-","*"],["+","-","+","*","-","+"],["+","-","+","*","-","-"],["+","-","+","+","*","*"],["+","-","+","+","*","+"],["+","-","+","+","*","-"],["+","-","+","+","+","*"],["+","-","+","+","+","+"],["+","-","+","+","+","-"],["+","-","+","+","-","*"],["+","-","+","+","-","+"],["+","-","+","+","-","-"],["+","-","+","-","*","*"],["+","-","+","-","*","+"],["+","-","+","-","*","-"],["+","-","+","-","+","*"],["+","-","+","-","+","+"],["+","-","+","-","+","-"],["+","-","+","-","-","*"],["+","-","+","-","-","+"],["+","-","+","-","-","-"],["+","-","-","*","*","*"],["+","-","-","*","*","+"],["+","-","-","*","*","-"],["+","-","-","*","+","*"],["+","-","-","*","+","+"],["+","-","-","*","+","-"],["+","-","-","*","-","*"],["+","-","-","*","-","+"],["+","-","-","*","-","-"],["+","-","-","+","*","*"],["+","-","-","+","*","+"],["+","-","-","+","*","-"],["+","-","-","+","+","*"],["+","-","-","+","+","+"],["+","-","-","+","+","-"],["+","-","-","+","-","*"],["+","-","-","+","-","+"],["+","-","-","+","-","-"],["+","-","-","-","*","*"],["+","-","-","-","*","+"],["+","-","-","-","*","-"],["+","-","-","-","+","*"],["+","-","-","-","+","+"],["+","-","-","-","+","-"],["+","-","-","-","-","*"],["+","-","-","-","-","+"],["+","-","-","-","-","-"],["-","*","*","*","*","*"],["-","*","*","*","*","+"],["-","*","*","*","*","-"],["-","*","*","*","+","*"],["-","*","*","*","+","+"],["-","*","*","*","+","-"],["-","*","*","*","-","*"],["-","*","*","*","-","+"],["-","*","*","*","-","-"],["-","*","*","+","*","*"],["-","*","*","+","*","+"],["-","*","*","+","*","-"],["-","*","*","+","+","*"],["-","*","*","+","+","+"],["-","*","*","+","+","-"],["-","*","*","+","-","*"],["-","*","*","+","-","+"],["-","*","*","+","-","-"],["-","*","*","-","*","*"],["-","*","*","-","*","+"],["-","*","*","-","*","-"],["-","*","*","-","+","*"],["-","*","*","-","+","+"],["-","*","*","-","+","-"],["-","*","*","-","-","*"],["-","*","*","-","-","+"],["-","*","*","-","-","-"],["-","*","+","*","*","*"],["-","*","+","*","*","+"],["-","*","+","*","*","-"],["-","*","+","*","+","*"],["-","*","+","*","+","+"],["-","*","+","*","+","-"],["-","*","+","*","-","*"],["-","*","+","*","-","+"],["-","*","+","*","-","-"],["-","*","+","+","*","*"],["-","*","+","+","*","+"],["-","*","+","+","*","-"],["-","*","+","+","+","*"],["-","*","+","+","+","+"],["-","*","+","+","+","-"],["-","*","+","+","-","*"],["-","*","+","+","-","+"],["-","*","+","+","-","-"],["-","*","+","-","*","*"],["-","*","+","-","*","+"],["-","*","+","-","*","-"],["-","*","+","-","+","*"],["-","*","+","-","+","+"],["-","*","+","-","+","-"],["-","*","+","-","-","*"],["-","*","+","-","-","+"],["-","*","+","-","-","-"],["-","*","-","*","*","*"],["-","*","-","*","*","+"],["-","*","-","*","*","-"],["-","*","-","*","+","*"],["-","*","-","*","+","+"],["-","*","-","*","+","-"],["-","*","-","*","-","*"],["-","*","-","*","-","+"],["-","*","-","*","-","-"],["-","*","-","+","*","*"],["-","*","-","+","*","+"],["-","*","-","+","*","-"],["-","*","-","+","+","*"],["-","*","-","+","+","+"],["-","*","-","+","+","-"],["-","*","-","+","-","*"],["-","*","-","+","-","+"],["-","*","-","+","-","-"],["-","*","-","-","*","*"],["-","*","-","-","*","+"],["-","*","-","-","*","-"],["-","*","-","-","+","*"],["-","*","-","-","+","+"],["-","*","-","-","+","-"],["-","*","-","-","-","*"],["-","*","-","-","-","+"],["-","*","-","-","-","-"],["-","+","*","*","*","*"],["-","+","*","*","*","+"],["-","+","*","*","*","-"],["-","+","*","*","+","*"],["-","+","*","*","+","+"],["-","+","*","*","+","-"],["-","+","*","*","-","*"],["-","+","*","*","-","+"],["-","+","*","*","-","-"],["-","+","*","+","*","*"],["-","+","*","+","*","+"],["-","+","*","+","*","-"],["-","+","*","+","+","*"],["-","+","*","+","+","+"],["-","+","*","+","+","-"],["-","+","*","+","-","*"],["-","+","*","+","-","+"],["-","+","*","+","-","-"],["-","+","*","-","*","*"],["-","+","*","-","*","+"],["-","+","*","-","*","-"],["-","+","*","-","+","*"],["-","+","*","-","+","+"],["-","+","*","-","+","-"],["-","+","*","-","-","*"],["-","+","*","-","-","+"],["-","+","*","-","-","-"],["-","+","+","*","*","*"],["-","+","+","*","*","+"],["-","+","+","*","*","-"],["-","+","+","*","+","*"],["-","+","+","*","+","+"],["-","+","+","*","+","-"],["-","+","+","*","-","*"],["-","+","+","*","-","+"],["-","+","+","*","-","-"],["-","+","+","+","*","*"],["-","+","+","+","*","+"],["-","+","+","+","*","-"],["-","+","+","+","+","*"],["-","+","+","+","+","+"],["-","+","+","+","+","-"],["-","+","+","+","-","*"],["-","+","+","+","-","+"],["-","+","+","+","-","-"],["-","+","+","-","*","*"],["-","+","+","-","*","+"],["-","+","+","-","*","-"],["-","+","+","-","+","*"],["-","+","+","-","+","+"],["-","+","+","-","+","-"],["-","+","+","-","-","*"],["-","+","+","-","-","+"],["-","+","+","-","-","-"],["-","+","-","*","*","*"],["-","+","-","*","*","+"],["-","+","-","*","*","-"],["-","+","-","*","+","*"],["-","+","-","*","+","+"],["-","+","-","*","+","-"],["-","+","-","*","-","*"],["-","+","-","*","-","+"],["-","+","-","*","-","-"],["-","+","-","+","*","*"],["-","+","-","+","*","+"],["-","+","-","+","*","-"],["-","+","-","+","+","*"],["-","+","-","+","+","+"],["-","+","-","+","+","-"],["-","+","-","+","-","*"],["-","+","-","+","-","+"],["-","+","-","+","-","-"],["-","+","-","-","*","*"],["-","+","-","-","*","+"],["-","+","-","-","*","-"],["-","+","-","-","+","*"],["-","+","-","-","+","+"],["-","+","-","-","+","-"],["-","+","-","-","-","*"],["-","+","-","-","-","+"],["-","+","-","-","-","-"],["-","-","*","*","*","*"],["-","-","*","*","*","+"],["-","-","*","*","*","-"],["-","-","*","*","+","*"],["-","-","*","*","+","+"],["-","-","*","*","+","-"],["-","-","*","*","-","*"],["-","-","*","*","-","+"],["-","-","*","*","-","-"],["-","-","*","+","*","*"],["-","-","*","+","*","+"],["-","-","*","+","*","-"],["-","-","*","+","+","*"],["-","-","*","+","+","+"],["-","-","*","+","+","-"],["-","-","*","+","-","*"],["-","-","*","+","-","+"],["-","-","*","+","-","-"],["-","-","*","-","*","*"],["-","-","*","-","*","+"],["-","-","*","-","*","-"],["-","-","*","-","+","*"],["-","-","*","-","+","+"],["-","-","*","-","+","-"],["-","-","*","-","-","*"],["-","-","*","-","-","+"],["-","-","*","-","-","-"],["-","-","+","*","*","*"],["-","-","+","*","*","+"],["-","-","+","*","*","-"],["-","-","+","*","+","*"],["-","-","+","*","+","+"],["-","-","+","*","+","-"],["-","-","+","*","-","*"],["-","-","+","*","-","+"],["-","-","+","*","-","-"],["-","-","+","+","*","*"],["-","-","+","+","*","+"],["-","-","+","+","*","-"],["-","-","+","+","+","*"],["-","-","+","+","+","+"],["-","-","+","+","+","-"],["-","-","+","+","-","*"],["-","-","+","+","-","+"],["-","-","+","+","-","-"],["-","-","+","-","*","*"],["-","-","+","-","*","+"],["-","-","+","-","*","-"],["-","-","+","-","+","*"],["-","-","+","-","+","+"],["-","-","+","-","+","-"],["-","-","+","-","-","*"],["-","-","+","-","-","+"],["-","-","+","-","-","-"],["-","-","-","*","*","*"],["-","-","-","*","*","+"],["-","-","-","*","*","-"],["-","-","-","*","+","*"],["-","-","-","*","+","+"],["-","-","-","*","+","-"],["-","-","-","*","-","*"],["-","-","-","*","-","+"],["-","-","-","*","-","-"],["-","-","-","+","*","*"],["-","-","-","+","*","+"],["-","-","-","+","*","-"],["-","-","-","+","+","*"],["-","-","-","+","+","+"],["-","-","-","+","+","-"],["-","-","-","+","-","*"],["-","-","-","+","-","+"],["-","-","-","+","-","-"],["-","-","-","-","*","*"],["-","-","-","-","*","+"],["-","-","-","-","*","-"],["-","-","-","-","+","*"],["-","-","-","-","+","+"],["-","-","-","-","+","-"],["-","-","-","-","-","*"],["-","-","-","-","-","+"],["-","-","-","-","-","-"]]]] -- cgit From 2be54ff3e7edc826747cd188f6e12cfa7ab75078 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 3 Nov 2025 14:17:34 +0100 Subject: Add Perl and Python solutions for challenge 346 --- challenge-346/lubos-kolouch/README | 36 ++++++------ challenge-346/lubos-kolouch/perl/ch-1.pl | 79 +++++++++++++++++++++++++ challenge-346/lubos-kolouch/perl/ch-2.pl | 92 ++++++++++++++++++++++++++++++ challenge-346/lubos-kolouch/python/ch-1.py | 61 ++++++++++++++++++++ challenge-346/lubos-kolouch/python/ch-2.py | 89 +++++++++++++++++++++++++++++ 5 files changed, 337 insertions(+), 20 deletions(-) create mode 100644 challenge-346/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-346/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-346/lubos-kolouch/python/ch-1.py create mode 100644 challenge-346/lubos-kolouch/python/ch-2.py diff --git a/challenge-346/lubos-kolouch/README b/challenge-346/lubos-kolouch/README index 5b34a3ed40..7f47843cb2 100644 --- a/challenge-346/lubos-kolouch/README +++ b/challenge-346/lubos-kolouch/README @@ -1,30 +1,26 @@ -# Perl Weekly Challenge 345 Solutions +# Perl Weekly Challenge 346 Solutions -Perl and Python implementations for both tasks of Weekly Challenge 345. +Perl and Python implementations for both tasks of Weekly Challenge 346. -## Task 1: Peak Positions +## Task 1: Longest Parenthesis -Identify every index whose value is strictly greater than its immediate -neighbour(s). Endpoints are considered peaks when they exceed their sole -neighbour. +Determine the length of the longest valid parenthesis substring contained +within an input built from `(` and `)`. -- **Perl (`perl/ch-1.pl`)**: Validates the integer list with `Type::Params`, - then performs a single pass comparing each value against the surrounding - entries to collect the peak indices. -- **Python (`python/ch-1.py`)**: Mirrors the linear scan with explicit type - hints and a set of `unittest` cases for the provided examples. +- **Perl (`perl/ch-1.pl`)**: Uses a stack of indices with type-checked input + to track unmatched parentheses and compute the maximum span. +- **Python (`python/ch-1.py`)**: Mirrors the stack-based pass with explicit + type hints and `unittest` coverage for the specification data. -## Task 2: Last Visitor +## Task 2: Magic Expression -Process a mixed list of positive integers and `-1` markers. Each integer is -stored at the front of a queue, while every `-1` requests the value `x` steps -back in that queue, where `x` counts consecutive `-1` entries. +Insert `+`, `-`, and `*` between the digits of the supplied string so the +expression evaluates to the target integer. -- **Perl (`perl/ch-2.pl`)**: Maintains the visitor queue with input validation - and tracks the current `-1` run to select the right lookup or `-1` when the - queue is too short. -- **Python (`python/ch-2.py`)**: Provides the same behaviour with typed - functions and unit tests aligned with the specification examples. +- **Perl (`perl/ch-2.pl`)**: Performs depth-first search with precedence-aware + bookkeeping, returning every lexicographically sorted solution. +- **Python (`python/ch-2.py`)**: Applies the same backtracking strategy with + typed helpers and unit tests bound to the official examples. ## Running the Solutions diff --git a/challenge-346/lubos-kolouch/perl/ch-1.pl b/challenge-346/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5319b3468e --- /dev/null +++ b/challenge-346/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(signatures state); +no warnings qw(experimental::signatures); +use Type::Params qw(compile); +use Types::Standard qw(StrMatch); +use Test::More; + +=pod + +=head1 PURPOSE + +Compute the length of the longest valid parenthesis substring contained in +an input string made up exclusively of opening and closing parentheses. + +=head1 ALGORITHM + +We validate the input using L to ensure it only contains the +characters C<(>) and C<)>. A stack of indices tracks unmatched opening +parentheses, seeded with C<-1> as a sentinel to simplify length +calculations. While scanning the string: + +=over 4 + +=item * +Push the index of every opening parenthesis. + +=item * +Pop for each closing parenthesis, measuring the span back to the previous +unmatched position. When the stack empties we push the current index to mark +the start of the next candidate segment. + +=back + +The maximum span observed is the required result. + +=cut + +## no critic (Subroutines::ProhibitSubroutinePrototypes) +sub longest_parenthesis ($str_input) { + state $check = compile( StrMatch [qr/\A[()]*\z/] ); + my ($str) = $check->($str_input); + + my $length = length $str; + return 0 if $length == 0; + + my @stack = (-1); + my $longest = 0; + + for my $idx ( 0 .. $length - 1 ) { + my $char = substr $str, $idx, 1; + + if ( $char eq '(' ) { + push @stack, $idx; + next; + } + + pop @stack; + if (@stack) { + my $candidate = $idx - $stack[-1]; + $longest = $candidate if $candidate > $longest; + } + else { + push @stack, $idx; + } + } + + return $longest; +} + +# Example tests provided by the specification. +is( longest_parenthesis('(()())'), 6, 'Example 1' ); +is( longest_parenthesis(')()())'), 4, 'Example 2' ); +is( longest_parenthesis('((()))()(((()'), 8, 'Example 3' ); +is( longest_parenthesis('))))((()('), 2, 'Example 4' ); +is( longest_parenthesis('()(()'), 2, 'Example 5' ); + +done_testing(); diff --git a/challenge-346/lubos-kolouch/perl/ch-2.pl b/challenge-346/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1fd5d5753c --- /dev/null +++ b/challenge-346/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,92 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(signatures state); +no warnings qw(experimental::signatures); +use Type::Params qw(compile); +use Types::Standard qw(Int StrMatch); +use Test::More; + +=pod + +=head1 PURPOSE + +Generate all expressions that insert the binary operators C<+>, C<->, and +C<*> between the digits of a string so that the resulting arithmetic equals +a target integer. + +=head1 ALGORITHM + +Input validation employs L ensuring a digit-only string and an +integer target. The search performs depth-first backtracking over every +possible digit partition. For each partition we append the next operand with +each operator, tracking both the cumulative value and the most recent +operand to preserve multiplication precedence: + +=over 4 + +=item * +Addition and subtraction directly adjust the running total. + +=item * +Multiplication rewinds the previous operand from the total and replaces it +with the product, effectively emulating standard precedence without parsing. + +=back + +Numbers containing leading zeros are skipped (except the single digit zero). +All matching expressions are sorted lexicographically before being returned. + +=cut + +## no critic (Subroutines::ProhibitSubroutinePrototypes) +sub magic_expression ( $digits_input, $target_input ) { + state $check = compile( StrMatch [qr/\A\d+\z/], Int ); + my ( $digits, $target ) = $check->( $digits_input, $target_input ); + + my $length = length $digits; + my @results; + + my $search; + $search = sub ( $pos, $expression, $value, $last_operand ) { + if ( $pos == $length ) { + push @results, $expression if $value == $target; + return; + } + + for my $end ( $pos + 1 .. $length ) { + my $chunk = substr $digits, $pos, $end - $pos; + next if length($chunk) > 1 && substr( $chunk, 0, 1 ) eq '0'; + my $number = 0 + $chunk; + + if ( $pos == 0 ) { + $search->( $end, $chunk, $number, $number ); + next; + } + + $search->( $end, "$expression+$chunk", $value + $number, $number ); + $search->( $end, "$expression-$chunk", $value - $number, -$number ); + + my $product = $last_operand * $number; + $search->( $end, "$expression*$chunk", $value - $last_operand + $product, $product ); + } + }; + + $search->( 0, q{}, 0, 0 ); + @results = sort @results; + + return \@results; +} + +# Example tests provided by the specification. +is_deeply( magic_expression( '123', 6 ), [ '1*2*3', '1+2+3' ], 'Example 1' ); +is_deeply( magic_expression( '105', 5 ), [ '1*0+5', '10-5' ], 'Example 2' ); +is_deeply( magic_expression( '232', 8 ), [ '2*3+2', '2+3*2' ], 'Example 3' ); +is_deeply( magic_expression( '1234', 10 ), [ '1*2*3+4', '1+2+3+4' ], 'Example 4' ); +is_deeply( + magic_expression( '1001', 2 ), + [ '1+0*0+1', '1+0+0+1', '1+0-0+1', '1-0*0+1', '1-0+0+1', '1-0-0+1' ], + 'Example 5' +); + +done_testing(); diff --git a/challenge-346/lubos-kolouch/python/ch-1.py b/challenge-346/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..c5a60ae383 --- /dev/null +++ b/challenge-346/lubos-kolouch/python/ch-1.py @@ -0,0 +1,61 @@ +"""Longest Parenthesis Challenge Solution. + +Determine the length of the longest valid parenthesis substring within an +input consisting solely of opening and closing parentheses. +""" + +from __future__ import annotations + +import unittest + + +def longest_parenthesis(paren_string: str) -> int: + """Return the length of the longest well-formed parenthesis substring. + + The algorithm scans the string once while maintaining a stack of indices + for unmatched opening parentheses. A sentinel index of ``-1`` simplifies + length calculations when a balanced substring is identified. + """ + stack: list[int] = [-1] + longest = 0 + + for index, char in enumerate(paren_string): + if char not in ("(", ")"): + msg = "Input must contain only '(' and ')'." + raise ValueError(msg) + + if char == "(": + stack.append(index) + continue + + stack.pop() + if stack: + candidate = index - stack[-1] + longest = max(longest, candidate) + else: + stack.append(index) + + return longest + + +class LongestParenthesisTests(unittest.TestCase): + """Unit tests restricted to the specification examples.""" + + def test_example_1(self) -> None: + self.assertEqual(longest_parenthesis("(()())"), 6) + + def test_example_2(self) -> None: + self.assertEqual(longest_parenthesis(")()())"), 4) + + def test_example_3(self) -> None: + self.assertEqual(longest_parenthesis("((()))()(((()"), 8) + + def test_example_4(self) -> None: + self.assertEqual(longest_parenthesis("))))((()("), 2) + + def test_example_5(self) -> None: + self.assertEqual(longest_parenthesis("()(()"), 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-346/lubos-kolouch/python/ch-2.py b/challenge-346/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..6196ec7e3e --- /dev/null +++ b/challenge-346/lubos-kolouch/python/ch-2.py @@ -0,0 +1,89 @@ +"""Magic Expression Challenge Solution. + +Insert binary operators between the digits of a string so that the resulting +expression evaluates to a target integer. +""" + +from __future__ import annotations + +import unittest + + +def magic_expression(digits: str, target: int) -> list[str]: + """Return sorted expressions that evaluate to ``target``. + + The function performs a depth-first search over every partition of the + digit string, appending each operator while maintaining the cumulative + value and most recent operand. Multiplication adjusts the running total + to honour standard operator precedence without relying on ``eval``. + """ + if not digits.isdigit(): + msg = "Input must consist solely of decimal digits." + raise ValueError(msg) + + results: list[str] = [] + length = len(digits) + + def backtrack(index: int, expression: str, value: int, + last_operand: int) -> None: + if index == length: + if value == target: + results.append(expression) + return + + for end in range(index + 1, length + 1): + chunk = digits[index:end] + if len(chunk) > 1 and chunk[0] == "0": + break + number = int(chunk) + + if index == 0: + backtrack(end, chunk, number, number) + continue + + backtrack(end, f"{expression}+{chunk}", value + number, number) + backtrack(end, f"{expression}-{chunk}", value - number, -number) + product = last_operand * number + backtrack( + end, + f"{expression}*{chunk}", + value - last_operand + product, + product, + ) + + backtrack(0, "", 0, 0) + results.sort() + return results + + +class MagicExpressionTests(unittest.TestCase): + """Unit tests restricted to the specification examples.""" + + def test_example_1(self) -> None: + self.assertEqual(magic_expression("123", 6), ["1*2*3", "1+2+3"]) + + def test_example_2(self) -> None: + self.assertEqual(magic_expression("105", 5), ["1*0+5", "10-5"]) + + def test_example_3(self) -> None: + self.assertEqual(magic_expression("232", 8), ["2*3+2", "2+3*2"]) + + def test_example_4(self) -> None: + self.assertEqual(magic_expression("1234", 10), ["1*2*3+4", "1+2+3+4"]) + + def test_example_5(self) -> None: + self.assertEqual( + magic_expression("1001", 2), + [ + "1+0*0+1", + "1+0+0+1", + "1+0-0+1", + "1-0*0+1", + "1-0+0+1", + "1-0-0+1", + ], + ) + + +if __name__ == "__main__": + unittest.main() -- cgit From 265a950c3945b4fbcc333241cc3530b34e7264c9 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 3 Nov 2025 15:17:40 +0100 Subject: Solve 346: Longest Parenthesis & Magic Expression by E. Choroba --- challenge-346/e-choroba/perl/ch-1.pl | 35 +++++++++++++++++++++ challenge-346/e-choroba/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 challenge-346/e-choroba/perl/ch-1.pl create mode 100755 challenge-346/e-choroba/perl/ch-2.pl diff --git a/challenge-346/e-choroba/perl/ch-1.pl b/challenge-346/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..44c0f664a4 --- /dev/null +++ b/challenge-346/e-choroba/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub longest_parenthesis($str) { + $str =~ s/^\)+//; + $str =~ s/\(+$//; + for my $length (reverse 2 .. length $str) { + FROM: + for my $from (0 .. length($str) - $length) { + my $depth = 0; + for my $pos ($from .. $from + $length - 1) { + $depth += substr($str, $pos, 1) eq '(' ? 1 : -1; + next FROM if $depth < 0; + } + return $length if 0 == $depth; + } + } + return 0 +} + +use Test::More tests => 5 + 3; + +is longest_parenthesis('(()())'), 6, 'Example 1'; +is longest_parenthesis(')()())'), 4, 'Example 2'; +is longest_parenthesis('((()))()(((()'), 8, 'Example 3'; +is longest_parenthesis('))))((()('), 2, 'Example 4'; +is longest_parenthesis('()(()'), 2, 'Example 5'; + +is longest_parenthesis(''), 0, 'Empty string'; +is longest_parenthesis('('), 0, 'Single char opening'; +is longest_parenthesis(')'), 0, 'Single char closing'; +is longest_parenthesis('))))(((('), 0, 'Zero'; +is longest_parenthesis('(((())(()))((((())()()())'), 12, 'Larger'; diff --git a/challenge-346/e-choroba/perl/ch-2.pl b/challenge-346/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..9acac972ca --- /dev/null +++ b/challenge-346/e-choroba/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; +use experimental qw( signatures ); + +use Algorithm::Combinatorics qw{ variations_with_repetition }; +use List::Util qw{ mesh }; + +sub magic_expression($str, $target) { + my @digits = split //, $str; + # We use "" for no operator. + my $iter = variations_with_repetition(['+', '-', '*', ""], @digits - 1); + my @correct; + while (my $ops = $iter->next) { + my $expression = join "", mesh(\@digits, [@$ops, ""]); + next if $expression =~ /\b0[0-9]/; # Only 0 can start with a 0. + + # Implement subtraction as addition of a negative number. + my $eval = $expression =~ s/-/+-/gr; + + # First solve all multiplications. + $eval =~ s/([0-9]+)\*([0-9]+)/$1*$2/ge while $eval =~ /\*/; + + # Then do additions. + $eval =~ s/(-?[0-9]+)\+(-?[0-9]+)/$1+$2/ge while $eval =~ /\+/; + + push @correct, $expression if $target == $eval; + } + return @correct +} + +use Test2::V0; +plan(5 + 1); + +# It seems we can't insert more than one operator, otherwise 1*-2*-3 +# would have been a solution, too. +is [magic_expression('123', 6)], + bag { item $_ for '1*2*3', '1+2+3'; end }, + 'Example 1'; + +is [magic_expression('105', 5)], + bag { item $_ for '1*0+5', '10-5'; end }, + 'Example 2'; + +is [magic_expression('232', 8)], + bag { item $_ for '2*3+2', '2+3*2'; end }, + 'Example 3'; + +is [magic_expression('1234', 10)], + bag { item $_ for '1*2*3+4', '1+2+3+4'; end }, + 'Example 4'; + +is [magic_expression('1001', 2)], + bag { item $_ for '1+0*0+1', '1+0+0+1', '1+0-0+1', + '1-0*0+1', '1-0+0+1', '1-0-0+1'; + end }, + 'Example 5'; + +my @large = magic_expression('123456789', 250); +is scalar @large, 8, 'Large'; -- cgit From 613e50acdc8aec58bca6c1da7c9509348be0060a Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Mon, 3 Nov 2025 16:08:39 +0100 Subject: Challenge 346 --- challenge-346/mahnkong/perl/ch-1.pl | 42 ++++++++++++++++++++++ challenge-346/mahnkong/perl/ch-2.pl | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 challenge-346/mahnkong/perl/ch-1.pl create mode 100644 challenge-346/mahnkong/perl/ch-2.pl diff --git a/challenge-346/mahnkong/perl/ch-1.pl b/challenge-346/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..7cfa9da985 --- /dev/null +++ b/challenge-346/mahnkong/perl/ch-1.pl @@ -0,0 +1,42 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($str) { + my @candidates; + foreach my $c (split //, $str) { + my $open = 0; + if ($c eq '(') { + push @candidates, { + open => 0, + found => 0, + }; + $open = 1; + } elsif ($c eq ')') { + $open = -1; + } + foreach my $candidate (@candidates) { + next unless exists $candidate->{open}; + $candidate->{found} += 1; + $candidate->{open} += $open; + if ($candidate->{open} == 0) { + push @candidates, { + found => $candidate->{found}, + } + } + } + } + + my $result = 0; + foreach my $candidate (@candidates) { + $result = $candidate->{found} if ! exists $candidate->{open} && $candidate->{found} > $result; + } + return $result; +} + +is(run('(()())'), 6, "Example 1"); +is(run(')()())'), 4, "Example 2"); +is(run('((()))()(((()'), 8, "Example 3"); +is(run('))))((()('), 2, "Example 4"); +is(run('()(()'), 2, "Example 5"); diff --git a/challenge-346/mahnkong/perl/ch-2.pl b/challenge-346/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..d0479e9190 --- /dev/null +++ b/challenge-346/mahnkong/perl/ch-2.pl @@ -0,0 +1,71 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +my @operations = ('*', '+', '-'); + +sub all_substring_splits($str, $prefix) { + $prefix //= []; + my @results; + + if ($str eq '') { + push @results, [@$prefix]; + return @results; + } + + for my $i (1 .. length($str)) { + my $first = substr($str, 0, $i); + my $rest = substr($str, $i); + push @results, all_substring_splits($rest, [@$prefix, $first]); + } + + return @results; +} + +sub add_operators($list, @result) { + if (scalar(@$list)) { + my $left = shift @$list; + my @local; + if (scalar(@result)) { + foreach my $r (@result) { + if (scalar(@$list)) { + foreach my $operator (@operations) { + push @local, "$r$left$operator"; + } + } else { + push @local, "$r$left"; + } + } + } else { + foreach my $operator (@operations) { + push @local, "$left$operator"; + } + } + @result = @local; + + foreach my $part (@$list) { + @result = add_operators($list, @result); + } + } + return @result; +} + + +sub run($str, $target) { + my @result; + foreach my $candidate_list (all_substring_splits($str, undef)) { + next if scalar(@$candidate_list == 1); + next if grep /^0\d/, @$candidate_list; + foreach my $eval (add_operators($candidate_list)) { + push @result, $eval if $target == eval $eval; + } + } + return \@result; +} + +is_deeply(run("123", 6), ["1*2*3", "1+2+3"], "Example 1"); +is_deeply(run("105", 5), ["1*0+5", "10-5"], "Example 2"); +is_deeply(run("232", 8), ["2*3+2", "2+3*2"], "Example 3"); +is_deeply(run("1234", 10), ["1*2*3+4", "1+2+3+4"], "Example 4"); +is_deeply(run("1001", 2), ["1+0*0+1", "1+0+0+1", "1+0-0+1", "1-0*0+1", "1-0+0+1", "1-0-0+1"], "Example 5"); -- cgit From c604bd6bd4adddfa52724ea52782fddd50dada0f Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 3 Nov 2025 11:11:13 -0500 Subject: Week 346 --- challenge-346/zapwai/perl/ch-1.pl | 112 ++++++++++++++++++++++++++++++++++++++ challenge-346/zapwai/perl/ch-2.pl | 44 +++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 challenge-346/zapwai/perl/ch-1.pl create mode 100644 challenge-346/zapwai/perl/ch-2.pl diff --git a/challenge-346/zapwai/perl/ch-1.pl b/challenge-346/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..7c9bbd6359 --- /dev/null +++ b/challenge-346/zapwai/perl/ch-1.pl @@ -0,0 +1,112 @@ +use v5.38; +sub proc($str) { + say "Input: \$str = $str"; + my (@open, @valid); + my @p = split '', $str; + for my $i (0 .. $#p) { + my $char = $p[$i]; + next if (@open == 0 && $char eq ')'); + if (@open == 0) { + push @open, $i; + } else { + if ($char eq ')') { + push @valid, pop(@open); + } else { + push @open, $i; + } + } + } + @valid = sort {$a <=> $b} @valid; + my $diff = 0; + my $maxdiff = 0; + for my $i (0 .. $#valid - 1) { + $diff = $valid[$i+1] - $valid[$i]; + if ($diff > $maxdiff) { + $maxdiff = $diff; + } + } + my $o = ""; + if ($maxdiff < 3) { + $o = 2 * scalar @valid; + } else { + # Each nest increases valid skip-length by one. (Default is 2) + my $skip_len = 2; + + my @val; # a multi-array for the non-contiguous case + my @curr; # the current contiguous block, ref pushed to val + for my $i (0 .. $#valid - 1) { + push @curr, $valid[0] if ($i == 0); + $diff = $valid[$i+1] - $valid[$i]; + + if ($diff > $skip_len) { # non-contiguous case + my @a = @curr; + push @val, \@a; + @curr = (); + $skip_len = 2; + } else { + $skip_len -= $diff - 2; + } + push @curr, $valid[$i+1]; + } + push @val, \@curr; + + my $max_len = 0; + for my $r (@val) { + my $len = scalar @$r; + $max_len = $len if ($len > $max_len); + } + + $o = 2*$max_len; + } + say "Output: $o"; + +} + +my $str = '(()())'; +proc($str); +$str = ')()())'; +proc($str); +$str = '((()))()(((()'; +proc($str); +$str = '))))((()('; +proc($str); +$str = '()(()'; +proc($str); + +# keep an @open array that holds the index of any opening bracket, the left-hand ( +# when a right-hand ) is encountered, you pop the index off of @open and onto @valid. +# [right-hand ) are ignored if @open is empty] + +# You can then double the length of the largest contiguous block in @valid. +# (adjacent indices means nested structure, one-space-away means neighboring pairs) + +# first example valid: {0, 1, 3} -> 6 +# second example valid: {1,3} -> 4 +# third valid : {0,1,2,6, 11} -> 8 +# four valid: {6} -> 2 +# five valid: {0,3} -> 2 + + +# # Some Extra Cases + +# my $str = '(())()'; # {0, 1, 4} -> 6 +# proc($str); +# $str = '(())()()'; # {0, 1, 4, 6} -> 8 +# proc($str); +# $str = '(()(()))()'; # {0, 1, 3, 4, 8} -> 10 +# proc($str); +# $str = '(()(())())'; # {0, 1, 3, 4, 7} -> 10 +# proc($str); + +# # Skip_len decreases when there is an abnormal skip (not a diff of 2, i.e. a closed pair) + +# $str = '(()(())()())'; +# proc($str); +# # {0, 1, 3, 4, 7, 9} -> 12 (skip_len decreases at the 4 to 7 gap.) +# $str = '(()(())())()'; +# proc($str); +# # {0, 1, 3, 4, 7, 10} -> 12 (skip_len decreases twice) +# $str = '(()(())()))()'; +# proc($str); +# # {0, 1, 3, 4, 7, 11} -> 10 (gap is larger than skip_len, non-contiguous case) + diff --git a/challenge-346/zapwai/perl/ch-2.pl b/challenge-346/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..50ff5a55c1 --- /dev/null +++ b/challenge-346/zapwai/perl/ch-2.pl @@ -0,0 +1,44 @@ +use v5.38; +sub proc($str, $target) { + say "Input: $str, $target"; + my @num = split '', $str; + + # This is the single digits case only. + # The general version would include (len choose n) cases as n runs from 2 to len - 1 + # I might upload a more general version later in the week, if I get a chance + + my @out; + my @s = ('+', '-', '*'); + + for my $symb (@s) { + push @out, $num[0].$symb; + } + + for my $i (1 .. $#num - 1) { + my @a; + for my $symb (@s) { + push @a, $_.$num[$i].$symb for (@out); + } + @out = @a; + } + + my @a; + push @a, $_.$num[$#num] for (@out); + say "Output: "; + for my $a (@a) { + my $ans = eval($a); + say "\t$a = ".$ans if ($ans == $target); + } +