From mark at purdue.edu Mon Apr 1 15:45:45 2019 From: mark at purdue.edu (Mark Senn) Date: Mon, 01 Apr 2019 18:45:45 -0400 Subject: [Purdue-pm] an improved solution Message-ID: <33716.1554158745@pier.ecn.purdue.edu> (Purdue Weekly Challenge, thought you might be interested in this. I don't know if anyone else thought of this solution.) Purdue Perl Mongers, Here is an improved solution with no conditional statements or loops. The new stuff is at the end of the following. -mark # # Retrieved from # https://perlweeklychallenge.org/blog/a-new-week-a-new-challenge # on 2019-03-27 at 11:28 -04: # # Challenge #2 # # Write one-liner to solve FizzBuzz problem and print number 1-20. # However, any number divisible by 3 should be replaced by the # word fizz and any divisible by 5 by the word buzz. Numbers # divisible by both become fizz buzz. # # Since we are not playing Perl Golf I'm going to concentrate on making # the code understandable. # # The # Numbers divisible by both become fizz buzz. # condition would make this a complicated structure of if statements. It # might be easier for people to understand if a # (conndition) and print "..."; # construct was used instead. This will make it easier to add conditions # like if divisible by 3, 5, and 7 print "fizz buzz baz". "If" statements # are hard enough to read when formatted in two dimensions---they're # even worse when typeset in one dimension. (I am assuming the one-liner # must be one physical line---not one logical line.) # # We could use # perl6 -e '(1..20).map({ my $t=""; my$e3 = $_ %% 3; my$e5 = $_ %% 5;$e3 and $t~="fizz";$e3 && $e5 and$t~=" "; $e5 and$t~="buzz"; !$e3 && !$e5 and $t~=$_; say $t;})' # # Using "for" instead of "map" will be understood by people that don't # already know about "map" and the "{...}" needed inside of it. Put # two spaces around each statement in the for body to make it easier to read. # perl6 -e 'for (1..20) { my$t=""; my $e3 =$_ %% 3; my $e5 =$_ %% 5; $e3 and$t~="fizz"; $e3 &&$e5 and $t~=" ";$e5 and $t~="buzz"; !$e3 && !$e5 and$t~=$_; say$t;}' # # Instead of building the string in $t and printing it at the end of # the loop we can make this more clear by doing print statements as we go. # For uniformity use only "print" instead of a combination of "print" and # "say". Multi-line commented version: # for (1..20) # { # # Is$_ evenly divisible by 3? # my $e3 =$_ %% 3; # # Is $_ evenly divisible by 5? # my$e5 = $_ %% 5; #$e3 and print "fizz"; # $e3 &&$e5 and print " "; # $e5 and print "buzz"; # !$e3 && !$e5 and print$_; # print "\n"; # } # # Ugh, can we do better than # perl6 -e 'for (1..20) { my $e3 =$_ %% 3; my $e5 =$_ %% 5; $e3 and print "fizz";$e3 && $e5 and print " ";$e5 and print "buzz"; !$e3 && !$e5 and print $_; print "\n"; }' # ? # # Yes! # Conditional statements cause complexity. # Loops cause complexity. # Let's get rid of both. # Multi-line commented version: # # @x is (0, 1, ..., 20); # my @x = (0..20); # # Set every third element to "fizz". # @x[0,3...*] = "fizz" xx *; # # Set every fifth element to "buzz". # @x[0,5...*] = "buzz" xx *; # # Set every fifteenth element to "fizz buzz". # @x[0,15...*] = "fizz buzz" xx *; # # Print the output. # say @x[1..20].join("\n"); # perl6 -e 'my @x = (0..20); @x[0,3...*]="fizz"xx*; @x[0,5...*]="buzz"xx*; @x[0,15...*]="fizz buzz"xx*; say @x[1..20].join("\n");' From mark at purdue.edu Sun Apr 7 19:38:32 2019 From: mark at purdue.edu (Mark Senn) Date: Sun, 07 Apr 2019 22:38:32 -0400 Subject: [Purdue-pm] Perl Weekly Challenge #002 Message-ID: <25897.1554691112@pier.ecn.purdue.edu> My Perl 6 solutions to the two problems are attached. As you can see, solving the problems is trivial in Perl 6. (The problem statements are in the solutions.) -mark -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ch-1.p6 URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ch-2.p6 URL: From mark at purdue.edu Sun Apr 14 14:55:03 2019 From: mark at purdue.edu (Mark Senn) Date: Sun, 14 Apr 2019 17:55:03 -0400 Subject: [Purdue-pm] Perl Weekly Challenge - 003 Message-ID: <25437.1555278903@pier.ecn.purdue.edu> See https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/ for the Perl Weekly Challenge - 003. Each challenge contains two problems to solve using Perl 5 and/or Perl 6. A repository of solutions is available at https://github.com/manwar/perlweeklychallenge-club Some solutions rely too much on Perl idioms. Because of that I'm trying to rein in my use of Perl idioms and write more straightforward code. My Perl 6 solutions to the two problems and blog entry are attached. -mark -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ch-1.p6 URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ch-2.p6 URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: t.pdf Type: application/pdf Size: 135902 bytes Desc: t.pdf URL: From mark at purdue.edu Sun Apr 21 22:14:28 2019 From: mark at purdue.edu (Mark Senn) Date: Mon, 22 Apr 2019 01:14:28 -0400 Subject: [Purdue-pm] Perl Weekly Challenge - 004 Message-ID: <9889.1555910068@pier.ecn.purdue.edu> (See my blog entry at https://engineering.purdue.edu/~mark/pwc-004.pdf for the details of the following challenges.) Challenge #1: Write a script to output the same number of PI digits as the size of your script. Say, if your script size is 10, it should print 3.141592653. Perl 6 Solution Highlights: Find a rapidly converging series for pi and use FatRat (fat rationals) for "infinite precision integer fractions". I checked my code was working by comparing it with the first 1000 digits of pi that the Wolfram Language (Mathematica) uses. Challenge #2 You are given a file containing a list of words (case insensitive 1 word per line) and a list of letters. Print each word from the file than can be made using only letters from the list. You can use each letter only once (though there can be duplicates and you can use each of them once), you don't have to use all the letters. (Disclaimer: The challenge was proposed by Scimon Proctor). Perl 6 Solution Highlights: Use the Perl 6 bag data structure. A bag is a set of which elements occur and how many times they occur. There is a commplete set of bag operators. "$word (<=) $letters" is true if all elements, including counts, of word are in letters. Next week's challenges are at https://perlweeklychallenge.org/blog/perl-weekly-challenge-005/f I find these to be good exercises. -mark From mdw at purdue.edu Mon Apr 22 01:45:08 2019 From: mdw at purdue.edu (Ward, Mark Daniel) Date: Mon, 22 Apr 2019 08:45:08 +0000 Subject: [Purdue-pm] Perl Weekly Challenge - 004 In-Reply-To: <9889.1555910068@pier.ecn.purdue.edu> References: <9889.1555910068@pier.ecn.purdue.edu> Message-ID: Dear Mark, Many thanks for sharing!? These are interesting. If you plan to share at large with the world, I see 3 tiny corrections to what you sent. 1.? In your email, you mentioned the link: https://perlweeklychallenge.org/blog/perl-weekly-challenge-005/f but I think the "f" at the end of the URL should be removed. 2.? In this pdf file: https://engineering.purdue.edu/~mark/pwc-004.pdf you have this formula for Pi: $$\pi = 4\sum_{k >= 0} -1^{k}\frac{1}{2k+1}$$ but the$-1^{k}$looks strange.? By order of operations, we should put$-1$into parenthesis, so that it looks like this: $$\pi = 4\sum_{k >= 0} (-1)^{k}\frac{1}{2k+1}$$ 3.? In the summation of the formula above, the sum looks more natural if you write$\sum_{k=0}^{\infty}$or if you write$\sum_{k\geq 0}$.? Either way would look better than$\sum_{k >= 0}$.? Just FYI. I hope that those are helpful early-morning comments! Again, thank you very much for sharing..... just wanted you to know that we are reading what you are sending! Best wishes, Mark On 4/22/19 1:14 AM, Mark Senn wrote: > (See my blog entry at > https://engineering.purdue.edu/~mark/pwc-004.pdf > for the details of the following challenges.) > > > Challenge #1: > Write a script to output the same number of PI digits as the size of > your script. Say, if your script size is 10, it should print > 3.141592653. > > Perl 6 Solution Highlights: > Find a rapidly converging series for pi and use FatRat (fat rationals) > for "infinite precision integer fractions". I checked my code was > working by comparing it with the first 1000 digits of pi that > the Wolfram Language (Mathematica) uses. > > > Challenge #2 > You are given a file containing a list of words (case insensitive 1 word > per line) and a list of letters. Print each word from the file than can > be made using only letters from the list. You can use each letter only > once (though there can be duplicates and you can use each of them once), > you don't have to use all the letters. (Disclaimer: The challenge was > proposed by Scimon Proctor). > > Perl 6 Solution Highlights: > Use the Perl 6 bag data structure. A bag is a set of which elements > occur and how many times they occur. There is a commplete set of > bag operators. "$word (<=) \$letters" is true if all elements, > including counts, of word are in letters. > > > Next week's challenges are at > https://perlweeklychallenge.org/blog/perl-weekly-challenge-005/f > I find these to be good exercises. > > -mark > _______________________________________________ > Purdue-pm mailing list > Purdue-pm at pm.org > https://mail.pm.org/mailman/listinfo/purdue-pm From mark at purdue.edu Tue Apr 23 22:38:24 2019 From: mark at purdue.edu (Mark Senn) Date: Wed, 24 Apr 2019 01:38:24 -0400 Subject: [Purdue-pm] Purdue Weekly Challenge - 005 Message-ID: <41375.1556084304@pier.ecn.purdue.edu> The challenges were 1. Write a program which prints out all anagrams for a given word. 2. Write a program to find the sequence of characters that has the most anagrams. I used a Perl 6 hash of arrays to solve the problem. See https://engineering.purdue.edu/~mark/pwc-005.pdf for blog entry with details about some Perl 6 one-liners, the math, the code, etc. -mark From mdw at purdue.edu Wed Apr 24 05:02:59 2019 From: mdw at purdue.edu (Mark Daniel Ward) Date: Wed, 24 Apr 2019 08:02:59 -0400 Subject: [Purdue-pm] Purdue Weekly Challenge - 005 In-Reply-To: <41375.1556084304@pier.ecn.purdue.edu> References: <41375.1556084304@pier.ecn.purdue.edu> Message-ID: <8ddd72ff-1d4a-534e-7544-91be1209a1b5@purdue.edu> Dear Mark, Thank you for your link to these challenges. Do you have a link to all of them?? I looked on your page but didn't see such an archive that shows all of them: https://engineering.purdue.edu/~mark/ Just curious! Mark On 4/24/19 1:38 AM, Mark Senn wrote: > The challenges were > > 1. Write a program which prints out all anagrams for a given word. > > 2. Write a program to find the sequence of characters that has the > most anagrams. > > I used a Perl 6 hash of arrays to solve the problem. See > https://engineering.purdue.edu/~mark/pwc-005.pdf for blog > entry with details about some Perl 6 one-liners, the math, > the code, etc. > > -mark > _______________________________________________ > Purdue-pm mailing list > Purdue-pm at pm.org > https://mail.pm.org/mailman/listinfo/purdue-pm From mark at ecn.purdue.edu Wed Apr 24 18:33:26 2019 From: mark at ecn.purdue.edu (Mark Senn) Date: Wed, 24 Apr 2019 21:33:26 -0400 Subject: [Purdue-pm] Purdue Weekly Challenge - 005 In-Reply-To: <8ddd72ff-1d4a-534e-7544-91be1209a1b5@purdue.edu> References: <41375.1556084304@pier.ecn.purdue.edu> <8ddd72ff-1d4a-534e-7544-91be1209a1b5@purdue.edu> Message-ID: <38769.1556156006@pier.ecn.purdue.edu> Mark Daniel Ward wrote on 2019-04-24 at 0802: | Dear Mark, | | Thank you for your link to these challenges. | | Do you have a link to all of them? I looked on your page but didn't | see such an archive that shows all of them: | | https://engineering.purdue.edu/~mark/ | | Just curious! | | Mark The challenges are at https://perlweeklychallenge.org/blog/perl-weekly-challenge-00X for 1 <= X <= 5. I assume 6 through the end will use the same numbering scheme as they are published (currently once a week). The challenge at https://perlweeklychallenge.org/blog/perl-weekly-challenge-001 is displayed wrong---I'll report it next. My blog entries regarding them are at https://engineering.purdue.edu/~mark/pwc-00X.pdf for 3 <= X <= 5. I need to create a blog index. All the already installed answers are available at https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-00X for 1 <= X <= 5. I tried using mkdir -p /tmp/mark/tmp cd /tmp/mark/tmp wget --continue --execute robots=off --level inf --no-parent --output-file=output.log --recursive --tries=5 --wait=3 https://github.com/manwar/perlweeklychallenge-club/tree/master to download that directory structure to my local computer. I was hoping that the perl5 and perl6 directory leaves would be stored as directories with .pl or .p6 files in them instead of just files. Does anyone know how to download it, preserving the existing directory structure so one could use a, for example, cat github.com/manwar/perlweeklychallenge-club/tree/master/challenge-*/*/perl6/*.p6 command to look at all the, for example, perl6 solutions instead of having to navigate through all the web pages on the web? Thanks. -mark From mark at purdue.edu Wed Apr 24 21:16:07 2019 From: mark at purdue.edu (Mark Senn) Date: Thu, 25 Apr 2019 00:16:07 -0400 Subject: [Purdue-pm] Purdue Weekly Challenge - 005 Message-ID: <11566.1556165767@pier.ecn.purdue.edu> Mark Senn wrote on 2019-04-25 at 2133: | Does anyone know how to download it, preserving the existing | directory structure so one could use a, for example, | cat github.com/manwar/perlweeklychallenge-club/tree/master/challenge-*/*/perl6/*.p6 | command to look at all the, for example, perl6 solutions instead of | having to navigate through all the web pages on the web? Thanks. Forgot to state this in the original note: without using git. -m