To: perlweeklychallenge@yahoo.com From: Mark Senn X-Attribution: mark Reply-to: Mark Senn Subject: Challenge #1 and #2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Sun, 31 Mar 2019 13:50:23 -0400 Message-ID: <15118.1554054623@pier.ecn.purdue.edu> --=-=-= Content-Type: text/plain Perl Weekly Challenge, Attached are challenges #1 and #2. -mark --=-=-= Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename=1.p6 Content-Transfer-Encoding: quoted-printable Content-Description: 1.p6 # # Retrieved from # https://perlweeklychallenge.org/blog/a-new-week-a-new-challenge # on 2019-03-27 at 11:28 -04: # # Challenge #1 # # Write a script to replace the character "e" with "E" in the # string "Perl Weekly Challenge". Also print the number of times # the character "e" found in the string. # $_ = 'Perl Weekly Challenge'; # See # https://docs.perl6.org/syntax/tr$SOLIDUS$SOLIDUS$SOLIDUS # and # https://docs.perl6.org/type/StrDistance # for more information about "tr///" and the "StrDistance" object. my $distance = tr/e/E/; say +$distance; --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=2 Content-Description: 2 (in Perl 6) # # 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 # (condition) 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"; # } # 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"; }' --=-=-=--