Talk:FizzBuzz: Difference between revisions

(→‎Too many versions for some languages: Expand on my reasons...)
Line 39:
:If pages get too long, make subpages, see [[Go Fish]]. But [[99 Bottles of Beer]] would need that more...
:Also, see [[:Category:Collection Members]] --[[User:Hajo|Hajo]] ([[User talk:Hajo|talk]]) 21:59, 19 November 2014 (UTC)
 
===Here are the five versions as they were:===
 
====Version 1 - regular if / else====
with linebreaks after each "FizzBuzz": <!-- http://ideone.com/UrHdvd -->
<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>
{{out}}
<pre>
# 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.
</pre>
 
====Version 2 - bash with echo====
using echo to generate the numbers: <!-- http://ideone.com/0VMIuO -->
<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====
using bash with seq to generate the input: <!-- http: -->
<lang AWK>seq 100 | awk '$0=NR%15?NR%5?NR%3?$0:"Fizz":"Buzz":"FizzBuzz"'</lang>
 
====Version 4 - no divisions====
awk, using no division & no modulo: <!-- http://ideone.com/uHmYUr -->
<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: <!-- http://ideone.com/jF9Ddd -->
<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.
 
===Critique of AWK versions===
I decided that there were possibly too many versions - 5 - for such a straight-forward task.
 
* Version 1 seemed clear in both its chosen algorithm and its execution.
* Version 2 seemed the the next clearest. Use of a bash shell and echo for some of the work is all standard AWK usage and the AWK program is well layed out.
* Version 3 is not as nice code, less maintainable, but it ''is'' in the grand tradition of AWK one liners.
* Version 4: Well using no division and modulo are just artificial restrictions that don't make the code look as good as version 1, and just makes me ask why? Those restrictions don't give a solution that emphasises some other part of AWK particularly - not enough for a mention by its author anyway.
* Version 5 reads like a me-to take on 4 with its stated use of the same extra restrictions and that really obscure <code>F="003053003503006"</code> line.
 
There is a task. THere is a language. The idea is to show off the language to its best. We do have a talk page, and people have delved into a task in more "depth" on talk pages before, but I thought that people were not thinking enough about showing AWK at its best. There was no comments on the different language features used in an example, just artificial limits applied to the task to produce different code.
 
Now some languages mention that they support different programming paradigms and so it could be useful to show, for example, both an OO and functional version of a Python program. This does ''not'' apply to versions 4 and 5
 
Sometimes a different example can use a different part of the language, such as testing for primes using a regexp rather than a non-regexp solution , or solution that allows for tail-call optimization versus an obvious algorithm that does not.That too would be highlighting a feature of the language, but even then judgement might be needed if the resultant code is too messy compared to the non tail-call optimized version/algorithm.
 
If you are adding the fourth or more variant for a language then you should think harder about your reasons for trying to include it with an eye on what extra it reveals about the language and/or its contribution to the task as a whole.
 
Now the above is just my opinion, and we ''could'' just have an edit war on version 4 and 5, but I would prefer to get your reasons for keeping them as, as you have stated, other tasks may have similar numbers of examples for similar reasons. I am trying to get a good task page with good, idiomatic code for each language. If you think my idea is duff then please explain your reasoning too (Not just Hajo, every RC user willing to express an opinion). Hopefully I can learn from how my ideas are shot down too (ouch).<br>:-)<br> --[[User:Paddy3118|Paddy3118]] ([[User talk:Paddy3118|talk]]) 23:26, 19 November 2014 (UTC)
Anonymous user