FizzBuzz/AWK: Difference between revisions

m
Fixed syntax highlighting.
(Remarks)
m (Fixed syntax highlighting.)
 
(13 intermediate revisions by one other user not shown)
Line 1:
{{collection|FizzBuzz}}
 
==Version 1 - =regular if / else===
<!-- http://ideone.com/UrHdvd -->
This is the "traditional" approach:
Loop, and modulo-check to see what to print. <br>
Minor tweak: printf with no newline, and linebreaks only after each "FizzBuzz",
to get a more compact output.
<langsyntaxhighlight AWKlang="awk"># usage: awk -v n=38 -f FizzBuzz.awk
#
BEGIN {
Line 24:
 
print "\n# Done."
}</langsyntaxhighlight>
{{out}}
<pre>
Line 37:
# Done.
</pre>
When the output is presented like that, it is easy to see a pattern.
 
==Version 2 - =bash with echo===
<!-- http://ideone.com/0VMIuO -->
Using echo from the shell to generate the numbers as input, .
 
so we need no loop inside the script.
Advantage: we need no loop inside the script.
 
Disadvantage: this needs a shell where echo can do this.
<langsyntaxhighlight AWKlang="awk">echo {1..100} | awk '
BEGIN {RS=" "}
$1 % 15 == 0 {print "FizzBuzz"; next}
Line 50 ⟶ 52:
$1 % 3 == 0 {printf "Fizz "; next}
{printf "%3d ",$1}
'</langsyntaxhighlight>
 
==Version 3 - one=One-liner with seq===
 
AsLike version 2, using bash with seq to generate the numbers as input. <br>
Disadvantage: needs external command seq, i.e. this only works on unix.
(Also, hard to read)
 
<langsyntaxhighlight AWKlang="awk">seq 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'</langsyntaxhighlight>
 
===No divisions, using counters===
==Version 4 - no divisions==
<!-- http://ideone.com/uHmYUr -->
Division is one of the more expensive operations,
All processing is done inside awk,
so it is nice if we can avoid it.
using no division & no modulo, and instead some counters:
 
<lang AWK># usage: awk -v n=38 -f fizzbuzzNoDiv.awk
All processing is done inside awk, using no division & no modulo. <br>
Instead, a simple counter for each of the output-variants is used:
<syntaxhighlight lang="awk"># usage: awk -v n=38 -f fizzbuzzNoDiv.awk
#
# FizzBuzz using no division & no modulo - operations:
BEGIN {
if(!n) n=100
Line 77 ⟶ 83:
}
print "\n# Done."
}</langsyntaxhighlight>
Same output as version 1.
 
==Version 5 - no=No divisions, repeatingusing pattern-string===
<!-- http://ideone.com/HJsrvl -->
Another solution withthat noworks divisionwithout &division no/ modulo.
{{works with|gawk|4.1.0}} {{works with|mawk|1.3.3}}
This is inspired by the version "Without Modulus" of Nimrod,
This is inspired by the versions "Without Modulus" of Nimrod and Python, <br>
using a precomputed pattern to decide what to print.
using a precomputed (observed:) pattern to decide how to print each number. <br>
 
But here, the pattern is represented as chars in a string,
<lang AWK># usage: awk -v n=42 -f fizzbuzzRepeatPattern.awk
instead of bits in an integer.
<syntaxhighlight lang="awk"># usage: awk -v n=42 -f fizzbuzzRepeatPattern.awk
#
funcfunction pprt(nx,tv) {
if(0+tv==0) {printf("%3d ",nx); return} # print number
printf fb[tv] # else: print text
}
BEGIN {
Line 96 ⟶ 104:
print "# FizzBuzz:"
 
Fpattern="003053003503006" # 0: print number, 3: print Fizz, etc.
split("1,2, Fizz,4, Buzz, FizzBuzz\n,", fb, ",")
 
while (i<n) {
i++; fmtsel++;
pprt(i, substr(Fpattern,fmtsel,1) ); # select variant to use from the pattern
if(fmt sel>=length(Fpattern) ) { fmtsel=1; }0
}
print "\n# Done."
}</langsyntaxhighlight>
Same output as version 1.
 
===Custom FizzBuzz===
===Example program 1===
generated from [[General_FizzBuzz#AWK]],
for factors 2, 3, 5 using the words A, B, C. <br>
The list of numbers is also generated (much like in version 2 and 3),
and comes in as the file numbers.txt.
 
;Input:
<pre>31
2 A
3 B
5 C</pre>
 
<!-- http://ideone.com/yw1oEK -->
<syntaxhighlight lang="awk"># usage: awk -f fizzbuzzCustom.awk numbers.txt
#
BEGIN {print "# CustomFizzBuzz:"}
 
$1 % 2 == 0 {x = x "A"}
$1 % 3 == 0 {x = x "B"}
$1 % 5 == 0 {x = x "C"}
 
!x {print $1; next}
{print " ",x; x=""}
 
END {print "# Done."} </syntaxhighlight>
 
{{out}}
<pre># CustomFizzBuzz:
1
A
B
A
C
AB
7
A
B
AC
11
AB
13
A
BC
A
17
AB
19
AC
B
A
23
AB
C
A
B
A
29
ABC
31
# Done.</pre>
 
===Example program 2===
;Input: for the generator-program at [[General_FizzBuzz#AWK]]:
<pre>105
3 Fizz
5 Buzz
7 Baxx</pre>
 
<syntaxhighlight lang="awk">BEGIN {print "# CustomFizzBuzz:"}
 
$1 % 3 == 0 {x = x "Fizz"}
$1 % 5 == 0 {x = x "Buzz"}
$1 % 7 == 0 {x = x "Baxx"}
 
!x {print $1; next}
{print " ", x; x=""}
END {print "# Done."}</syntaxhighlight>
9,476

edits