aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCris-HD <59669732+Cris-HD@users.noreply.github.com>2020-01-11 19:02:23 +0100
committerCris-HD <59669732+Cris-HD@users.noreply.github.com>2020-01-11 19:02:23 +0100
commitec3da20619ee51da0eaa4fe67fd39a3d0b5de89d (patch)
tree6253ca07bd4b75168c0fe0bd4e6d6714a50baf44
parenta56426175505a28f0cf131f3bb668cfd87d16dff (diff)
downloadperlweeklychallenge-club-ec3da20619ee51da0eaa4fe67fd39a3d0b5de89d.tar.gz
perlweeklychallenge-club-ec3da20619ee51da0eaa4fe67fd39a3d0b5de89d.tar.bz2
perlweeklychallenge-club-ec3da20619ee51da0eaa4fe67fd39a3d0b5de89d.zip
Created solutions for challenge042
-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";
+ }
+}
+
+