aboutsummaryrefslogtreecommitdiff
path: root/challenge-042
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-042')
-rwxr-xr-xchallenge-042/ruben-westerberg/perl/ch-2.pl26
-rwxr-xr-xchallenge-042/ruben-westerberg/raku/ch-2.p617
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-042/ruben-westerberg/perl/ch-2.pl b/challenge-042/ruben-westerberg/perl/ch-2.pl
new file mode 100755
index 0000000000..b72c777c89
--- /dev/null
+++ b/challenge-042/ruben-westerberg/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use POSIX qw<round>;
+
+my $maxLength=$ARGV[0]//20; #If no max on command line use 20
+while () {
+ my $str="";
+ #make a random length string of up to $maxLength long
+ $str.=chr round rand()+40 for 0..int rand $maxLength;
+
+ my $v=0;
+ for (split "",$str) {
+ $v+=(ord($_)-40)*-2+1;
+ last unless $v >=0;
+ }
+
+ if($v==0) {
+ print("balanced: $str\n");
+ sleep 1;
+ next;
+ }
+ print("unbalanced: $str\n");
+}
+
+
diff --git a/challenge-042/ruben-westerberg/raku/ch-2.p6 b/challenge-042/ruben-westerberg/raku/ch-2.p6
new file mode 100755
index 0000000000..99d2ee83ba
--- /dev/null
+++ b/challenge-042/ruben-westerberg/raku/ch-2.p6
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl6
+my $maxLength=@*ARGS[0]//20; #If no max on command line use 20
+while 1 {
+ my $str="";
+ #make a random length string of upto $maxLength long
+ $str~=(1.rand()+40).round.chr for 0..$maxLength.rand.Int;
+ my @v= [\+] $str.comb.map({(ord($_)-40)*-2+1});
+
+ if @v[*-1]==0 and !@v.grep(* < 0) {
+ put "balanced $str";
+ sleep 1;
+ next;
+ }
+ say "unbalanced: $str";
+}
+
+