General FizzBuzz: Difference between revisions

Content added Content deleted
(Added Elixir)
Line 47: Line 47:
This is a two-step solution:
This is a two-step solution:
* First, we get the parameters, and
* First, we get the parameters, and
** generate a file with the list of numbers (writing direcly to that file)
** generate a file with the list of numbers (writing directly to that file)
** generate a custom awk-program for that special case (redirecting standard-output)
** 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
* 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
</pre>

<!-- 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
<lang awk># usage: awk -f fizzbuzzGen.awk > fizzbuzzCustom.awk
#
#
Line 61: Line 69:
q2 = "\""
q2 = "\""
fN = "numbers.txt"
fN = "numbers.txt"
#fP = "fizzbuzzCustom.awk"
#fP = "fizzbuzzCustom.awk"
}
}


NF==1 { print "#", $1, "Numbers:"
NF==1 { print "#", $1, "Numbers:"
for( i=1; i <= $1; i++ )
for( i=1; i <= $1; i++ )
print( i ) > fN
print( i ) > fN # (!!)


print "# Custom program:"
print "# Custom program:"
print "BEGIN {print "q2"# CustomFizzBuzz:"q2"}"
print "BEGIN {print " q2 "# CustomFizzBuzz:" q2 "}"
next
next
}
}