Post History
Sandbox
FizzBuzz based on line count
Article
code-golf
#4: Post edited
- **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:
- ```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)], 120 bytes- <!-- language-all: lang-c -->
- <pre><code>#define L (__LINE__-i+1)
- #define p printf("%s%s\n",L%3?"":"Fizz",L%5?"":"Buzz");
i;func(){i=L+i-1;- p
- p
- p
- p
- p
- p
- p}
- </code></pre>
[Try it online!][TIO-kqawlxep]- [C (gcc)]: https://gcc.gnu.org/
[TIO-kqawlxep]: https://tio.run/##NYzLCsIwFET3@YpLSiGhRijixvgAQUEIfoEQJGn0gqbFtC5a@u2xD4RhmMMwY8TDmJigN6/GFrANtcVy@dzHxBYOfQEKmNbqcj1pLTDLOfkXFVQf9LVjNA1puHm6UOnqQOmGnrFtR1pPdGwG4pKgdI03jHe4UxmKXBJSjRps9in0cfiE9x09sG@JlkMH805CH38 "C (gcc) – Try It Online"
- **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:
- ```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
- <!-- language-all: lang-c -->
- <pre><code>#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}
- </code></pre>
- [Try it online!][TIO-kqawoyrd]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-kqawoyrd]: https://tio.run/##S9ZNT07@r5yZl5xTmpKqYFNckpKZr5dh9185JTUtMy9VwUdBIz7ex9PPNT5eN1PbUJMLJlGgUFCUmVeSpqGkWqxaHJOnpOOjamyvpGSl5JZZVQXimYJ5TqVAnqY1V6Z1moZmdaatj3amrqE1F1cBCAEJCAlm1P4HGqiQm5iZp6BRlp@ZoqlQrQDUZK1Q@x8A "C (gcc) – Try It Online"
#3: Post edited
- **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:
- ```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
````
- **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:
- ```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)], 120 bytes
- <!-- language-all: lang-c -->
- <pre><code>#define L (__LINE__-i+1)
- #define p printf("%s%s\n",L%3?"":"Fizz",L%5?"":"Buzz");
- i;func(){i=L+i-1;
- p
- p
- p
- p
- p
- p
- p}
- </code></pre>
- [Try it online!][TIO-kqawlxep]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-kqawlxep]: https://tio.run/##NYzLCsIwFET3@YpLSiGhRijixvgAQUEIfoEQJGn0gqbFtC5a@u2xD4RhmMMwY8TDmJigN6/GFrANtcVy@dzHxBYOfQEKmNbqcj1pLTDLOfkXFVQf9LVjNA1puHm6UOnqQOmGnrFtR1pPdGwG4pKgdI03jHe4UxmKXBJSjRps9in0cfiE9x09sG@JlkMH805CH38 "C (gcc) – Try It Online"
#2: Post edited
- **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:
- ```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*/ }
```
- **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:
- ```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
- ````
#1: Initial revision
FizzBuzz based on line count
**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: ```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*/ } ```