aboutsummaryrefslogtreecommitdiff
path: root/challenge-001/mark-senn/perl6/ch-2.p6
blob: 057621e9e8852d571c748a454793845d19c6e48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
# 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";
#     }
#

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";  }'

#===SORRY!=== Error while compiling /home/manwar/github/perlweeklychallenge-club/challenge-001/mark-senn/perl6/ch-2.p6
#Two terms in a row
#at /home/manwar/github/perlweeklychallenge-club/challenge-001/mark-senn/perl6/ch-2.p6:53
#------> perl6 -e⏏ 'for (1..20) {  my $e3 = $_ %% 3;  my $
#    expecting any of:
#            infix
#            infix stopper
#            postfix
#            statement end
#            statement modifier
#            statement modifier loop