diff options
| author | Duane Powell <duane.r.powell@gmail.com> | 2020-01-07 12:00:33 -0600 |
|---|---|---|
| committer | Duane Powell <duane.r.powell@gmail.com> | 2020-01-07 12:00:33 -0600 |
| commit | fdaf9c2543db12ca0d45c6877f435f3336856e2e (patch) | |
| tree | e63c5e82d1168f2a175912de5e3b112c14694d96 | |
| parent | 8e30607364f6abdd2250591c032126e3e68fdaba (diff) | |
| download | perlweeklychallenge-club-fdaf9c2543db12ca0d45c6877f435f3336856e2e.tar.gz perlweeklychallenge-club-fdaf9c2543db12ca0d45c6877f435f3336856e2e.tar.bz2 perlweeklychallenge-club-fdaf9c2543db12ca0d45c6877f435f3336856e2e.zip | |
Commit solutions for perl weekly challenge 042
| -rwxr-xr-x | challenge-042/duane-powell/perl5/ch-1.pl | 87 | ||||
| -rwxr-xr-x | challenge-042/duane-powell/perl5/ch-2.pl | 61 |
2 files changed, 148 insertions, 0 deletions
diff --git a/challenge-042/duane-powell/perl5/ch-1.pl b/challenge-042/duane-powell/perl5/ch-1.pl new file mode 100755 index 0000000000..774852e2d9 --- /dev/null +++ b/challenge-042/duane-powell/perl5/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw( say ); + +# Write a script to print decimal number 0 to 50 in Octal Number System. + +my $decimal_no = shift; +if ($decimal_no) { + say convert_to_octal($decimal_no); +} +else { + for (0 .. 50) { + say convert_to_octal($_); + } +} + +sub convert_to_octal { + my $n = shift; + return 0 if ($n == 0); + my @oct; + while ($n > 0) { + my $remainder = $n/8 - int($n/8); + push @oct, $remainder * 8; + $n = int($n/8); + } + return join('',reverse @oct); +} + +__END__ + +./ch-1.pl 1023 +1777 + +./ch-1.pl +0 +1 +2 +3 +4 +5 +6 +7 +10 +11 +12 +13 +14 +15 +16 +17 +20 +21 +22 +23 +24 +25 +26 +27 +30 +31 +32 +33 +34 +35 +36 +37 +40 +41 +42 +43 +44 +45 +46 +47 +50 +51 +52 +53 +54 +55 +56 +57 +60 +61 +62 + diff --git a/challenge-042/duane-powell/perl5/ch-2.pl b/challenge-042/duane-powell/perl5/ch-2.pl new file mode 100755 index 0000000000..ca738e1235 --- /dev/null +++ b/challenge-042/duane-powell/perl5/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw( say ); + +# 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 + +use constant { + L_PAREN => '(', + R_PAREN => ')', +}; + +# generate between 1 and 7 parens, if paren string not passed from cmd line +my $paren = shift; +if ($paren) { + say "Given paren string = $paren"; +} +else { + my $lower_limit = 2; + my $upper_limit = 7; + my $random_number = int(rand($upper_limit-$lower_limit)) + $lower_limit; + for (1 .. $random_number) { + # coin toss left or right paren + $paren .= (rand() < 0.5) ? L_PAREN : R_PAREN; + } + say "Random paren string = $paren"; +} + +# test if parens are balanced +my $msg; +my $paren_count = 0; +foreach (split(//,$paren)) { + $paren_count-- if ($_ eq R_PAREN); + $paren_count++ if ($_ eq L_PAREN); + if ($paren_count < 0) { + $msg = "Parens are not balanced."; + last; + } +} +$msg = 'Parens are balanced.' if ($paren_count == 0); +$msg = 'Parens are not closed.' if ($paren_count > 0); +say $msg; + +__END__ + +./ch-2.pl +random paren string = () +Parens are balanced. + +./ch-2.pl "((())())" +Given paren string = ((())()) +Parens are balanced. + +./ch-2.pl ")(()()(((()))))" +Given paren string = )(()()(((())))) +Parens are not balanced. + +./ch-2.pl "((())" +Given paren string = ((()) +Parens are not closed. + |
