From ea0e10fea656e93328f21ee3ae2ce6390ebf4181 Mon Sep 17 00:00:00 2001 From: Burkhard Nickels Date: Mon, 30 Dec 2019 00:02:16 +0100 Subject: Solution for PWC 40 from Burkhard Nickels. --- challenge-040/burkhard-nickels/README | 1 + challenge-040/burkhard-nickels/blogs.txt | 1 + challenge-040/burkhard-nickels/perl5/ch-1.pl | 301 +++++++++++++++++++++++++++ challenge-040/burkhard-nickels/perl5/ch-1.py | 14 ++ challenge-040/burkhard-nickels/perl5/ch-2.pl | 219 +++++++++++++++++++ challenge-040/burkhard-nickels/perl5/ch-2.py | 30 +++ challenge-040/burkhard-nickels/perl6/ch-1.p6 | 61 ++++++ challenge-040/burkhard-nickels/perl6/ch-2.p6 | 17 ++ 8 files changed, 644 insertions(+) create mode 100644 challenge-040/burkhard-nickels/README create mode 100644 challenge-040/burkhard-nickels/blogs.txt create mode 100755 challenge-040/burkhard-nickels/perl5/ch-1.pl create mode 100755 challenge-040/burkhard-nickels/perl5/ch-1.py create mode 100755 challenge-040/burkhard-nickels/perl5/ch-2.pl create mode 100755 challenge-040/burkhard-nickels/perl5/ch-2.py create mode 100755 challenge-040/burkhard-nickels/perl6/ch-1.p6 create mode 100755 challenge-040/burkhard-nickels/perl6/ch-2.p6 diff --git a/challenge-040/burkhard-nickels/README b/challenge-040/burkhard-nickels/README new file mode 100644 index 0000000000..f5e16bb98b --- /dev/null +++ b/challenge-040/burkhard-nickels/README @@ -0,0 +1 @@ +Solutions by Burkhard Nickels. diff --git a/challenge-040/burkhard-nickels/blogs.txt b/challenge-040/burkhard-nickels/blogs.txt new file mode 100644 index 0000000000..9cb0ea99b5 --- /dev/null +++ b/challenge-040/burkhard-nickels/blogs.txt @@ -0,0 +1 @@ +http://pearls.dyndnss.net diff --git a/challenge-040/burkhard-nickels/perl5/ch-1.pl b/challenge-040/burkhard-nickels/perl5/ch-1.pl new file mode 100755 index 0000000000..af6bee85fd --- /dev/null +++ b/challenge-040/burkhard-nickels/perl5/ch-1.pl @@ -0,0 +1,301 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +=head1 Perl Weekly Challenge #40 Task #1: Show multiple arrays content. + +The code is an iteration with a C loop through its elements by index. + +I compare three solutions, first with Perl5, second with Perl6 and third +also with Python. For me it is a learning excercise in Perl6 and Python. + +So the special thing here are the 3 used script languages: + +=over 3 + +=item * Perl5 Solution + +=item * Perl6 Solution + +=item * Python Solution + +=back + + +=begin html + +

Download and References

