diff options
| author | Burkhard Nickels <chuck@peregrina> | 2019-12-30 00:02:16 +0100 |
|---|---|---|
| committer | Burkhard Nickels <chuck@peregrina> | 2019-12-30 00:02:16 +0100 |
| commit | ea0e10fea656e93328f21ee3ae2ce6390ebf4181 (patch) | |
| tree | 1491682f7c1272d2a352028613f37c83a0d3e3d1 /challenge-040 | |
| parent | e75cf41be3dacd8a99083a382a0500b3934d18d4 (diff) | |
| download | perlweeklychallenge-club-ea0e10fea656e93328f21ee3ae2ce6390ebf4181.tar.gz perlweeklychallenge-club-ea0e10fea656e93328f21ee3ae2ce6390ebf4181.tar.bz2 perlweeklychallenge-club-ea0e10fea656e93328f21ee3ae2ce6390ebf4181.zip | |
Solution for PWC 40 from Burkhard Nickels.
Diffstat (limited to 'challenge-040')
| -rw-r--r-- | challenge-040/burkhard-nickels/README | 1 | ||||
| -rw-r--r-- | challenge-040/burkhard-nickels/blogs.txt | 1 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl5/ch-1.pl | 301 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl5/ch-1.py | 14 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl5/ch-2.pl | 219 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl5/ch-2.py | 30 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl6/ch-1.p6 | 61 | ||||
| -rwxr-xr-x | challenge-040/burkhard-nickels/perl6/ch-2.p6 | 17 |
8 files changed, 644 insertions, 0 deletions
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<for> 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 + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-1.pl" download>Solution PWC #40 Task #1 ch-1.pl</a><br> +<b>Download File:</b><a href="ch-2.pl" download>Solution PWC #40 Task #2 ch-2.pl</a><br> +<b>Download File:</b><a href="ch-1.p6" download>Solution PWC #40 Task #1 ch-1.p6</a><br> +<b>Download File:</b><a href="ch-2.p6" download>Solution PWC #40 Task #2 ch-2.p6</a><br> +<b>Download File:</b><a href="ch-1.py" download>Solution PWC #40 Task #1 ch-1.py</a><br> +<b>Download File:</b><a href="ch-2.py" download>Solution PWC #40 Task #2 ch-2.py</a><br> +<br> + +<b> Perl5 to Perl6 in a Nutshell </b><br> +<a target=_blank href="https://docs.perl6.org/language/5to6-nutshell"> +https://docs.perl6.org/language/5to6-nutshell</a><br> +<br> +<b> Raku Website </b><br> +<a target=_blank href="https://raku.org/"> +https://raku.org/</a><br> + +=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<for> 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: <AltGr><Shift><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<loop> is used to iterate +through the elements. But the most significant change is, that +the C<for> keyword in Perl5 is now C<loop> 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<import> instead of C<use>, +curly braces, double colon after C<for> 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 [<command>]\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 =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(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<sort()> 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 + +<h2> Download and References </h2> +<b>Download File:</b><a href="ch-1.pl" download>Solution PWC #40 Task #1 ch-1.pl</a><br> +<b>Download File:</b><a href="ch-2.pl" download>Solution PWC #40 Task #2 ch-2.pl</a><br> +<b>Download File:</b><a href="ch-1.p6" download>Solution PWC #40 Task #1 ch-1.p6</a><br> +<b>Download File:</b><a href="ch-2.p6" download>Solution PWC #40 Task #2 ch-2.p6</a><br> +<b>Download File:</b><a href="ch-1.py" download>Solution PWC #40 Task #1 ch-1.py</a><br> +<b>Download File:</b><a href="ch-2.py" download>Solution PWC #40 Task #2 ch-2.py</a><br> +<br> + +=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 [<command>] + # 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<sort()> 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<sort()> 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<numpy> Python Module is used +because of the C<sort()> 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 [<command>]\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 =<<CSS; +body { margin-left:auto; margin-right:auto; width:80%; } +h1 { border-bottom:4px solid red; } +h2 { border-bottom:2px solid orange; } +pre { border:2px solid grey; background-color:#eef; padding:10px; } +li { padding:5px; } +a { text-decoration:none; color:black; padding:4px; } +a:hover { background-color: brown; color:white; } +._podblock_ { width:100%; background-color:black; color:white; padding:10px; } +CSS + + open(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 |
