We've all heard of the ol' Fizz Buzz problem, but what happens when you try to apply it with more factors? Fizz Buzz Crackle Pop!
The Challenge
Write a full program that takes an integer input n, then n tuples containing an integer and a string, then another integer (>1) k, as such:
n int1 str1 int2 str2 (...) intn strn k
You can take this line from either command line or STDIN.
Then, for all integers 1 to k, if it is divisible by any of int1, int2...intn, output all corresponding strs in the input order, followed by a newline. If it isn't, then just output the integer, followed by a newline.
For example, with input
3 2 Fizz 3 Buzz 5 Crackle 10
we get
1
Fizz
Buzz
Fizz
Crackle
FizzBuzz
7
Fizz
Buzz
FizzCrackle
But with input (notice the order change)
3 3 Buzz 2 Fizz 5 Crackle 10
we get
1
Fizz
Buzz
Fizz
Crackle
BuzzFizz
7
Fizz
Buzz
FizzCrackle
Optional trailing newline is acceptable.
Shortest code in bytes wins.
Edits:
Obviously I've missed out a lot, sorry.
- Inputs from
console and STDIN, anything else gets +5 bytes (:c)anywhere - Full programs please.
- Assume non-empty strings for strs
- No guarantees for uniqueness of ints
Sample C++ program (limited at 20 because I'm lazy):
#include <iostream>
#include <string>
using namespace std;
int main() {
string names[20];
int mods[20], n, max;
cin >> max >> n;
for (int i=0; i<n; i++) {
cin >> mods[i] >> names[i];
}
for (int i=1; i<=max; i++) {
bool found = false;
for (int j=0; j<n; j++) {
if (i % mods[j] == 0) {
found = true;
cout << names[j];
}
}
if (!found)
cout << i;
cout << endl;
}
return 0;
}
Generalized Fizz Buzz
. \$\endgroup\$