diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-12 22:29:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-12 22:29:17 +0000 |
| commit | ec9783b40ffeec12b492a7924d8218cf33731e39 (patch) | |
| tree | c46f3c02bf32fddbf59bcf7515ccab754c376f9e /challenge-042 | |
| parent | fa6a694b3e4a4e7a99a24e08cc8f1bbee3829424 (diff) | |
| parent | e0dff92203a6e930c27dc8c86a10623d295498d0 (diff) | |
| download | perlweeklychallenge-club-ec9783b40ffeec12b492a7924d8218cf33731e39.tar.gz perlweeklychallenge-club-ec9783b40ffeec12b492a7924d8218cf33731e39.tar.bz2 perlweeklychallenge-club-ec9783b40ffeec12b492a7924d8218cf33731e39.zip | |
Merge pull request #1130 from adamcrussell/challenge-042
solutions for challenge 042
Diffstat (limited to 'challenge-042')
| -rw-r--r-- | challenge-042/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-042/adam-russell/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-042/adam-russell/perl/ch-2.pl | 39 |
3 files changed, 50 insertions, 0 deletions
diff --git a/challenge-042/adam-russell/blog.txt b/challenge-042/adam-russell/blog.txt new file mode 100644 index 0000000000..8892e85a3d --- /dev/null +++ b/challenge-042/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/14160.html diff --git a/challenge-042/adam-russell/perl/ch-1.pl b/challenge-042/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..25a8092967 --- /dev/null +++ b/challenge-042/adam-russell/perl/ch-1.pl @@ -0,0 +1,10 @@ +use strict; +use warnings; +## +# Write a script to print decimal number 0 to 50 in octal. +## +MAIN:{ + for my $x (0..50){ + print "Decimal $x = Octal " . sprintf("%o", $x) . "\n"; + } +} diff --git a/challenge-042/adam-russell/perl/ch-2.pl b/challenge-042/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..f92b5022d1 --- /dev/null +++ b/challenge-042/adam-russell/perl/ch-2.pl @@ -0,0 +1,39 @@ +use strict; +use warnings; +## +# Write a script to generate a string with a random +# number of ( and ) parentheses. Make the script validate +# the string for balanced parentheses. +## +use boolean; +use constant LENGTH => 4; +use constant OPEN => "("; +use constant CLOSE => ")"; + +sub build{ + my $s; + for (0 .. (LENGTH - 1)){ + my $p = rand() < 0.5 ? OPEN : CLOSE; + $s .= $p; + } + return $s; +} + +sub validate{ + my($s) = @_; + my @a; + for my $c (split(//, $s)){ + push @a, $c if($c eq OPEN); + if($c eq CLOSE){ + return false if(!@a || pop @a ne OPEN); + } + } + return true if !@a; + return false; +} + +MAIN:{ + my $s = build(); + my $r = validate($s) ? "balanced" : "not balanced"; + print "$s is $r\n"; +} |
