From 3ef21f679f78f3672e18a1fb9f6e026ba01c4811 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 7 Jan 2020 13:28:07 +0000 Subject: - Added solutions by Wanderdoc. --- challenge-042/wanderdoc/perl/ch-1.pl | 23 +++++++++++ challenge-042/wanderdoc/perl/ch-2.pl | 75 +++++++++++++++++++++++++++++++++++ challenge-042/wanderdoc/perl5/ch-1.pl | 23 ----------- challenge-042/wanderdoc/perl5/ch-2.pl | 75 ----------------------------------- 4 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 challenge-042/wanderdoc/perl/ch-1.pl create mode 100644 challenge-042/wanderdoc/perl/ch-2.pl delete mode 100644 challenge-042/wanderdoc/perl5/ch-1.pl delete mode 100644 challenge-042/wanderdoc/perl5/ch-2.pl (limited to 'challenge-042') diff --git a/challenge-042/wanderdoc/perl/ch-1.pl b/challenge-042/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..b10a7934cb --- /dev/null +++ b/challenge-042/wanderdoc/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to print decimal number 0 to 50 in Octal Number System. + +For example: +Decimal 0 = Octal 0 +... +Decimal 7 = Octal 7 +Decimal 8 = Octal 10 + +and so on. +=cut + + + +for my $number ( 0 .. 50 ) +{ + print join(' = ', join(' ', 'Decimal', $number), + join(' ', 'Octal', sprintf("%o", $number))), $/; +} \ No newline at end of file diff --git a/challenge-042/wanderdoc/perl/ch-2.pl b/challenge-042/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..41a09ec261 --- /dev/null +++ b/challenge-042/wanderdoc/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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. +For example: +() - OK +(()) - OK +)( - NOT OK +())() - NOT OK +=cut + + + + +use constant { OB => '(', CB => ')' , WEIGHT => 0.25}; +my $FAIR = shift() ? 1 : 0; +my $TOTAL = 50; +my $DEBUG_NUM_OK = 0; +for my $i ( 1 .. $TOTAL ) +{ + my $ml = int((rand(9) + 1)) * 2 + 2; # Should be even, otherwise would be immediately "NOT OK", assumed the string consists of brackets only. + + + + # Making a string. The "simple" version produces too little "OK" strings. + # Therefore: two versions - "simple" and "fair". + my $string = $FAIR ? fair_brackets($ml) : simple_brackets($ml); + + my $counter = 0; + + for my $i (0 .. length($string) - 1) + { + --$counter if ( substr($string, $i, 1) eq CB ); + last if ( $counter < 0 ); + ++$counter if ( substr($string, $i, 1) eq OB ); + } + + + print join(' ', $string, '-', 0 == $counter ? 'OK' : 'NOT OK' ), $/; + ++$DEBUG_NUM_OK if 0 == $counter; +} + +print join(' ', + join('=', 'FAIR BUILD', $FAIR), + join('=', 'DEBUG_NUM_OK', $DEBUG_NUM_OK), + join('=', 'TOTAL', $TOTAL)), $/; + +sub fair_brackets +{ + my $str .= OB; + my $length = $_[0]; + while ( length($str) < $length ) + { + $str .= CB and last if (length($str) + 1 == $length); + + + my $last = substr($str, -1, 1); + my $compl = $last eq OB ? CB : OB; + $str .= rand > WEIGHT ? $compl : $last; + } + return $str; +} + + + +sub simple_brackets +{ + my @brackets = (OB, CB); + my $length = $_[0]; + + my $str = join('', @brackets[ map rand (@brackets) , 1 .. $length ]); + return $str; +} \ No newline at end of file diff --git a/challenge-042/wanderdoc/perl5/ch-1.pl b/challenge-042/wanderdoc/perl5/ch-1.pl deleted file mode 100644 index b10a7934cb..0000000000 --- a/challenge-042/wanderdoc/perl5/ch-1.pl +++ /dev/null @@ -1,23 +0,0 @@ -#!perl -use strict; -use warnings FATAL => qw(all); - -=prompt -Write a script to print decimal number 0 to 50 in Octal Number System. - -For example: -Decimal 0 = Octal 0 -... -Decimal 7 = Octal 7 -Decimal 8 = Octal 10 - -and so on. -=cut - - - -for my $number ( 0 .. 50 ) -{ - print join(' = ', join(' ', 'Decimal', $number), - join(' ', 'Octal', sprintf("%o", $number))), $/; -} \ No newline at end of file diff --git a/challenge-042/wanderdoc/perl5/ch-2.pl b/challenge-042/wanderdoc/perl5/ch-2.pl deleted file mode 100644 index 41a09ec261..0000000000 --- a/challenge-042/wanderdoc/perl5/ch-2.pl +++ /dev/null @@ -1,75 +0,0 @@ -#!perl -use strict; -use warnings FATAL => qw(all); - -=prompt -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. -For example: -() - OK -(()) - OK -)( - NOT OK -())() - NOT OK -=cut - - - - -use constant { OB => '(', CB => ')' , WEIGHT => 0.25}; -my $FAIR = shift() ? 1 : 0; -my $TOTAL = 50; -my $DEBUG_NUM_OK = 0; -for my $i ( 1 .. $TOTAL ) -{ - my $ml = int((rand(9) + 1)) * 2 + 2; # Should be even, otherwise would be immediately "NOT OK", assumed the string consists of brackets only. - - - - # Making a string. The "simple" version produces too little "OK" strings. - # Therefore: two versions - "simple" and "fair". - my $string = $FAIR ? fair_brackets($ml) : simple_brackets($ml); - - my $counter = 0; - - for my $i (0 .. length($string) - 1) - { - --$counter if ( substr($string, $i, 1) eq CB ); - last if ( $counter < 0 ); - ++$counter if ( substr($string, $i, 1) eq OB ); - } - - - print join(' ', $string, '-', 0 == $counter ? 'OK' : 'NOT OK' ), $/; - ++$DEBUG_NUM_OK if 0 == $counter; -} - -print join(' ', - join('=', 'FAIR BUILD', $FAIR), - join('=', 'DEBUG_NUM_OK', $DEBUG_NUM_OK), - join('=', 'TOTAL', $TOTAL)), $/; - -sub fair_brackets -{ - my $str .= OB; - my $length = $_[0]; - while ( length($str) < $length ) - { - $str .= CB and last if (length($str) + 1 == $length); - - - my $last = substr($str, -1, 1); - my $compl = $last eq OB ? CB : OB; - $str .= rand > WEIGHT ? $compl : $last; - } - return $str; -} - - - -sub simple_brackets -{ - my @brackets = (OB, CB); - my $length = $_[0]; - - my $str = join('', @brackets[ map rand (@brackets) , 1 .. $length ]); - return $str; -} \ No newline at end of file -- cgit