General FizzBuzz: Difference between revisions

Content added Content deleted
(Added Elixir)
Line 47:
This is a two-step solution:
* First, we get the parameters, and
** generate a file with the list of numbers (writing direclydirectly to that file)
** generate a custom awk-program for that special case (redirecting standard-output)
* the custom program is run, and does the actual work to output the desired result
<lang bash>
awk -f fizzbuzzGenerate.awk > fizzbuzzCustom.awk
awk -f fizzbuzzCustom.awk numbers.txt
</lang>
 
<lang bash>awk -f fizzbuzzGenerate.awk input.txt > fizzbuzzCustom.awk
<!-- http://ideone.com/fACMfK -->
awk -f fizzbuzzCustom.awk numbers.txt</lang>
 
;Input:
<pre>
105
3 Fizz
5 Buzz
7 Baxx
</langpre>
 
<!-- http://ideone.com/fACMfK -->
<!-- (!!) Note: the sandbox at ideone.com does not allow output to files -->
<lang awk># usage: awk -f fizzbuzzGen.awk > fizzbuzzCustom.awk
#
Line 61 ⟶ 69:
q2 = "\""
fN = "numbers.txt"
#fP = "fizzbuzzCustom.awk"
}
 
NF==1 { print "#", $1, "Numbers:"
for( i=1; i <= $1; i++ )
print( i ) > fN # (!!)
 
print "# Custom program:"
print "BEGIN {print " q2 "# CustomFizzBuzz:" q2 "}"
next
}