From 888461dd14f7c29adbc0a49f1cb964504bd3929d Mon Sep 17 00:00:00 2001 From: Burkhard Nickels Date: Fri, 10 Jan 2020 22:14:26 +0100 Subject: Solution for PWC 42 from Burkhard Nickels. --- challenge-042/burkhard-nickels/README | 1 + challenge-042/burkhard-nickels/blogs.txt | 1 + challenge-042/burkhard-nickels/perl5/ch-1.html | 170 +++++++++++++ challenge-042/burkhard-nickels/perl5/ch-1.pl | 15 ++ challenge-042/burkhard-nickels/perl5/ch-1.pod | 165 ++++++++++++ challenge-042/burkhard-nickels/perl5/ch-1.py | 19 ++ challenge-042/burkhard-nickels/perl5/ch-2.html | 335 +++++++++++++++++++++++++ challenge-042/burkhard-nickels/perl5/ch-2.pl | 41 +++ challenge-042/burkhard-nickels/perl5/ch-2.pod | 317 +++++++++++++++++++++++ challenge-042/burkhard-nickels/perl5/ch-2.py | 38 +++ challenge-042/burkhard-nickels/perl6/ch-1.p6 | 14 ++ challenge-042/burkhard-nickels/perl6/ch-2.p6 | 40 +++ 12 files changed, 1156 insertions(+) create mode 100644 challenge-042/burkhard-nickels/README create mode 100644 challenge-042/burkhard-nickels/blogs.txt create mode 100644 challenge-042/burkhard-nickels/perl5/ch-1.html create mode 100755 challenge-042/burkhard-nickels/perl5/ch-1.pl create mode 100755 challenge-042/burkhard-nickels/perl5/ch-1.pod create mode 100755 challenge-042/burkhard-nickels/perl5/ch-1.py create mode 100644 challenge-042/burkhard-nickels/perl5/ch-2.html create mode 100755 challenge-042/burkhard-nickels/perl5/ch-2.pl create mode 100755 challenge-042/burkhard-nickels/perl5/ch-2.pod create mode 100755 challenge-042/burkhard-nickels/perl5/ch-2.py create mode 100755 challenge-042/burkhard-nickels/perl6/ch-1.p6 create mode 100755 challenge-042/burkhard-nickels/perl6/ch-2.p6 diff --git a/challenge-042/burkhard-nickels/README b/challenge-042/burkhard-nickels/README new file mode 100644 index 0000000000..f5e16bb98b --- /dev/null +++ b/challenge-042/burkhard-nickels/README @@ -0,0 +1 @@ +Solutions by Burkhard Nickels. diff --git a/challenge-042/burkhard-nickels/blogs.txt b/challenge-042/burkhard-nickels/blogs.txt new file mode 100644 index 0000000000..9cb0ea99b5 --- /dev/null +++ b/challenge-042/burkhard-nickels/blogs.txt @@ -0,0 +1 @@ +http://pearls.dyndnss.net diff --git a/challenge-042/burkhard-nickels/perl5/ch-1.html b/challenge-042/burkhard-nickels/perl5/ch-1.html new file mode 100644 index 0000000000..ea1c43b93a --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-1.html @@ -0,0 +1,170 @@ + + + + +Perl Weekly Challenge #42 Task #1, Octal Numbers + + + + + + + + +
+ Perl Weekly Challenge #42 Task #1, Octal Numbers +
+ + + + + +

Perl Weekly Challenge #42 Task #1: Octal Numbers

+ +

The octal number is printed with printf or sprintf and the format string %o. I provided two solutions, first with a foreach loop (Perl5) or for loop (Perl6), second with a map on one line.

+ +

Following are some possibilities to deal with Octal numbers:

+ + + +

Download and References

+Download File: Perl5 Solution PWC #42 Task #1 ch-1.pl
+Download File: Perl6 Solution PWC #42 Task #1 ch-1.p6
+Download File: Python Solution PWC #42 Task #1 ch-1.py
+
+ +

SYNOPSIS

+ +
 # ./ch-1.pl                    - Program execution
+ # ./ch-1.p6                    - Program execution
+ # perldoc ch-1.pod             - POD
+ +

Definition Task #1: Octal Number System

+ +

Write a script to print decimal number 0 to 50 in Octal Number System.