+Download File:Solution PWC #40 Task #1 ch-1.pl
+Download File:Solution PWC #40 Task #2 ch-2.pl
+Download File:Solution PWC #40 Task #1 ch-1.p6
+Download File:Solution PWC #40 Task #2 ch-2.p6
+Download File:Solution PWC #40 Task #1 ch-1.py
+Download File:Solution PWC #40 Task #2 ch-2.py
+
+ + Perl5 to Perl6 in a Nutshell
+ +https://docs.perl6.org/language/5to6-nutshell
+
+ Raku Website
+ +https://raku.org/
+ +=end html + +=head1 SYNOPSIS + + # ./ch-1.pl + ch-1.pl (Version 1.0) PWC #40 Task #1: Show multiple arrays content. + I 2 ! + L 4 ? + O 0 P + V 3 $ + E 2 % + Y 0 ^ + O 1 & + U 9 * + + # perldoc ch-1.pl - POD + # ./ch-1.pl html - HTML/CSS in ch-1.html/pwc.css + # ./ch-1.pl help - Usage information + +=cut + +print "ch-1.pl (Version 1.0) PWC #40 Task #1: Show multiple arrays content.\n"; + +my $cmd = shift @ARGV; # Read command or text string +if($cmd and $cmd eq "html") { + html(); exit 0; +} +elsif($cmd and $cmd eq "help") { + usage(); exit 0; +} + +# ====================== TASK 1 ============================== + +=head1 Definition Task #1: Show multiple arrays content + +You are given two or more arrays. +Write a script to display values of each list at a given index. + +For example: + + Array 1: [ I L O V E Y O U ] + Array 2: [ 2 4 0 3 2 0 1 9 ] + Array 3: [ ! ? P $ % ^ & * ] + +We expect the following output: + + I 2 ! + L 4 ? + O 0 P + V 3 $ + E 2 % + Y 0 ^ + O 1 & + U 9 * + +=cut + +# ====================== TASK 1 ============================== + +=head1 Perl5 Solution + +The three arrays are initialized with its values. With a C loop +an iteration over the indices is done. Than every array element is +printed with the index. + + #!/usr/bin/perl + + use strict; + use warnings; + + print "ch-1.pl (Version 1.0) PWC #40 Task #1: Show multiple arrays content.\n"; + + my @a1 = ('I','L','O','V','E','Y','O','U'); + my @a2 = ('2','4','0','3','2','0','1','9'); + my @a3 = ('!','?','P','$','%','^','&','*'); + + for( my $i=0; $i<=$#a1; $i++ ) { + print "$a1[$i] $a2[$i] $a3[$i]\n"; + } + +=cut + +my @a1 = ('I','L','O','V','E','Y','O','U'); +my @a2 = ('2','4','0','3','2','0','1','9'); +my @a3 = ('!','?','£','$','%','^','&','*'); + +# Pound Sign: <3> on Debian with german keyboard + +for( my $i=0; $i<=$#a1; $i++ ) { + print "$a1[$i] $a2[$i] $a3[$i]\n"; +} + +=head1 Perl6 Solution + +The Perl6 Solution is similar to Perl5. A for C is used to iterate +through the elements. But the most significant change is, that +the C keyword in Perl5 is now C in Perl6. + +What are the differences here to make it working: + +=over 3 + +=item * ch-1.p6, instead of ch-1.pl + +=item * Shebang: #!/home/chuck/rakudo/bin/perl6 instead #!/usr/bin/perl. + (I installed rakudo to user home.) + +=item * loop, instead of for keyword + +=item * blank after loop: "loop (" + +=item * @a1.end, instead of $#a1 + +=item * @a1[$i], instead of $a1[$i] for each list element + +=item * Execution time for perl6 is much longer then for perl5, see below. + +=back + +Following is the code for the Perl6 solution. + + #!/home/chuck/rakudo/bin/perl6 + + use strict; + + print "ch-1.p6 (Version 1.0) PWC #40 Task #1: Show multiple arrays content.\n"; + + my @a1 = ('I','L','O','V','E','Y','O','U'); + my @a2 = ('2','4','0','3','2','0','1','9'); + my @a3 = ('!','?','P','$','%','^','&','*'); + + loop ( my $i = 0; $i <= @a1.end; $i++ ) { + print "@a1[$i] @a2[$i] @a3[$i]\n"; + } + +=cut + +=head1 Python Solution + +Some syntactical differences in Python are, C instead of C, +curly braces, double colon after C loop, no sigil for variables, ... + +Because this is also my first Python Script, have a look on it for yourself: + + #!/usr/bin/python + + import array as arr + + print "ch-1.py (Version 1.0) PWC #40 Task #1: Show multiple arrays content." + + a1 = ['I','L','O','V','E','Y','O','U'] + a2 = ['2','4','0','3','2','0','1','9'] + a3 = ['!','?',u"\xA3",'$','%','^','&','*'] + A1 = arr.array('c', a1) + + for i in range(0,len(a1)): + print A1[i], " ", a2[i], " ", a3[i] + +=cut + + +=head1 Runtime Comparision + +Below is a comparision of the runtime. Perl5 is the fastest, Python is +close to Perl5, but Perl6 takes much longer to run this Script. + + # time ./ch-1.p6 + ch-1.p6 (Version 1.0) PWC #40 Task #1: Show multiple arrays content. + I 2 ! + L 4 ? + O 0 P + V 3 $ + E 2 % + Y 0 ^ + O 1 & + U 9 * + + real 0m0,356s + user 0m0,516s + sys 0m0,028s + + # time ./ch-1.pl + ch-1.pl (Version 1.0) PWC #40 Task #1: Show multiple arrays content. + I 2 ! + L 4 ? + O 0 P + V 3 $ + E 2 % + Y 0 ^ + O 1 & + U 9 * + + real 0m0,017s + user 0m0,012s + sys 0m0,004s + + # time ./ch-1.py + ch-1.py (Version 1.0) PWC #40 Task #1: Show multiple arrays content. + I 2 ! + L 4 ? + O 0 P + V 3 $ + E 2 % + Y 0 ^ + O 1 & + U 9 * + + real 0m0,032s + user 0m0,028s + sys 0m0,000s + +=cut + +# ================================ Usage ============================ +sub usage { + print "./ch-1.pl []\n"; + print "\n"; + print " command, html|help\n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print "\n"; + print " Examples:\n"; + print " # ./ch-1.pl\n"; + print " # perldoc ch-1.pl\n"; + print " # ./ch-1.pl help\n"; + print " # ./ch-1.pl html\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #40 Task #1, Show Multiple Array Contents\" --css \"pwc.css\" ch-1.pl > ch-1.html ]; + + # ------------- Create CSS -------------- + my $CSS =<pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-040/burkhard-nickels/perl5/ch-1.py b/challenge-040/burkhard-nickels/perl5/ch-1.py new file mode 100755 index 0000000000..fd8c1c447f --- /dev/null +++ b/challenge-040/burkhard-nickels/perl5/ch-1.py @@ -0,0 +1,14 @@ +#!/usr/bin/python + +import array as arr + +print "ch-1.py (Version 1.0) PWC #40 Task #1: Show multiple arrays content." + +a1 = ['I','L','O','V','E','Y','O','U'] +a2 = ['2','4','0','3','2','0','1','9'] +a3 = ['!','?',u"\xA3",'$','%','^','&','*'] +A1 = arr.array('c', a1) + +for i in range(0,len(a1)): + print A1[i], " ", a2[i], " ", a3[i] + diff --git a/challenge-040/burkhard-nickels/perl5/ch-2.pl b/challenge-040/burkhard-nickels/perl5/ch-2.pl new file mode 100755 index 0000000000..c06149811c --- /dev/null +++ b/challenge-040/burkhard-nickels/perl5/ch-2.pl @@ -0,0 +1,219 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Data::Dumper qw(Dumper); +$Data::Dumper::Indent=0; + +=head1 Perl Weekly Challenge #40 Task #2: Sort SubList + +The code for the sorting of a SubList is mainly the C function +applied on an array slice. I use a simple way of output with the Data::Dumper +module. + +I compare also in Task#2 three solutions, first with Perl5, second with Perl6 and third +with Python. For me it is a learning excercise in Perl6 and Python. + +So the special thing here are the 3 used script languages: + +=over 3 + +=item * Perl5 Solution + +=item * Perl6 Solution + +=item * Python Solution + +=back + +=begin html + +

