diff options
| author | Adam Russell <ac.russell@live.com> | 2020-01-12 16:19:39 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2020-01-12 16:19:39 -0500 |
| commit | e0dff92203a6e930c27dc8c86a10623d295498d0 (patch) | |
| tree | d35492b894dcc3c6298bf2e87d914bbcdfe1d8d7 | |
| parent | 895359996318bf38dd21c0461557e13cc13e4562 (diff) | |
| download | perlweeklychallenge-club-e0dff92203a6e930c27dc8c86a10623d295498d0.tar.gz perlweeklychallenge-club-e0dff92203a6e930c27dc8c86a10623d295498d0.tar.bz2 perlweeklychallenge-club-e0dff92203a6e930c27dc8c86a10623d295498d0.zip | |
solutions for 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"; +} |
