FizzBuzz/AWK: Difference between revisions
(FizzBuzz / AWK) |
(Remarks) |
||
Line 1: | Line 1: | ||
{{collection|FizzBuzz}} |
{{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: linebreaks after each "FizzBuzz", |
|||
to get a more compact output. |
|||
<lang AWK># usage: awk -v n=38 -f FizzBuzz.awk |
<lang AWK># usage: awk -v n=38 -f FizzBuzz.awk |
||
# |
# |
||
Line 8: | Line 12: | ||
if(!n) n=100 |
if(!n) n=100 |
||
print "# FizzBuzz:" |
print "# FizzBuzz:" |
||
for (ii=1; ii<=n; ii++) |
for (ii=1; ii<=n; ii++) |
||
if (ii % 15 == 0) |
if (ii % 15 == 0) |
||
Line 17: | Line 22: | ||
else |
else |
||
{printf "%3d ", ii} |
{printf "%3d ", ii} |
||
print "\n# Done." |
print "\n# Done." |
||
}</lang> |
}</lang> |
||
Line 32: | Line 38: | ||
</pre> |
</pre> |
||
==Version 2 - bash with echo== |
|||
<!-- http://ideone.com/0VMIuO --> |
|||
Using echo to generate the numbers as input, |
|||
so we need no loop inside the script. |
|||
Disadvantage: this needs a shell where echo can do this. |
|||
<lang AWK>echo {1..100} | awk ' |
<lang AWK>echo {1..100} | awk ' |
||
BEGIN {RS=" "} |
BEGIN {RS=" "} |
||
Line 42: | Line 52: | ||
'</lang> |
'</lang> |
||
==Version 3 - one-liner with seq== |
|||
using bash with seq to generate the input |
As version 2, using bash with seq to generate the input. <br> |
||
Disadvantage: needs external command seq, i.e. only works on unix. |
|||
<lang AWK>seq 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'</lang> |
<lang AWK>seq 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'</lang> |
||
==Version 4 - no divisions== |
|||
<!-- http://ideone.com/uHmYUr --> |
|||
All processing is done inside awk, |
|||
using no division & no modulo, and instead some counters: |
|||
<lang AWK># usage: awk -v n=38 -f fizzbuzzNoDiv.awk |
<lang AWK># usage: awk -v n=38 -f fizzbuzzNoDiv.awk |
||
# |
# |
||
Line 57: | Line 72: | ||
c1++; c3++; c5++; cF++; x=sprintf("%3d ",c1) |
c1++; c3++; c5++; cF++; x=sprintf("%3d ",c1) |
||
if(c3>= 3) { c3=0; x="Fizz " } |
if(c3>= 3) { c3=0; x="Fizz " } |
||
if(c5>= 5) { c5=0; x="Buzz " } |
|||
if(cF>=15) { cF=0; x="FizzBuzz\n" } |
if(cF>=15) { cF=0; x="FizzBuzz\n" } |
||
printf(x) |
printf(x) |
||
Line 65: | Line 80: | ||
Same output as version 1. |
Same output as version 1. |
||
==Version 5 - no divisions, repeating pattern== |
|||
<!-- http://ideone.com/HJsrvl --> |
|||
Another solution with no division & no modulo. |
|||
This is inspired by the version "Without Modulus" of Nimrod, |
|||
using a precomputed pattern to decide what to print. |
|||
<lang AWK># usage: awk -v n=42 -f fizzbuzzRepeatPattern.awk |
<lang AWK># usage: awk -v n=42 -f fizzbuzzRepeatPattern.awk |
||
# |
# |
Revision as of 22:55, 19 November 2014
Version 1 - regular if / else
This is the "traditional" approach:
Loop, and modulo-check to see what to print.
Minor tweak: linebreaks after each "FizzBuzz",
to get a more compact output.
<lang AWK># usage: awk -v n=38 -f FizzBuzz.awk
BEGIN {
if(!n) n=100 print "# FizzBuzz:"
for (ii=1; ii<=n; ii++) if (ii % 15 == 0) {print "FizzBuzz"} else if (ii % 3 == 0) {printf "Fizz "} else if (ii % 5 == 0) {printf "Buzz "} else {printf "%3d ", ii}
print "\n# Done."
}</lang>
- Output:
# FizzBuzz: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz # Done.
Version 2 - bash with echo
Using echo to generate the numbers as input, so we need no loop inside the script.
Disadvantage: this needs a shell where echo can do this. <lang AWK>echo {1..100} | awk ' BEGIN {RS=" "} $1 % 15 == 0 {print "FizzBuzz"; next} $1 % 5 == 0 {printf "Buzz "; next} $1 % 3 == 0 {printf "Fizz "; next} {printf "%3d ",$1} '</lang>
Version 3 - one-liner with seq
As version 2, using bash with seq to generate the input.
Disadvantage: needs external command seq, i.e. only works on unix.
<lang AWK>seq 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'</lang>
Version 4 - no divisions
All processing is done inside awk, using no division & no modulo, and instead some counters: <lang AWK># usage: awk -v n=38 -f fizzbuzzNoDiv.awk
- FizzBuzz using no division & no modulo - operations:
BEGIN {
if(!n) n=100 print "# FizzBuzz:" while (c1<n) {
c1++; c3++; c5++; cF++; x=sprintf("%3d ",c1) if(c3>= 3) { c3=0; x="Fizz " }
if(c5>= 5) { c5=0; x="Buzz " }
if(cF>=15) { cF=0; x="FizzBuzz\n" } printf(x)
} print "\n# Done."
}</lang> Same output as version 1.
Version 5 - no divisions, repeating pattern
Another solution with no division & no modulo. This is inspired by the version "Without Modulus" of Nimrod, using a precomputed pattern to decide what to print.
<lang AWK># usage: awk -v n=42 -f fizzbuzzRepeatPattern.awk
func p(n,t) {
if(0+t==0) {printf("%3d ",n); return} printf fb[t]
} BEGIN {
if(!n) n=100 print "# FizzBuzz:"
F="003053003503006" split("1,2, Fizz,4, Buzz, FizzBuzz\n,", fb, ",")
while (i<n) {
i++; fmt++; p(i, substr(F,fmt,1) ) if(fmt>length(F)) { fmt=1; }
} print "\n# Done."
}</lang> Same output as version 1.