diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2025-11-10 14:39:07 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2025-11-10 14:39:07 +0000 |
| commit | 00f12b64ee72fe59f860dcfd0c471b2d915b50cd (patch) | |
| tree | 39bf33aa309c721141200cce81c8f81e73cfa875 /challenge-346 | |
| parent | 29f384236e56f32faddf1bd726e90447f21ed179 (diff) | |
| download | perlweeklychallenge-club-00f12b64ee72fe59f860dcfd0c471b2d915b50cd.tar.gz perlweeklychallenge-club-00f12b64ee72fe59f860dcfd0c471b2d915b50cd.tar.bz2 perlweeklychallenge-club-00f12b64ee72fe59f860dcfd0c471b2d915b50cd.zip | |
w346 - Task 1 & 2 (Perl)
Diffstat (limited to 'challenge-346')
| -rwxr-xr-x | challenge-346/perlboy1967/perl/ch1.pl | 47 | ||||
| -rwxr-xr-x | challenge-346/perlboy1967/perl/ch2.pl | 45 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-346/perlboy1967/perl/ch1.pl b/challenge-346/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..aba8ac09e5 --- /dev/null +++ b/challenge-346/perlboy1967/perl/ch1.pl @@ -0,0 +1,47 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-346#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Longest Parenthesis +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only ( and ). + +Write a script to find the length of the longest valid parenthesis. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(max); + +sub longestParenthesis ($str) { + my ($maxLen,$i,@stack) = (0,0,-1); + for (split//,$str) { + if ($_ eq '(') { + push @stack,$i; + } else { + pop @stack; + if (@stack) { + $maxLen = max($maxLen,$i - $stack[-1]); + } else { + push @stack, $i; + } + } + $i++; + } + return $maxLen; +} + +is(longestParenthesis('(()())'),6,'Example 1'); +is(longestParenthesis(')()())'),4,'Example 2'); +is(longestParenthesis('((()))()(((()'),8,'Example 3'); +is(longestParenthesis('))))((()('),2,'Example 4'); +is(longestParenthesis('()(()'),2,'Example 5'); + +done_testing; diff --git a/challenge-346/perlboy1967/perl/ch2.pl b/challenge-346/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..574c7283c8 --- /dev/null +++ b/challenge-346/perlboy1967/perl/ch2.pl @@ -0,0 +1,45 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-346#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Magic Expression +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only digits and a target integer. + +Write a script to insert binary operators +, - and * between the digits +in the given string that evaluates to target integer. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use Algorithm::Combinatorics qw(variations_with_repetition); + +sub magicExpression ($str,$target) { + my @d = split//,$str; + my (@res,$eval); + my @op = ('*','+','-',''); + my $sprintfFmt = join('%s',@d); + for my $op (variations_with_repetition(\@op,$#d)) { + my $s = sprintf($sprintfFmt,@$op); + next if $s =~ m#\D[0]+\d#; + $eval .= sprintf(qq{push(\@res,'%s') if %s == %d;\n}, $s, $s, $target); + } + eval $eval; + @res; +} + +is([magicExpression('123',6)],['1*2*3','1+2+3'],'Example 1'); +is([magicExpression('105',5)],['1*0+5','10-5'],'Example 2'); +is([magicExpression('232',8)],['2*3+2','2+3*2'],'Example 3'); +is([magicExpression('1234',10)],['1*2*3+4','1+2+3+4'],'Example 4'); +is([magicExpression('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; |
