diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-09 23:58:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-09 23:58:45 +0000 |
| commit | c3bbc76499be62ff9ce74e9d92476ada7f885678 (patch) | |
| tree | d9a6d1fa8a410568fd92e10f44647756e1c9460f | |
| parent | 0de5f0e4739485d76a1cd2701026a21d95f1f7fa (diff) | |
| parent | 55810dacb6b1ea71ba84ac51918783711fefc6f5 (diff) | |
| download | perlweeklychallenge-club-c3bbc76499be62ff9ce74e9d92476ada7f885678.tar.gz perlweeklychallenge-club-c3bbc76499be62ff9ce74e9d92476ada7f885678.tar.bz2 perlweeklychallenge-club-c3bbc76499be62ff9ce74e9d92476ada7f885678.zip | |
Merge pull request #12997 from ysth/challenge-345-ysth
use Run::WeeklyChallenge
| -rw-r--r-- | challenge-345/ysth/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-345/ysth/perl/ch-2.pl | 61 |
2 files changed, 15 insertions, 101 deletions
diff --git a/challenge-345/ysth/perl/ch-1.pl b/challenge-345/ysth/perl/ch-1.pl index d2f7dd49a1..92034fa16c 100644 --- a/challenge-345/ysth/perl/ch-1.pl +++ b/challenge-345/ysth/perl/ch-1.pl @@ -1,7 +1,4 @@ use 5.040; -use Cpanel::JSON::XS; -use JSON::Schema::Modern; -use List::Util (); sub longest_balanced_parentheses($string) { @@ -41,56 +38,16 @@ sub longest_balanced_parentheses($string) { } sub main() { - my $input_example = '"()()"'; - - my $input_schema = '{ - "type": "string" - }'; + require Run::WeeklyChallenge; 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; + my $inputs_example = '"()()"'; + my $inputs_schema_json = '{ + "type": "string" + }'; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); } main() unless caller; diff --git a/challenge-345/ysth/perl/ch-2.pl b/challenge-345/ysth/perl/ch-2.pl index 0f9d5965d4..996c02025b 100644 --- a/challenge-345/ysth/perl/ch-2.pl +++ b/challenge-345/ysth/perl/ch-2.pl @@ -1,7 +1,4 @@ use 5.040; -use Cpanel::JSON::XS; -use JSON::Schema::Modern; -use List::Util (); sub magic_expression($string, $target) { @@ -19,9 +16,15 @@ sub magic_expression($string, $target) { } sub main() { - my $input_example = '{"string":"123","target":6}'; + require Run::WeeklyChallenge; - my $input_schema = '{ + 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 $inputs_example = '{"string":"123","target":6}'; + my $inputs_schema_json = '{ "type": "object", "properties": { "string": { @@ -33,53 +36,7 @@ sub main() { "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; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); } main() unless caller; |
