aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-042/simon-proctor/raku/ch-2.p627
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-042/simon-proctor/raku/ch-2.p6 b/challenge-042/simon-proctor/raku/ch-2.p6
new file mode 100644
index 0000000000..50178cdb05
--- /dev/null
+++ b/challenge-042/simon-proctor/raku/ch-2.p6
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+#| Generate a random string of brackets then test if they are balanced
+sub MAIN() {
+ my $brackets = ("(",")").roll( (1..25).pick() * 2 ).join("");
+ say "String to test {$brackets}";
+ say "Balanced? {balanced($brackets)}";
+}
+
+sub balanced( Str \brackets ) {
+ my @list = brackets.comb("");
+ my $count = 0;
+ for @list -> \bracket {
+ given bracket {
+ when "(" {
+ $count++;
+ }
+ when ")" {
+ $count--;
+ }
+ }
+ return False if $count < 0;
+ }
+ return $count == 0;
+}