aboutsummaryrefslogtreecommitdiff
path: root/challenge-346/athanasius/raku/ch-1.raku
blob: 9ed2c98279612ce6975cee66dc59b5fb8e166182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use v6d;

################################################################################
=begin comment

Perl Weekly Challenge 346
=========================

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.

Example 1

  Input: $str = '(()())'
  Output: 6

  Valid Parenthesis: '(()())'

Example 2

  Input: $str = ')()())'
  Output: 4

  Valid Parenthesis: '()()' at positions 1-4.

Example 3

  Input: $str = '((()))()(((()'
  Output: 8

  Valid Parenthesis: '((()))()' at positions 0-7.

Example 4

  Input: $str = '))))((()('
  Output: 2

  Valid Parenthesis: '()' at positions 6-7.

Example 5

  Input: $str = '()(()'
  Output: 2

  Valid Parenthesis: '()' at positions 0-1 and 3-4.

=end comment
################################################################################

#--------------------------------------#
# Copyright © 2025 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
=begin comment

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A single string, containing only left and right parentheses, is entered on
   the command-line.

=end comment
#===============================================================================

use Test;

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    "\nChallenge 346, Task #1: Longest Parenthesis (Raku)\n".put;
}

#===============================================================================
multi sub MAIN
(
    Str:D $str where { / ^ <[()]>* $ / }     #= A string containing only ( and )
)
#===============================================================================
{
    "Input:  \$str = '$str'".put;

    my UInt $len = find-longest-paren-len( $str );

    "Output: $len".put;
}

#===============================================================================
multi sub MAIN()                                  # No input: run the test suite
#===============================================================================
{
    run-tests();
}

#-------------------------------------------------------------------------------
sub find-longest-paren-len( Str:D $string where { / ^ <[()]>* $ / } --> UInt:D )
#-------------------------------------------------------------------------------
{
    my Str  $str      =  $string;
            $str      ~~ s/ ^ \)+   //;
            $str      ~~ s/   \(+ $ //;
    my UInt $max-len  =  0;
    my Int  $last-idx =  $str.chars - 1;
    my UInt $start    =  0;
    my Int  $last-end = -2;
    my UInt $last-len =  0;

    while $last-idx > $start
    {
        my UInt $count = 1;
        my Bool $found = False;

        for $start + 1 .. $last-idx -> UInt $end
        {
            if    $str.substr( $end, 1 ) eq '('
            {
                ++$count;
            }
            elsif --$count == 0        # Match found
            {
                my UInt $len  = $end - $start + 1;
                        $len += $last-len if $start == $last-end + 1;

                $max-len  = ($len, $max-len).max;
                $last-end =  $end;
                $last-len =  $len;
                $found    =  True;
                $start    =  $end + 1;

                ++$start while $str.substr( $start, 1 ) eq ')';

                last;
            }
        }

        ++$start unless $found;        # No match: advance 1 char & search again
    }

    return $max-len;
}

#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
    'Running the test suite'.put;

    for test-data.lines -> Str $line
    {
        my Str ($test-name, $str, $expected) = $line.split: / \| /;

        for     $test-name, $str, $expected
        {
            s/ ^ \s+   //;
            s/   \s+ $ //;
        }

        my UInt $len = find-longest-paren-len( $str );

        is $len, $expected.Int, $test-name;
    }

    done-testing;
}

#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
    my Str $usage = $*USAGE;

    $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;

    $usage.put;
}

#-------------------------------------------------------------------------------
sub test-data( --> Str:D )
#-------------------------------------------------------------------------------
{
    return q:to/END/;
        Example 1|(()())       |6|(()())
        Example 2|)()())       |4|()()     at positions 1-4
        Example 3|((()))()(((()|8|((()))() at positions 0-7
        Example 4|))))((()(    |2|()       at positions 6-7
        Example 5|()(()        |2|()       at positions 0-1 and 3-4
        2 matches|())(())      |4|(())     at positions 3-6
        END
}

################################################################################