The Weekly Challenge - 347

Monday, Nov 10, 2025| Tags: Perl, Raku

TABLE OF CONTENTS


  1. HEADLINES

  2. SPONSOR

  3. RECAP

  4. PERL REVIEW

  5. RAKU REVIEW

  6. CHART

  7. NEW MEMBERS

  8. GUESTS

  9. TASK #1: Format Date

10. TASK #2: Format Phone Number


HEADLINES


Welcome to the Week #347 of The Weekly Challenge.

In Week #346, we had new guest contributor lexi and this week we have one more guest contributor beespider. To my surprise, both of them are Uiua hackers. Thank you, beespider, for your first contributions in Uiua.

Welcome back, Luca Ferrari and thanks for your contributions.

There is another surprise element for you all. Yitzchak Scott-Thoennes uploaded a new distribution Run::WeeklyChallenge. Please do give it a try and share your experience.

Looking at the TOP 10 Guest Languages, apparently C and JavaScript are tied with total contributions count 596. May be, next week, we would see some changes in the ranking.

Below is my contributions to the Task #1 of Week #346.


Perl


#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;

my @examples = (
    { str => '(()())',        exp => 6 },
    { str => ')()())',        exp => 4 },
    { str => '((()))()(((()', exp => 8 },
    { str => '))))((()(',     exp => 2 },
    { str => '()(()',         exp => 2 },
);

foreach (@examples) {
    is(valid_longest_parenthesis($_->{str}), $_->{exp});
}

done_testing;

sub valid_longest_parenthesis {
    my $s       = shift;
    my @stack   = (-1);
    my $max_len = 0;

    for my $i (0 .. length($s) - 1) {
        if (substr($s, $i, 1) eq "(") {
            push @stack, $i;
        } else {
            pop @stack;
            if (@stack) {
                $max_len = $max_len > ($i - $stack[-1])
                          ? $max_len
                          : ($i - $stack[-1]);
            } else {
                push @stack, $i;  # New starting point
            }
        }
    }

    return $max_len;
}

Raku


#!/usr/bin/env raku

use Test;

my @examples = (
    { str => '(()())',        exp => 6 },
    { str => ')()())',        exp => 4 },
    { str => '((()))()(((()', exp => 8 },
    { str => '))))((()(',     exp => 2 },
    { str => '()(()',         exp => 2 },
);

for @examples -> %example {
    is(valid-longest-parenthesis(%example<str>), %example<exp>);
}

done-testing;

sub valid-longest-parenthesis(Str $s) {
    my @stack   = (-1);
    my $max-len = 0;

    for 0 .. $s.chars - 1 -> $i {
        if $s.substr($i, 1) eq "(" {
            @stack.push($i);
        } else {
            @stack.pop();
            if @stack.elems > 0 {
                $max-len = $max-len > ($i - @stack[*-1])
                           ?? $max-len
                           !! ($i - @stack[*-1]);
            } else {
                @stack.push($i);  # New starting point
            }
        }
    }

    return $max-len;
}

Python


#!/usr/bin/env python3

def valid_longest_parenthesis(s):
    stack = [-1]
    max_len = 0

    for i in range(len(s)):
        if s[i] == "(":
            stack.append(i)
        else:
            stack.pop()
            if stack:
                max_len = max(max_len, i - stack[-1])
            else:
                stack.append(i)  # New starting point

    return max_len

def test_examples():
    examples = [
        {"str": "(()())",        "exp": 6},
        {"str": ")()())",        "exp": 4},
        {"str": "((()))()(((()", "exp": 8},
        {"str": "))))((()(",     "exp": 2},
        {"str": "()(()",         "exp": 2},
    ]

    for example in examples:
        result = valid_longest_parenthesis(example["str"])
        expected = example["exp"]
        assert result == expected, f"Failed for '{example['str']}': expected {expected}, got {result}"
        print(f"'{example['str']}' -> {result}")

if __name__ == "__main__":
    test_examples()
    print("All tests passed!")

Thank you Team PWC, once again.

Happy Hacking!!


Last 5 weeks mainstream contribution stats. Thank you Team PWC for your support and encouragements.

  Week      Perl       Raku       Blog   
   342       51       22       15   
   343       44       19       13   
   344       47       25       17   
   345       47       22       15   
   346       46       16       13   