+ +
 For example:
+ Decimal 0 = Octal 0
+ Decimal 1 = Octal 1
+ Decimal 2 = Octal 2
+ Decimal 3 = Octal 3
+ Decimal 4 = Octal 4
+ Decimal 5 = Octal 5
+ Decimal 6 = Octal 6
+ Decimal 7 = Octal 7
+ Decimal 8 = Octal 10
+ +

and so on.

+ +

Sourcecode for Perl5 and Perl6

+ +

Most important difference between Perl5 and Perl6 in this code is the loop statement foreach(1 .. 50) in Perl5 and for (1 .. 50) in Perl6. Not only that foreach becomes for, but also the important blank behind the for keyword.

+ +

The printf() / sprintf() calls are the same. the map {}, (1 .. 50) is different in the comma behind the block in the Perl6 part.

+ +
+
Perl5
+ 1 #!/usr/bin/perl + 2 + 3 use strict; + 4 use warnings; + 5 + 6 print "ch-1.pl (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + 7 + 8 # Version 1: foreach loop with printf + 9 foreach (0 .. 50) { + 10 printf("Decimal: %d - Octal: %o\n",$_,$_); + 11 } + 12 + 13 # Version 2: map with sprintf + 14 print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) } (0 .. 50); +
+
+
Perl6
+ 1 #!/home/chuck/rakudo/bin/perl6 + 2 + 3 use strict; + 4 + 5 print "ch-1.p6 (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + 6 + 7 # Version 1: foreach loop with printf + 8 for (0 .. 50) { + 9 printf("Decimal: %d - Octal: %o\n",$_,$_); + 10 } + 11 + 12 # Version 2: map with sprintf + 13 print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) }, (0 .. 50); + 14 +
+
+ +

Sourcecode for Python

+ +

In Python I tried to implement the loop and the map way I did in Perl. So below you can see these trials. The for loop is very easy and similar. The map() way seems to be a little bit more complicated than in Perl.

+ +
+
Python
+ 1 #!/usr/bin/python + 2 + 3 import array as arr + 4 + 5 print "ch-1.py (Version 1.0) PWC #42 Task #1: Octal numbers." + 6 + 7 for i in range(0,50): + 8 print "Decimal: ", i, " - Octal: ", oct(i) + 9 + 10 + 11 def print_string(n): + 12 buf = "Decimal: %d, Octal: %o" % (n, n) + 13 return buf + 14 + 15 a = list(range(0,50)) + 16 x = map(print_string, a) + 17 # print(list(x)) + 18 for e in x: print e + 19 +
+
+ +

AUTHOR

+ +

Chuck

+ + + +
+ Perl Weekly Challenge #42 Task #1, Octal Numbers +
+ + + + + + diff --git a/challenge-042/burkhard-nickels/perl5/ch-1.pl b/challenge-042/burkhard-nickels/perl5/ch-1.pl new file mode 100755 index 0000000000..d908c529b5 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +print "ch-1.pl (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + +# Version 1: foreach loop with printf +foreach (0 .. 50) { + printf("Decimal: %d - Octal: %o\n",$_,$_); +} + +# Version 2: map with sprintf +print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) } (0 .. 50); + diff --git a/challenge-042/burkhard-nickels/perl5/ch-1.pod b/challenge-042/burkhard-nickels/perl5/ch-1.pod new file mode 100755 index 0000000000..1a37383fc2 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-1.pod @@ -0,0 +1,165 @@ +#!/usr/bin/perldoc + +=head1 Perl Weekly Challenge #42 Task #1: Octal Numbers + +The octal number is printed with C or C and the format string +C<%o>. I provided two solutions, first with a C loop (Perl5) or C loop +(Perl6), second with a C on one line. + +Following are some possibilities to deal with Octal numbers: + +=over 3 + +=item * printf, sprintf and "%o" + Print with format string used in this solution. + +=item * foreach(0 .. 50) / for (0 .. 50) (Perl5/Perl6) + Loop over values from 1 to 50 in Perl5 and Perl6. + +=item * @result = map { ... } (0 .. 50) + Loop over values from 1 to 50 with map function. + +=item * my $number = 0644; + Assignment of octal number. + +=item * my $octal = oct(644); + Convert scalar to octal number. + +=item * (s)printf("0%o %d", $number, $number); + Print number as octal and decimal. + +=back + +=begin html + +