Download and References

+Download File:Solution PWC #40 Task #1 ch-1.pl
+Download File:Solution PWC #40 Task #2 ch-2.pl
+Download File:Solution PWC #40 Task #1 ch-1.p6
+Download File:Solution PWC #40 Task #2 ch-2.p6
+Download File:Solution PWC #40 Task #1 ch-1.py
+Download File:Solution PWC #40 Task #2 ch-2.py
+
+ +=end html + +=head1 SYNOPSIS + + # ./ch-2.pl + ch-2.pl (Version 1.0) PWC #40 Task #2: Sort SubList. + Before:$VAR1 = [10,4,1,8,12,3]; + After: $VAR1 = [1,4,3,8,12,10]; + + ./ch-2.pl [] + # perldoc ch-2.pl - POD + # ./ch-2.pl html - HTML/CSS in ch-2.html/pwc.css + # ./ch-2.pl help - Usage information + +=cut + +print "ch-2.pl (Version 1.0) PWC #40 Task #2: Sort SubList.\n"; + +my $cmd = shift @ARGV; # Read command or text string +if($cmd and $cmd eq "html") { + html(); exit 0; +} +elsif($cmd and $cmd eq "help") { + usage(); exit 0; +} + +# ====================== TASK 2 ============================== + +=head1 Definition Task #2: Sort SubList + +You are given a list of numbers and set of indices belong to the list. +Write a script to sort the values belongs to the indices. + +For example, + + List: [ 10, 4, 1, 8, 12, 3 ] + Indices: 0,2,5 + +We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3. + +Final List would look like below: + + List: [ 1, 4, 3, 8, 12, 10 ] + +=cut + +# ====================== TASK 2 ============================== + +=head1 Perl5 Solution + +The sorting of a SubList is done on an Array Slice with the C function. +The sorted Array Slice is assigned back to the same Array Slice. + +Data::Dumper is used for the output. The C<$Data::Dumper::Indent> variable is +set to "0" that means no indentation. + + #!/usr/bin/perl + + use strict; + use warnings; + use Data::Dumper qw(Dumper); + $Data::Dumper::Indent=0; + + my @a = (10,4,1,8,12,3); # Initialize Array + my @i = (0,2,5); # Initialize Indices, not used here. + + print "Before:", Dumper(\@a), "\n"; + @a[0,2,5] = sort { $a <=> $b } ( @a[0,2,5] ); # sort on array slice + print "After: ", Dumper(\@a), "\n"; + +=cut + +my @a = (10,4,1,8,12,3); +my @i = (0,2,5); +print "Before:", Dumper(\@a), "\n"; + +@a[0,2,5] = sort { $a <=> $b } ( @a[0,2,5] ); +print "After: ", Dumper(\@a), "\n"; + + +=head1 Perl6 Solution + +What are some differences here between Perl5 and Perl6: The C methode +is called C<@d.sort()>. In Perl5 subroutines are called with an arrow. The +C<.Int> as the sorting function I still do not understand. Like I said before, +this is my first Perl6 Script. + + #!/home/chuck/rakudo/bin/perl6 + + use strict; + + print "ch-2.p6 (Version 1.0) PWC #40 Task #2: Sort SubList\n"; + + # ----------------------------------------------- + my @a = (10,4,1,8,12,3); + my @i = (0,2,5); + + print "Before:", join(" - ", @a), "\n"; + my @d = @a[0,2,5]; + @a[0,2,5] = @d.sort( { .Int } ); # sort on array slice + print "After: ", join(" - ", @a), "\n"; + + # ----------------------------------------------- + # p6doc -f Type::List.sort + +=cut + +=head1 Python Solution + +What special can we find here? The C Python Module is used +because of the C Methode. Syntactial it is different to Perl. + +But see yourself the code below: + + #!/usr/bin/python + + import array as arr + import numpy as np + + a = arr.array('i', [10,4,1,8,12,3]) + b = [10,4,1,8,12,3] + print(a) + print(b) + + print(a[slice(0,2,5)]) + + # ------------------------------------------------- + B = np.array(b) + + R = B[np.array([0,2,5])] + R.sort() + B[np.array([0,2,5])] = R + print(" Result: ", B ) + +=cut + +# ================================ Usage ============================ +sub usage { + print "./ch-2.pl []\n"; + print "\n"; + print " command, html|help\n"; + print " help, Prints out some usage information.\n"; + print " html, Writes HTML and CSS from POD.\n"; + print "\n"; + print " Examples:\n"; + print " # ./ch-2.pl\n"; + print " # perldoc ch-2.pl\n"; + print " # ./ch-2.pl help\n"; + print " # ./ch-2.pl html\n"; +} + +sub html { + # ------------- Create HTML -------------- + qx[ pod2html --header --title \"Perl Weekly Challenge #40 Task #2, Sort SubList\" --css \"pwc.css\" ch-2.pl > ch-2.html ]; + + # ------------- Create CSS -------------- + my $CSS =<pwc.css") or die "Cant open pwc.css!\n"; + print CSS $CSS; + close CSS; +} + +=head1 AUTHOR + +Chuck + +=cut + +# ############################## END ############################################# + diff --git a/challenge-040/burkhard-nickels/perl5/ch-2.py b/challenge-040/burkhard-nickels/perl5/ch-2.py new file mode 100755 index 0000000000..f5a38c425e --- /dev/null +++ b/challenge-040/burkhard-nickels/perl5/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/python + +import array as arr +import numpy as np + +a = arr.array('i', [10,4,1,8,12,3]) +b = [10,4,1,8,12,3] +print(a) +print(b) + +print(a[slice(0,2,5)]) + +# ------------------------------------------------- +B = np.array(b) + +R = B[np.array([0,2,5])] +R.sort() +B[np.array([0,2,5])] = R +print(" Result: ", R ) +print(" Result: ", B ) + +# ------------------------------------------------- +# Create a sequence of integers from 10 to 1 with a step of -2 +# a = np.arange(10, 1, -2) +# print("\n A sequential array with a negative step: \n",a) + +# Indexes are specified inside the np.array method. +# newarr = a[np.array([3, 1, 2 ])] +# print("\n Elements at these indices are:\n",newarr) + diff --git a/challenge-040/burkhard-nickels/perl6/ch-1.p6 b/challenge-040/burkhard-nickels/perl6/ch-1.p6 new file mode 100755 index 0000000000..5475aee7fe --- /dev/null +++ b/challenge-040/burkhard-nickels/perl6/ch-1.p6 @@ -0,0 +1,61 @@ +#!/home/chuck/rakudo/bin/perl6 + +use strict; + +print "ch-1.p6 (Version 1.0) PWC #40 Task #1: Show multiple arrays content.\n"; + +my @a1 = ('I','L','O','V','E','Y','O','U'); +my @a2 = ('2','4','0','3','2','0','1','9'); +my @a3 = ('!','?','£','$','%','^','&','*'); + +loop ( my $i = 0; $i <= @a1.end; $i++ ) { + print "@a1[$i] @a2[$i] @a3[$i]\n"; +} + +# What are the differences here to make it working: +# - ch-1.p6, instead of ch-1.pl +# - Shebang: #!/home/chuck/rakudo/bin/perl6 instead #!/usr/bin/perl +# I installed rakudo to user home. +# - loop, instead of for keyword +# - blank after loop: "loop (" +# - @a1.end, instead of $#a1 +# - @a1[$i], instead of $a1[$i] for each list element +# +# What else? +# - Execution time for perl6 is much longer then for perl5, see below. + +# ---------------------------------------------------------------------- +# # time ./ch-1.p6 +# ch-1.p6 (Version 1.0) PWC #40 Task #1: Show multiple arrays content. +# I 2 ! +# L 4 ? +# O 0 £ +# V 3 $ +# E 2 % +# Y 0 ^ +# O 1 & +# U 9 * +# +# real 0m0,354s +# user 0m0,484s +# sys 0m0,048s +# +# ---------------------------------------------------------------------- +# # time ./ch-1.pl +# ch-1.pl (Version 1.0) PWC #40 Task #1: Show multiple arrays content. +# I 2 ! +# L 4 ? +# O 0 £ +# V 3 $ +# E 2 % +# Y 0 ^ +# O 1 & +# U 9 * +# +# real 0m0,015s +# user 0m0,012s +# sys 0m0,000s + + + + diff --git a/challenge-040/burkhard-nickels/perl6/ch-2.p6 b/challenge-040/burkhard-nickels/perl6/ch-2.p6 new file mode 100755 index 0000000000..85b12dc6d8 --- /dev/null +++ b/challenge-040/burkhard-nickels/perl6/ch-2.p6 @@ -0,0 +1,17 @@ +#!/home/chuck/rakudo/bin/perl6 + +use strict; + +print "ch-2.p6 (Version 1.0) PWC #40 Task #2: SubList\n"; + +# ----------------------------------------------- +my @a = (10,4,1,8,12,3); +my @i = (0,2,5); + +print "Before:", join(" - ", @a), "\n"; +my @d = @a[0,2,5]; +@a[0,2,5] = @d.sort( { .Int } ); +print "After: ", join(" - ", @a), "\n"; + +# ----------------------------------------------- +# p6doc -f Type::List.sort -- cgit