diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-07 23:54:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-07 23:54:15 +0000 |
| commit | 159c16f35ef2f357b8ec270c23724afec934ffdf (patch) | |
| tree | 78424e03a0712eb109dc5fadf478b7b57311a6d7 | |
| parent | 5ed7874a11f18e01d070e7fc4cba28edf587541b (diff) | |
| parent | f5912742863a2503baec40bc6820c6ce333f8c00 (diff) | |
| download | perlweeklychallenge-club-159c16f35ef2f357b8ec270c23724afec934ffdf.tar.gz perlweeklychallenge-club-159c16f35ef2f357b8ec270c23724afec934ffdf.tar.bz2 perlweeklychallenge-club-159c16f35ef2f357b8ec270c23724afec934ffdf.zip | |
Merge pull request #12983 from ysth/challenge-345-ysth
challenge 345 perl/go solutions
| -rw-r--r-- | challenge-345/ysth/go/ch-1.go | 56 | ||||
| -rw-r--r-- | challenge-345/ysth/perl/README.md | 1 | ||||
| -rw-r--r-- | challenge-345/ysth/perl/ch-1.pl | 96 | ||||
| -rw-r--r-- | challenge-345/ysth/perl/ch-2.pl | 85 |
4 files changed, 238 insertions, 0 deletions
diff --git a/challenge-345/ysth/go/ch-1.go b/challenge-345/ysth/go/ch-1.go new file mode 100644 index 0000000000..14e7f2eea7 --- /dev/null +++ b/challenge-345/ysth/go/ch-1.go @@ -0,0 +1,56 @@ +package main + +import run "github.com/ysth/runweeklychallenge-go" + +func LongestBalancedParentheses(inputString string) (int) { + + longest_length := 0 + nested_depth := 0 + start_index := []int{} + + for index, rune := range inputString { + if rune == '(' { + // opening parenthesis + + nested_depth++ + + // a new potentially balanced string starts here, unless we just had a close paren for this same depth + if (len(start_index) < nested_depth) { + start_index = append(start_index, index) + } + } else if nested_depth > 0 { + // closing parenthesis and there's something to close + + nested_depth-- + + length := index - start_index[nested_depth] + 1 + if (length > longest_length) { + longest_length = length + } + + // starting indexes deeper than this just closed depth are no longer relevant + start_index = start_index[:nested_depth+1] + } else { + // bad closing parenthesis; nothing before this is relevant anymore + + start_index = []int{} + nested_depth = 0 + } + } + + return longest_length; +} + +func main() { + runSolution := func(inputs any) (any, error) { + return LongestBalancedParentheses(inputs.(string)), nil + } + + inputExample := `"()()"` + + inputSchema := `{ + "type": "string" + }` + + run.RunWeeklyChallenge(runSolution, inputExample, inputSchema) +} diff --git a/challenge-345/ysth/perl/README.md b/challenge-345/ysth/perl/README.md new file mode 100644 index 0000000000..60d86e8401 --- /dev/null +++ b/challenge-345/ysth/perl/README.md @@ -0,0 +1 @@ +# Solutions by Yitzchak Scott-Thoennes diff --git a/challenge-345/ysth/perl/ch-1.pl b/challenge-345/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..d2f7dd49a1 --- /dev/null +++ b/challenge-345/ysth/perl/ch-1.pl @@ -0,0 +1,96 @@ +use 5.040; +use Cpanel::JSON::XS; +use JSON::Schema::Modern; +use List::Util (); + +sub longest_balanced_parentheses($string) { + + my $longest_length = 0; + my $nested_depth = 0; + my @start_index = 0; + + for my $index (0..length($string) - 1) { + # opening parenthesis + if (substr($string, $index, 1) eq '(') { + ++$nested_depth; + # a new potentially balanced string starts here, unless we just had a close paren for this same depth + if (@start_index == $nested_depth) { + push @start_index, $index; + } + } + # closing parenthesis and there's something to close + elsif ($nested_depth) { + my $length = $index - $start_index[$nested_depth] + 1; + if ($length > $longest_length) { + $longest_length = $length; + } + + # starting indexes deeper than this just closed depth are no longer relevant + splice @start_index, $nested_depth+1; + + --$nested_depth; + } + # bad closing parenthesis; nothing before this is relevant anymore + else { + @start_index = $index + 1; + $nested_depth = 0; + } + } + + return $longest_length; +} + +sub main() { + my $input_example = '"()()"'; + + my $input_schema = '{ + "type": "string" + }'; + + my $run_solution = sub ($inputs) { + longest_balanced_parentheses($inputs); + }; + + my $json = Cpanel::JSON::XS->new->allow_nonref; + my $validator = JSON::Schema::Modern->new( 'specification_version' => 'draft2020-12', 'output_format' => 'flag' ); + my $schema = $json->decode($input_schema); + + my $errors; + + for my $inputs_json (@ARGV) { + say "Input: $inputs_json"; + + try { + my $inputs = $json->decode($inputs_json); + if (! $validator->evaluate($inputs, $schema)->valid) { + $errors = true; + say "Error: invalid input"; + next; + } + else { + try { + my $result = $run_solution->($inputs); + say "Output: $result"; + } + catch ($e) { + chomp $e; + say "Error: $e"; + } + } + } + catch ($e) { + $errors = true; + chomp $e; + say "Error: invalid json input: $e"; + next; + } + } + + if ($errors) { + say "Expected arguments like '$input_example'"; + } + + return; +} + +main() unless caller; diff --git a/challenge-345/ysth/perl/ch-2.pl b/challenge-345/ysth/perl/ch-2.pl new file mode 100644 index 0000000000..0f9d5965d4 --- /dev/null +++ b/challenge-345/ysth/perl/ch-2.pl @@ -0,0 +1,85 @@ +use 5.040; +use Cpanel::JSON::XS; +use JSON::Schema::Modern; +use List::Util (); + +sub magic_expression($string, $target) { + + my @solutions; + + my $glob_pattern = join '{,+,\*,-}', split //, $string; + while (my $expression = glob $glob_pattern) { + # from the challenge examples, we won't allow extra leading 0's in numbers in the expression + next if $expression =~ /\b0[0-9]/; + + push @solutions, $expression if eval $expression == $target; + } + + return @solutions; +} + +sub main() { + my $input_example = '{"string":"123","target":6}'; + + my $input_schema = '{ + "type": "object", + "properties": { + "string": { + "type": "string", + "pattern": "^[0-9]+$" + }, + "target": {"type": "integer"} + }, + "required": ["string","target"], + "additionalProperties": false + }'; + + my $run_solution = sub ($inputs) { + # format results like: ("2*3+2", "2+3*2") + sprintf '(%s)', join ', ', map qq!"$_"!, + magic_expression($inputs->{'string'}, $inputs->{'target'}); + }; + + my $json = Cpanel::JSON::XS->new->allow_nonref; + my $validator = JSON::Schema::Modern->new( 'specification_version' => 'draft2020-12', 'output_format' => 'flag' ); + my $schema = $json->decode($input_schema); + + my $errors; + + for my $inputs_json (@ARGV) { + say "Input: $inputs_json"; + + try { + my $inputs = $json->decode($inputs_json); + if (! $validator->evaluate($inputs, $schema)->valid) { + $errors = true; + say "Error: invalid input"; + next; + } + else { + try { + my $result = $run_solution->($inputs); + say "Output: $result"; + } + catch ($e) { + chomp $e; + say "Error: $e"; + } + } + } + catch ($e) { + $errors = true; + chomp $e; + say "Error: invalid json input: $e"; + next; + } + } + + if ($errors) { + say "Expected arguments like '$input_example'"; + } + + return; +} + +main() unless caller; |