Download and References

+Download File: Perl5 Solution PWC #42 Task #1 ch-1.pl
+Download File: Perl6 Solution PWC #42 Task #1 ch-1.p6
+Download File: Python Solution PWC #42 Task #1 ch-1.py
+
+ +=end html + +=head1 SYNOPSIS + + # ./ch-1.pl - Program execution + # ./ch-1.p6 - Program execution + # perldoc ch-1.pod - POD + +=cut + +# ====================== TASK 1 ============================== + +=head1 Definition Task #1: Octal Number System + +Write a script to print decimal number 0 to 50 in Octal Number System. + + For example: + Decimal 0 = Octal 0 + Decimal 1 = Octal 1 + Decimal 2 = Octal 2 + Decimal 3 = Octal 3 + Decimal 4 = Octal 4 + Decimal 5 = Octal 5 + Decimal 6 = Octal 6 + Decimal 7 = Octal 7 + Decimal 8 = Octal 10 + +and so on. + + +=head1 Sourcecode for Perl5 and Perl6 + +Most important difference between Perl5 and Perl6 in this code is the loop statement +C in Perl5 and C in Perl6. Not only that foreach +becomes for, but also the important blank behind the B keyword. + +The C calls are the same. the C is different +in the comma behind the block in the Perl6 part. + +=begin html + +
+
Perl5
+ 1 #!/usr/bin/perl + 2 + 3 use strict; + 4 use warnings; + 5 + 6 print "ch-1.pl (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + 7 + 8 # Version 1: foreach loop with printf + 9 foreach (0 .. 50) { + 10 printf("Decimal: %d - Octal: %o\n",$_,$_); + 11 } + 12 + 13 # Version 2: map with sprintf + 14 print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) } (0 .. 50); +
+
+
Perl6
+ 1 #!/home/chuck/rakudo/bin/perl6 + 2 + 3 use strict; + 4 + 5 print "ch-1.p6 (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + 6 + 7 # Version 1: foreach loop with printf + 8 for (0 .. 50) { + 9 printf("Decimal: %d - Octal: %o\n",$_,$_); + 10 } + 11 + 12 # Version 2: map with sprintf + 13 print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) }, (0 .. 50); + 14 +
+
+ +=end html + + +=head1 Sourcecode for Python + +In Python I tried to implement the loop and the map way I did in Perl. So below +you can see these trials. The C loop is very easy and similar. The C +way seems to be a little bit more complicated than in Perl. + +=begin html + +
+
Python
+ 1 #!/usr/bin/python + 2 + 3 import array as arr + 4 + 5 print "ch-1.py (Version 1.0) PWC #42 Task #1: Octal numbers." + 6 + 7 for i in range(0,50): + 8 print "Decimal: ", i, " - Octal: ", oct(i) + 9 + 10 + 11 def print_string(n): + 12 buf = "Decimal: %d, Octal: %o" % (n, n) + 13 return buf + 14 + 15 a = list(range(0,50)) + 16 x = map(print_string, a) + 17 # print(list(x)) + 18 for e in x: print e + 19 +
+
+ +=end html + + +=cut + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-042/burkhard-nickels/perl5/ch-1.py b/challenge-042/burkhard-nickels/perl5/ch-1.py new file mode 100755 index 0000000000..40388def78 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-1.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import array as arr + +print "ch-1.py (Version 1.0) PWC #42 Task #1: Octal numbers." + +for i in range(0,50): + print "Decimal: ", i, " - Octal: ", oct(i) + + +def print_string(n): + buf = "Decimal: %d, Octal: %o" % (n, n) + return buf + +a = list(range(0,50)) +x = map(print_string, a) +# print(list(x)) +for e in x: print e + diff --git a/challenge-042/burkhard-nickels/perl5/ch-2.html b/challenge-042/burkhard-nickels/perl5/ch-2.html new file mode 100644 index 0000000000..65f20b4cd6 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-2.html @@ -0,0 +1,335 @@ + + + + +Perl Weekly Challenge #42 Task #2, Balanced Brackets + + + + + + + + +
+ Perl Weekly Challenge #42 Task #2, Balanced Brackets +
+ + + + + +

Perl Weekly Challenge #42 Task #2: Balanced Brackets

