aboutsummaryrefslogtreecommitdiff
path: root/challenge-042/noud/perl6/ch-2.p6
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-042/noud/perl6/ch-2.p6')
-rw-r--r--challenge-042/noud/perl6/ch-2.p651
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-042/noud/perl6/ch-2.p6 b/challenge-042/noud/perl6/ch-2.p6
new file mode 100644
index 0000000000..59415fbd69
--- /dev/null
+++ b/challenge-042/noud/perl6/ch-2.p6
@@ -0,0 +1,51 @@
+# Balanced Brackets
+#
+# Write a script to generate a string with random number of ( and ) brackets.
+# Then make the script validate the string if it has balanced brackets.
+#
+# For example:
+# () - OK
+# (()) - OK
+# )( - NOT OK
+# ())() - NOT OK
+
+sub rand_brackets($size) {
+ return [~] (<( )>.pick() for ^$size);
+}
+
+sub balanced($brackets) {
+ # Count the open ( and close ) brackets sequentially. ( is given +1, ) is
+ # given -1. If the count ever goes below zero there are more close
+ # brackets than open brackets, hence an unbalanced string. If at the end
+ # the count is more than zero there are more open brackets than close
+ # brackets, hence an unbalanced string.
+ my $c = 0;
+
+ for $brackets.comb() -> $b {
+ if ($b === '(') {
+ $c++;
+ } elsif (--$c < 0) {
+ return False;
+ }
+ }
+
+ return $c == 0;
+}
+
+say "() - " ~ balanced("()");
+say "(()) - " ~ balanced("(())");
+say ")( - " ~ balanced(")(");
+say "())() - " ~ balanced("())()");
+
+
+my $brackets = '';
+for 1..20 -> $i {
+ # Only generate bracket strings of length 4, because with higher lengths
+ # the randomly generated strings rarely are balanced.
+ $brackets = rand_brackets(4);
+ if (balanced($brackets)) {
+ say $brackets ~ ' is balanced.';
+ } else {
+ say $brackets ~ ' is not balanced.';
+ }
+}