aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-01-14 12:36:35 +0000
committerGitHub <noreply@github.com>2020-01-14 12:36:35 +0000
commit0cefdf93d2b22a9bbe47f05b00aeb4f1eaec7fed (patch)
tree47324781db15bfe95cda2b4b9638c52a93c5ea67
parenta20e1254ddc59c5cff661e770193bd8495987917 (diff)
parentf19feae9716975807b29c89a80050dc569ccabf8 (diff)
downloadperlweeklychallenge-club-0cefdf93d2b22a9bbe47f05b00aeb4f1eaec7fed.tar.gz
perlweeklychallenge-club-0cefdf93d2b22a9bbe47f05b00aeb4f1eaec7fed.tar.bz2
perlweeklychallenge-club-0cefdf93d2b22a9bbe47f05b00aeb4f1eaec7fed.zip
Merge pull request #1136 from andrezgz/challenge-042
challenge-042 andrezgz solution
-rw-r--r--challenge-042/andrezgz/perl/ch-1.pl24
-rw-r--r--challenge-042/andrezgz/perl/ch-2.pl31
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-042/andrezgz/perl/ch-1.pl b/challenge-042/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..ae71c5c31e
--- /dev/null
+++ b/challenge-042/andrezgz/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-042/
+# Task #1
+# Octal Number System
+# Write a script to print decimal number 0 to 50 in Octal Number System.
+#
+# For example:
+#
+# Decimal 0 = Octal 0
+# Decimal 1 = Octal 1
+# Decimal 2 = Octal 2
+# Decimal 3 = Octal 3
+# Decimal 4 = Octal 4
+# Decimal 5 = Octal 5
+# Decimal 6 = Octal 6
+# Decimal 7 = Octal 7
+# Decimal 8 = Octal 10
+# and so on.
+
+use strict;
+use warnings;
+
+printf "Decimal $_ = Octal %o\n",$_ for (0 .. 50);
diff --git a/challenge-042/andrezgz/perl/ch-2.pl b/challenge-042/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..14370dfe2d
--- /dev/null
+++ b/challenge-042/andrezgz/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-042/
+# Task #2
+# 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
+
+
+use strict;
+use warnings;
+
+my $brackets = int rand(10) + 1;
+
+my $str = '';
+$str .= rand() < 0.5 ? '(' : ')' for ( 1 .. $brackets );
+
+my $open_count = 0;
+foreach my $b ( split //, $brackets ) {
+ last if $open_count < 0;
+ $b eq '(' ? $open_count++ : $open_count--;
+}
+
+print $str , ' - ', $open_count ? 'NOT OK' : 'OK';