aboutsummaryrefslogtreecommitdiff
path: root/challenge-042/dave-jacoby
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-01-08 12:01:29 +0000
committerGitHub <noreply@github.com>2020-01-08 12:01:29 +0000
commit1cfc975fc77167dd72102c31b2e914a555739fc9 (patch)
treedf73a37760e669ae882f64615704466a2f9e5ad0 /challenge-042/dave-jacoby
parent2beb95259d12881339724ae087a44d7220360c29 (diff)
parent38c910924f9b7fe5c5ae004474ac971ac0ce340c (diff)
downloadperlweeklychallenge-club-1cfc975fc77167dd72102c31b2e914a555739fc9.tar.gz
perlweeklychallenge-club-1cfc975fc77167dd72102c31b2e914a555739fc9.tar.bz2
perlweeklychallenge-club-1cfc975fc77167dd72102c31b2e914a555739fc9.zip
Merge pull request #1122 from jacoby/master
Week 42
Diffstat (limited to 'challenge-042/dave-jacoby')
-rw-r--r--challenge-042/dave-jacoby/perl5/ch-1.pl13
-rw-r--r--challenge-042/dave-jacoby/perl5/ch-2.pl37
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-042/dave-jacoby/perl5/ch-1.pl b/challenge-042/dave-jacoby/perl5/ch-1.pl
new file mode 100644
index 0000000000..327ea71539
--- /dev/null
+++ b/challenge-042/dave-jacoby/perl5/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+
+use feature qw{ say };
+use strict;
+use warnings;
+
+# Too easy - sprintf gives us base conversion almost for free
+
+for my $d ( 0 .. 50 ) {
+ my $o = sprintf '%o', $d;
+ say qq{Decimal $d = Octal $o};
+}
+
diff --git a/challenge-042/dave-jacoby/perl5/ch-2.pl b/challenge-042/dave-jacoby/perl5/ch-2.pl
new file mode 100644
index 0000000000..2cb2d701ec
--- /dev/null
+++ b/challenge-042/dave-jacoby/perl5/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures };
+no warnings qw{ experimental::signatures };
+
+# generate the string of braces
+my $string;
+for ( 0 .. 1 + int rand 9 ) {
+ $string .= int rand 2 ? '(' : ')';
+}
+
+# test if the string has matched braces
+my $t = test_braces($string);
+my $response = $t == 0 ? 'OK' : 'NOT OK';
+say qq{$string - $response};
+
+exit;
+
+# The trick is a stack. When you get a "(",
+# you push onto the stack, and when get a ")",
+# you pop from the stack. We special-case when
+# the stack is empty and we get a ")", immediately
+# returning -1, and then returning the number of
+# values in the array. A balanced array will return
+# 0
+
+sub test_braces ( $string ) {
+ my @x;
+ for my $i ( split //, $string ) {
+ return -1 if !scalar @x && $i eq ')';
+ pop @x if scalar @x && $i eq ')';
+ push @x, '' if $i eq '(';
+ }
+ return scalar @x;
+}