+ +

This Task "Balanced Brackets" I solved in 3 languages: Perl5, Perl6 and Python. My personal aim here is also to learn Perl6 and Python beside the eager in solving the Tasks in Perl5.

+ +

Some highlights:

+ +
    + +
  • Perl5, Perl6 and Python solution.

    + +
  • +
  • Recursive function call.

    + +
  • +
  • do-while, do-repeat or while-if-break as DO-WHILE loop.

    + +
  • +
  • random integer number generation.

    + +
  • +
+ +

Download and References

+Download File: Perl5 Solution PWC #42 Task #2 ch-2.pl
+Download File: Perl6 Solution PWC #42 Task #2 ch-2.p6
+Download File: Python Solution PWC #42 Task #2 ch-2.py
+
+ +

SYNOPSIS

+ +
 # ./ch-2.pl            - Execution of program
+ # ./ch-2.p6            - Execution of program
+ # ./ch-2.py            - Execution of program
+ # perldoc ch-2.pod             - POD
+ +

Definition Task #2: Balanced Brackets

+ +

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
+ +

Perl5 and Perl6

+ +

How does the program work? Within a loop, that is left when some Balanced Brackets are found, a random number of brackets is created with the function create_brackets(). Than the bracket string is verified if it is Balanced Brackets in the function balanced_brackets(). The function balanced_brackets() is called recursively as long as a pair of brackets can be found with search/replace.

+ +

Perl Example Execution

+ +
 # ./ch-2.pl
+ ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets
+ (()))()))) - NOT OK
+ () - OK
+ +

Source Code

+ +

The code for Perl5 and Perl6 are similar, only the differences in the language. Let me mention some of the differences between Perl5 / Perl6:

+ +
    + +
  • for() / loop (), Remember the blank in Perl6.

    + +
  • +
  • .= / ~=, Assignment of a String.

    + +
  • +
  • int(rand(2)) / Int(2.rand), Create a random integer.

    + +
  • +
  • if() / if (), Again the blank after the statement, in Perl6 brackets directly after the keyword means to call a function.

    + +
  • +
  • sub balanced_brackets { my $str = shift; ... } sub balanced_brackets ($arg) { ... } Different function call syntax.

    + +
  • +
  • =~ / ~~, for applying the search/replace on a scalar.

    + +
  • +
  • do {} while() / repeat {} while (), A do-while loop becomes repeat-while.

    + +
  • +
+ +
+
Perl5
+ 1 #!/usr/bin/perl + 2 + 3 use strict; + 4 use warnings; + 5 + 6 print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 7 + 8 sub create_brackets { + 9 my ($nr) = @_; + 10 my $s; + 11 for( my $i=0; $i<=$nr; $i++ ) { + 12 my $br = int(rand(2)); + 13 if($br) { $s .= ")"; } else { $s .= "("; } + 14 } + 15 return $s; + 16 } + 17 + 18 my $ok; + 19 do { + 20 my $nr = int(rand(10)); + 21 my $str = create_brackets($nr); + 22 $ok = balanced_brackets($str); + 23 my $rs = "NOT OK"; + 24 $rs = "OK" if $ok; + 25 print $str, " - ", $rs, "\n"; + 26 } while( ! $ok ); + 27 + 28 sub balanced_brackets { + 29 my $str = shift; + 30 my $found = $str =~ s/\(\)//; + 31 my $ok; + 32 if($found) { + 33 $ok = balanced_brackets($str); + 34 return $ok; + 35 } + 36 else { + 37 if( $str=~/\(|\)/ ) { return 0; } + 38 else { return 1; } + 39 } + 40 } +
+
+
Perl6
+ 1 #!/home/chuck/rakudo/bin/perl6 + 2 + 3 use strict; + 4 + 5 print "ch-2.p6 (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 6 + 7 sub create_brackets { + 8 my ($nr) = @_; + 9 my $s; + 10 loop ( my $i=0; $i <= $nr; $i++ ) { + 11 my $br = Int(2.rand); + 12 if ($br) { $s ~= ")"; } else { $s ~= "("; } + 13 } + 14 return $s; + 15 } + 16 + 17 my $ok; + 18 repeat { + 19 my $nr = Int(10.rand); + 20 my $str = create_brackets($nr); + 21 $ok = balanced_brackets($str); + 22 my $rs = "NOT OK"; + 23 $rs = "OK" if $ok; + 24 print $str, " - ", $rs, "\n"; + 25 } while ( ! $ok ); + 26 + 27 sub balanced_brackets ($arg) { + 28 my $str = $arg; # Cannot assign to a read-only. + 29 my $found = ($str ~~ s/\(\)//); + 30 my $ok; + 31 if ($found) { + 32 $ok = balanced_brackets($str); + 33 return $ok; + 34 } + 35 else { + 36 if ( $str ~~ /\(|\)/ ) { return 0; } + 37 else { return 1; } + 38 } + 39 } + 40 +
+
+ +

