Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Sandbox

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}

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Feedback wanted (4 comments)
Feedback wanted
Lundin‭ wrote almost 3 years ago

Curious to see how this turns out. Wasn't sure if I should restrict it to 15 or go with 100. The snippets would turn rather long in that case. Is this challenge sensible in a lot of different languages? The language won't need a line number counting feature, though it probably helps.

Lundin‭ wrote almost 3 years ago

I posted a solution too, just for reference. Would this challenge be interesting in other languages too?

Quintec‭ wrote almost 3 years ago

I'm not sure this would be very interesting - essentially, this is the same as the fizzbuzz challenge, but you define the function at the beginning of your code and then the added part is calling it 7 times

Razetime‭ wrote almost 3 years ago

This is a kind of interesting source-layout challenge, but i think you need more restrictions(i.e. line length must be divisible by 5 or 3) and you need to be more stringent with the restrictions(aka, line numbers are always enforced from beginning of file)