aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-042/cristian-heredia/perl/ch-1.pl7
-rw-r--r--challenge-042/cristian-heredia/perl/ch-2.pl72
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-042/cristian-heredia/perl/ch-1.pl b/challenge-042/cristian-heredia/perl/ch-1.pl
new file mode 100644
index 0000000000..6414e6d397
--- /dev/null
+++ b/challenge-042/cristian-heredia/perl/ch-1.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/perl
+
+for (my $i = 0; $i <= 50; $i++) {
+
+ print "Decimal $i = Octal ";
+ printf("%o\n",$_)for "$i";
+}
diff --git a/challenge-042/cristian-heredia/perl/ch-2.pl b/challenge-042/cristian-heredia/perl/ch-2.pl
new file mode 100644
index 0000000000..851768e393
--- /dev/null
+++ b/challenge-042/cristian-heredia/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+use strict;
+
+#Variables
+my $random;
+my @array;
+my $i = 0;
+my $text;
+my $code = 0;
+
+
+generateString();
+validation();
+result();
+
+#Fuctions
+
+#Create the string of parenthesis by generates random numbers between 0-2 (0 = '(', 1 = ')' and 2 = end).
+sub generateString {
+ $random = int rand(2);
+ convertParenthesis();
+ while ($random != 2) {
+ $random = int rand(3);
+ convertParenthesis();
+ }
+}
+
+#Converts the numbers generated into parenthesis
+sub convertParenthesis{
+
+ if ($random == '0') {
+ @array[$i] = $random;
+ $text .= '(';
+ $i++;
+ }
+ elsif ($random == '1') {
+ @array[$i] = $random;
+ $text .= ')';
+ $i++;
+ }
+
+}
+
+#Checks if the string has balanced brackets
+sub validation {
+ my $length = @array;
+ for (my $j = 0; $j < $length; $j++) {
+ if (@array[$j] == 0) {
+ $code++;
+ }
+ elsif (@array[$j] == 1 and $code != 0) {
+ $code--;
+ }
+ else {
+ $code = 1;
+ last;
+ }
+ }
+}
+
+#Write the string and indicate if it's ok or not
+sub result {
+ if ($code == 0) {
+ print "$text - OK\n";
+ }
+ else {
+ print "$text - NOT OK\n";
+ }
+}
+
+