Python

+ +

Because the syntax is rather different, it takes some more time to convert the code to Python. But I succeeded and can publish also a Python code for this PWC Task.

+ +

Python Example Execution

+ +
 # ./ch-2.py
+ ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets
+
+ ( 1)          ( = NOT OK
+ ( 2)     (((()( = NOT OK
+ ( 3)          ) = NOT OK
+ ( 4)     (()))( = NOT OK
+ ( 5)         (( = NOT OK
+ ( 6)   ))))(()) = NOT OK
+ ( 7)       (()( = NOT OK
+ ( 8)        )(( = NOT OK
+ ( 9)    ()((((( = NOT OK
+ (10)     )())() = NOT OK
+ (11)   (()())() = OK
+ +

Source Code Python

+ +

The Python source code is similar to the two Perl examples. The defined functions are the same. Because the syntax is very different to the two Perl examples, it is up to you to investigate in its details.

+ +

Beside all the other differences I want to mention the loop while True: ... if condition: break instead of the do-while or repeat-while in Perl5 or Perl6.

+ +
+
Python
+ 1 #!/usr/bin/python + 2 + 3 import array as arr + 4 import re + 5 import random + 6 + 7 print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 8 + 9 def create_brackets(nr): + 10 s = "" + 11 for i in range(1,nr): + 12 br = random.randint(0,1) + 13 if br: s += ")" + 14 else : s += "(" + 15 return s + 16 + 17 def balanced_brackets(brs): + 18 brs, found = re.subn('\(\)','',brs); + 19 ok = 0 + 20 if found: + 21 ok = balanced_brackets(brs) + 22 return ok + 23 else: + 24 if re.search('\(|\)',brs): return 0 + 25 else: return 1 + 26 + 27 loops = 0 + 28 while True: + 29 loops = loops + 1 + 30 nr = random.randint(2,10) + 31 brs = create_brackets(nr) + 32 ok = balanced_brackets(brs) + 33 rs = "NOT OK" + 34 if ok: rs = "OK" + 35 print("(%2d) %10s = %s" % (loops, brs, rs)) + 36 if ok: break + 37 if loops > 20: break +
+
+ +

Python help

+ +

One important question as a biginner is: How do I get help? One answer is to search the net. The other is use the command line tools shipped with your Python. Below I, as a biginner, can see how to use the offline help:

+ +

In short: Start python CLI, use help() command, type the class or function you want to find, i.e. re.

+ +
 # python
+ Python 2.7.13 (default, Sep 26 2018, 18:42:22) 
+ [GCC 6.3.0 20170516] on linux2
+ Type "help", "copyright", "credits" or "license" for more information.
+ >>> help()
+
+ Welcome to Python 2.7!  This is the online help utility.
+
+ If this is your first time using Python, you should definitely check out
+ the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.
+
+ Enter the name of any module, keyword, or topic to get help on writing
+ Python programs and using Python modules.  To quit this help utility and
+ return to the interpreter, just type "quit".
+
+ To get a list of available modules, keywords, or topics, type "modules",
+ "keywords", or "topics".  Each module also comes with a one-line summary
+ of what it does; to list the modules whose summaries contain a given word
+ such as "spam", type "modules spam".
+
+ help> re
+ ...
+ /subn
+ ...
+ q
+ help> quit
+
+ You are now leaving help and returning to the Python interpreter.
+ If you want to ask for help on a particular object directly from the
+ interpreter, you can type "help(object)".  Executing "help('string')"
+ has the same effect as typing a particular string at the help> prompt.
+ >>> quit()
+ +

AUTHOR

+ +

Chuck

+ + + +
+ Perl Weekly Challenge #42 Task #2, Balanced Brackets +
+ + + + + + diff --git a/challenge-042/burkhard-nickels/perl5/ch-2.pl b/challenge-042/burkhard-nickels/perl5/ch-2.pl new file mode 100755 index 0000000000..4ac103997e --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + +sub create_brackets { + my ($nr) = @_; + my $s; + for( my $i=0; $i<=$nr; $i++ ) { + my $br = int(rand(2)); + if($br) { $s .= ")"; } else { $s .= "("; } + } + return $s; +} + +my $ok; +do { + my $nr = int(rand(10)); + my $str = create_brackets($nr); + $ok = balanced_brackets($str); + my $rs = "NOT OK"; + $rs = "OK" if $ok; + print $str, " - ", $rs, "\n"; +} while( ! $ok ); + +sub balanced_brackets { + my $str = shift; + my $found = $str =~ s/\(\)//; + my $ok; + if($found) { + $ok = balanced_brackets($str); + return $ok; + } + else { + if( $str=~/\(|\)/ ) { return 0; } + else { return 1; } + } +} + diff --git a/challenge-042/burkhard-nickels/perl5/ch-2.pod b/challenge-042/burkhard-nickels/perl5/ch-2.pod new file mode 100755 index 0000000000..917205d746 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-2.pod @@ -0,0 +1,317 @@ +#!/usr/bin/perldoc + +=head1 Perl Weekly Challenge #42 Task #2: Balanced Brackets + +This Task "Balanced Brackets" I solved in 3 languages: Perl5, Perl6 and Python. +My personal aim here is also to learn Perl6 and Python beside the eager in solving +the Tasks in Perl5. + +Some highlights: + +=over 3 + +=item * Perl5, Perl6 and Python solution. + +=item * Recursive function call. + +=item * do-while, do-repeat or while-if-break as DO-WHILE loop. + +=item * random integer number generation. + +=back + + +=begin html + +

Download and References

+Download File: Perl5 Solution PWC #42 Task #2 ch-2.pl
+Download File: Perl6 Solution PWC #42 Task #2 ch-2.p6
+Download File: Python Solution PWC #42 Task #2 ch-2.py
+
+ +=end html + +=head1 SYNOPSIS + + # ./ch-2.pl - Execution of program + # ./ch-2.p6 - Execution of program + # ./ch-2.py - Execution of program + # perldoc ch-2.pod - POD + +=cut + +# ====================== TASK 1 ============================== + +=head1 Definition Task #2: Balanced Brackets + +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 + +=head1 Perl5 and Perl6 + +How does the program work? Within a loop, that is left when some B +are found, a random number of brackets is created with the function C. +Than the bracket string is verified if it is B in the function +C. The function C is called recursively as +long as a pair of brackets can be found with search/replace. + +=head2 Perl Example Execution + + # ./ch-2.pl + ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets + (()))()))) - NOT OK + () - OK + +=head2 Source Code + +The code for Perl5 and Perl6 are similar, only the differences in the language. +Let me mention some of the differences between B: + +=over 2 + +=item * for() / loop (), Remember the blank in Perl6. + +=item * .= / ~=, Assignment of a String. + +=item * int(rand(2)) / Int(2.rand), Create a random integer. + +=item * if() / if (), Again the blank after the statement, in Perl6 brackets + directly after the keyword means to call a function. + +=item * sub balanced_brackets { my $str = shift; ... } + sub balanced_brackets ($arg) { ... } + Different function call syntax. + +=item * =~ / ~~, for applying the search/replace on a scalar. + +=item * do {} while() / repeat {} while (), A do-while loop becomes repeat-while. + +=back + +=begin html + +
+
Perl5
+ 1 #!/usr/bin/perl + 2 + 3 use strict; + 4 use warnings; + 5 + 6 print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 7 + 8 sub create_brackets { + 9 my ($nr) = @_; + 10 my $s; + 11 for( my $i=0; $i<=$nr; $i++ ) { + 12 my $br = int(rand(2)); + 13 if($br) { $s .= ")"; } else { $s .= "("; } + 14 } + 15 return $s; + 16 } + 17 + 18 my $ok; + 19 do { + 20 my $nr = int(rand(10)); + 21 my $str = create_brackets($nr); + 22 $ok = balanced_brackets($str); + 23 my $rs = "NOT OK"; + 24 $rs = "OK" if $ok; + 25 print $str, " - ", $rs, "\n"; + 26 } while( ! $ok ); + 27 + 28 sub balanced_brackets { + 29 my $str = shift; + 30 my $found = $str =~ s/\(\)//; + 31 my $ok; + 32 if($found) { + 33 $ok = balanced_brackets($str); + 34 return $ok; + 35 } + 36 else { + 37 if( $str=~/\(|\)/ ) { return 0; } + 38 else { return 1; } + 39 } + 40 } +
+
+
Perl6
+ 1 #!/home/chuck/rakudo/bin/perl6 + 2 + 3 use strict; + 4 + 5 print "ch-2.p6 (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 6 + 7 sub create_brackets { + 8 my ($nr) = @_; + 9 my $s; + 10 loop ( my $i=0; $i <= $nr; $i++ ) { + 11 my $br = Int(2.rand); + 12 if ($br) { $s ~= ")"; } else { $s ~= "("; } + 13 } + 14 return $s; + 15 } + 16 + 17 my $ok; + 18 repeat { + 19 my $nr = Int(10.rand); + 20 my $str = create_brackets($nr); + 21 $ok = balanced_brackets($str); + 22 my $rs = "NOT OK"; + 23 $rs = "OK" if $ok; + 24 print $str, " - ", $rs, "\n"; + 25 } while ( ! $ok ); + 26 + 27 sub balanced_brackets ($arg) { + 28 my $str = $arg; # Cannot assign to a read-only. + 29 my $found = ($str ~~ s/\(\)//); + 30 my $ok; + 31 if ($found) { + 32 $ok = balanced_brackets($str); + 33 return $ok; + 34 } + 35 else { + 36 if ( $str ~~ /\(|\)/ ) { return 0; } + 37 else { return 1; } + 38 } + 39 } + 40 +
+
+ +=end html + +=head1 Python + +Because the syntax is rather different, it takes some more time to convert the code +to Python. But I succeeded and can publish also a Python code for this PWC Task. + +=head2 Python Example Execution + + # ./ch-2.py + ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets + + ( 1) ( = NOT OK + ( 2) (((()( = NOT OK + ( 3) ) = NOT OK + ( 4) (()))( = NOT OK + ( 5) (( = NOT OK + ( 6) ))))(()) = NOT OK + ( 7) (()( = NOT OK + ( 8) )(( = NOT OK + ( 9) ()((((( = NOT OK + (10) )())() = NOT OK + (11) (()())() = OK + +=head2 Source Code Python + +The Python source code is similar to the two Perl examples. The defined functions +are the same. Because the syntax is very different to the two Perl examples, it is +up to you to investigate in its details. + +Beside all the other differences I want to mention the loop +C instead of the do-while or repeat-while +in Perl5 or Perl6. + +=begin html + +
+
Python
+ 1 #!/usr/bin/python + 2 + 3 import array as arr + 4 import re + 5 import random + 6 + 7 print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + 8 + 9 def create_brackets(nr): + 10 s = "" + 11 for i in range(1,nr): + 12 br = random.randint(0,1) + 13 if br: s += ")" + 14 else : s += "(" + 15 return s + 16 + 17 def balanced_brackets(brs): + 18 brs, found = re.subn('\(\)','',brs); + 19 ok = 0 + 20 if found: + 21 ok = balanced_brackets(brs) + 22 return ok + 23 else: + 24 if re.search('\(|\)',brs): return 0 + 25 else: return 1 + 26 + 27 loops = 0 + 28 while True: + 29 loops = loops + 1 + 30 nr = random.randint(2,10) + 31 brs = create_brackets(nr) + 32 ok = balanced_brackets(brs) + 33 rs = "NOT OK" + 34 if ok: rs = "OK" + 35 print("(%2d) %10s = %s" % (loops, brs, rs)) + 36 if ok: break + 37 if loops > 20: break +
+
+ +=end html + +=head2 Python help + +One important question as a biginner is: How do I get help? One answer is to search the net. +The other is use the command line tools shipped with your Python. Below I, as a biginner, +can see how to use the offline help: + +In short: Start python CLI, use C command, type the class or function you want +to find, i.e. C. + + # python + Python 2.7.13 (default, Sep 26 2018, 18:42:22) + [GCC 6.3.0 20170516] on linux2 + Type "help", "copyright", "credits" or "license" for more information. + >>> help() + + Welcome to Python 2.7! This is the online help utility. + + If this is your first time using Python, you should definitely check out + the tutorial on the Internet at http://docs.python.org/2.7/tutorial/. + + Enter the name of any module, keyword, or topic to get help on writing + Python programs and using Python modules. To quit this help utility and + return to the interpreter, just type "quit". + + To get a list of available modules, keywords, or topics, type "modules", + "keywords", or "topics". Each module also comes with a one-line summary + of what it does; to list the modules whose summaries contain a given word + such as "spam", type "modules spam". + + help> re + ... + /subn + ... + q + help> quit + + You are now leaving help and returning to the Python interpreter. + If you want to ask for help on a particular object directly from the + interpreter, you can type "help(object)". Executing "help('string')" + has the same effect as typing a particular string at the help> prompt. + >>> quit() + + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-042/burkhard-nickels/perl5/ch-2.py b/challenge-042/burkhard-nickels/perl5/ch-2.py new file mode 100755 index 0000000000..a52447f036 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl5/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python + +import array as arr +import re +import random + +print "ch-2.pl (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + +def create_brackets(nr): + s = "" + for i in range(1,nr): + br = random.randint(0,1) + if br: s += ")" + else : s += "(" + return s + +def balanced_brackets(brs): + brs, found = re.subn('\(\)','',brs); + ok = 0 + if found: + ok = balanced_brackets(brs) + return ok + else: + if re.search('\(|\)',brs): return 0 + else: return 1 + +loops = 0 +while True: + loops = loops + 1 + nr = random.randint(2,10) + brs = create_brackets(nr) + ok = balanced_brackets(brs) + rs = "NOT OK" + if ok: rs = "OK" + print("(%2d) %10s = %s" % (loops, brs, rs)) + if ok: break + if loops > 20: break + diff --git a/challenge-042/burkhard-nickels/perl6/ch-1.p6 b/challenge-042/burkhard-nickels/perl6/ch-1.p6 new file mode 100755 index 0000000000..8086834ec4 --- /dev/null +++ b/challenge-042/burkhard-nickels/perl6/ch-1.p6 @@ -0,0 +1,14 @@ +#!/home/chuck/rakudo/bin/perl6 + +use strict; + +print "ch-1.p6 (Version 1.0) PWC #42 Task #1: Octal Numbers\n"; + +# Version 1: foreach loop with printf +for (0 .. 50) { + printf("Decimal: %d - Octal: %o\n",$_,$_); +} + +# Version 2: map with sprintf +print my @res = map { sprintf("Dec %d - Oct %o\n",$_,$_) }, (0 .. 50); + diff --git a/challenge-042/burkhard-nickels/perl6/ch-2.p6 b/challenge-042/burkhard-nickels/perl6/ch-2.p6 new file mode 100755 index 0000000000..55ba8b94fc --- /dev/null +++ b/challenge-042/burkhard-nickels/perl6/ch-2.p6 @@ -0,0 +1,40 @@ +#!/home/chuck/rakudo/bin/perl6 + +use strict; + +print "ch-2.p6 (Version 1.0) PWC #42 Task #2: Balanced Brackets\n"; + +sub create_brackets { + my ($nr) = @_; + my $s; + loop ( my $i=0; $i <= $nr; $i++ ) { + my $br = Int(2.rand); + if ($br) { $s ~= ")"; } else { $s ~= "("; } + } + return $s; +} + +my $ok; +repeat { + my $nr = Int(10.rand); + my $str = create_brackets($nr); + $ok = balanced_brackets($str); + my $rs = "NOT OK"; + $rs = "OK" if $ok; + print $str, " - ", $rs, "\n"; +} while ( ! $ok ); + +sub balanced_brackets ($arg) { + my $str = $arg; # Cannot assign to a read-only. + my $found = ($str ~~ s/\(\)//); + my $ok; + if ($found) { + $ok = balanced_brackets($str); + return $ok; + } + else { + if ( $str ~~ /\(|\)/ ) { return 0; } + else { return 1; } + } +} + -- cgit