Comments on FizzBuzz based on line count
Post
FizzBuzz based on line count
+1
−0
Background
The normal "FizzBuzz" task is to create an up-counting loop which prints "Fizz" in case the count is divisible by 3 or "Buzz" in case the count is divisible by 5. Consequently, "FizzBuzz" for numbers like 15 that are divisible by both.
Task
The task is to write a solution which prints "Fizz", "Buzz" or both like above, every print ending with a new line. The difference for this challenge is to do so based on the line in the source.
- To give language flexibility, function solutions are fine and recommended. In such solutions, the beginning of the function may be referred to as line 1. Solutions where the beginning of the file is referred to as line 1 are fine too.
- The function must be at least 15 source code lines long.
- On all lines in your solution where the source line number is divisible by 3, you must print "Fizz" (new line). In case it is divisible by 5, you must print "Buzz" (new line). In case of both, "FizzBuzz" (new line).
- The printing call must be located at the relevant line in the source code.
- This is Code Golf, shortest program per language that fulfils the above criteria wins.
Example
Non-golfed function solution in C:
#include <stdio.h>
/* 1 */ void func (void) {
/* 2 */
/* 3 */ puts("Fizz");
/* 4 */
/* 5 */ puts("Buzz");
/* 6 */ puts("Fizz");
/* 7 */
/* 8 */
/* 9 */ puts("Fizz");
/* 10*/ puts("Buzz");
/* 11*/
/* 12*/ puts("Fizz");
/* 13*/
/* 14*/
/* 15*/ puts("FizzBuzz");
/* 16*/ }
Expected output:
Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
(challenge ends here)
An example of what a golfed solution might look like:
C (gcc), 117 bytes
#define L (__LINE__-i+1)
#define p printf("%s%s\n",L%3?"":"Fizz",L%5?"":"Buzz");
i;f(){i=L+i-1;
p
p
p
p
p
p
p}
1 comment thread