Last 5 weeks guest contribution stats. Thank you each and every guest contributors for your time and efforts.

  Week      Guests       Contributions       Languages   
   342       14       46       14   
   343       15       61       20   
   344       16       60       18   
   345       17       57       18   
   346       14       47       17   

TOP 10 Guest Languages


Do you see your favourite language in the Top #10? If not then why not contribute regularly and make it to the top.

 1. Python     (3721)
 2. Rust       (1036)
 3. Ruby       (844)
 4. Haskell    (829)
 5. Lua        (788)
 6. C++        (664)
 7. C          (596)
 8. JavaScript (596)
 9. Go         (574)
10. BQN        (481)

Blogs with Creative Title


1. Parenthesised Magic by Arne Sommer.

2. Magic Parentheses by Jorg Sommrey.

3. really not inspired! by Luca Ferrari.

4. Recursive Parentheses - But no Recursive Magic by Matthias Muth.

5. Whoa-oh-oh! Sing about parens! by Packy Anderson.

6. (Magic) by Peter Campbell Smith.

7. All Aboard The Magic Parenthesis by Roger Bell_West.

8. Longest Expression by Simon Green.


GitHub Repository Stats


1. Commits: 46,181 (+109)

2. Pull Requests: 12,958 (+37)

3. Contributors: 265 (+1)

4. Fork: 339

5. Stars: 199



With start of Week #268, we have a new sponsor Lance Wicks until the end of year 2025. Having said we are looking for more sponsors so that we can go back to weekly winner. If anyone interested please get in touch with us at perlweeklychallenge@yahoo.com. Thanks for your support in advance.


RECAP


Quick recap of The Weekly Challenge - 346 by Mohammad Sajid Anwar.


PERL REVIEW


If you missed any past reviews then please check out the collection.


RAKU REVIEW


If you missed any past reviews then please check out the collection.


CHART


Please take a look at the charts showing interesting data.

I would like to THANK every member of the team for their valuable suggestions. Please do share your experience with us.


NEW MEMBERS


Please find out How to contribute?, if you have any doubts.

Please try the excellent tool EZPWC created by respected member Saif Ahmed of Team PWC.


GUESTS


Please check out the guest contributions for the Week #346.

Please find past solutions by respected guests. Please share your creative solutions in other languages.


Task 1: Format Date

Submitted by: Mohammad Sajid Anwar

You are given a date in the form: 10th Nov 2025.

Write a script to format the given date in the form: 2025-11-10 using the set below.

@DAYS   = ("1st", "2nd", "3rd", ....., "30th", "31st")
@MONTHS = ("Jan", "Feb", "Mar", ....., "Nov",  "Dec")
@YEARS  = (1900..2100)

Example 1

Input: $str = "1st Jan 2025"
Output: "2025-01-01"

Example 2

Input: $str = "22nd Feb 2025"
Output: "2025-02-22"

Example 3

Input: $str = "15th Apr 2025"
Output: "2025-04-15"

Example 4

Input: $str = "23rd Oct 2025"
Output: "2025-10-23"

Example 5

Input: $str = "31st Dec 2025"
Output: "2025-12-31"

Task 2: Format Phone Number

Submitted by: Mohammad Sajid Anwar

You are given a phone number as a string containing digits, space and dash only.

Write a script to format the given phone number using the below rules:

1. Removing all spaces and dashes
2. Grouping digits into blocks of length 3 from left to right
3. Handling the final digits (4 or fewer) specially:
   - 2 digits: one block of length 2
   - 3 digits: one block of length 3
   - 4 digits: two blocks of length 2
4. Joining all blocks with dashes

Example 1

Input: $phone = "1-23-45-6"
Output: "123-456"

Example 2

Input: $phone = "1234"
Output: "12-34"

Example 3

Input: $phone = "12 345-6789"
Output: "123-456-789"

Example 4

Input: $phone = "123 4567"
Output: "123-45-67"

Example 5

Input: $phone = "123 456-78"
Output: "123-456-78"


Last date to submit the solution 23:59 (UK Time) Sunday 16th November 2025.


SO WHAT DO YOU THINK ?

If you have any suggestions or ideas then please do share with us.

Contact with me