12
\$\begingroup\$

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;
}
\$\endgroup\$
17
  • 5
    \$\begingroup\$ Does the input need to be so rigid? Or could we take in a hashtable/dictionary/array/etc. in language's native format? \$\endgroup\$ Feb 6, 2017 at 15:26
  • 2
    \$\begingroup\$ Also, welcome to PPCG! \$\endgroup\$ Feb 6, 2017 at 15:37
  • 1
    \$\begingroup\$ What if two of the numbers are the same? Will they always be unique positive integers? \$\endgroup\$ Feb 6, 2017 at 16:15
  • 1
    \$\begingroup\$ What guarantees are there about the numbers: will they all be positive, or at least non-zero? What about the strings: are they all non-empty? \$\endgroup\$ Feb 6, 2017 at 17:03
  • 1
    \$\begingroup\$ A better title might be Generalized Fizz Buzz. \$\endgroup\$
    – mbomb007
    Feb 6, 2017 at 17:49

9 Answers 9

4
\$\begingroup\$

JavaScript (ES6), 90 bytes

Generates a leading newline.

f=(a,i=a.pop())=>i?f(a,i-1)+`
`+(a.map((_,j)=>++j>a[0]|i%a[j*2-1]?'':a[j*2]).join``||i):''

Test

f=(a,i=a.pop())=>i?f(a,i-1)+`
`+(a.map((_,j)=>++j>a[0]|i%a[j*2-1]?'':a[j*2]).join``||i):''

console.log(f([3, 2, 'Fizz', 3, 'Buzz', 5, 'Crackle', 10]))
console.log(f([3, 3, 'Buzz', 2, 'Fizz', 5, 'Crackle', 10]))

\$\endgroup\$
0
3
\$\begingroup\$

05AB1E, 28 bytes

#¦¤U¨2ôøVXLvY`sysÖÏJDg1‹iy},

Try it online!

Or with a different input format:

05AB1E, 16 bytes

Lv²y³ÖÏJDg1‹iy},

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 2, 98 bytes

lambda b:[''.join(''if x%b[1+i*2]else b[2+i*2]for i in range(b[0]))or`x`for x in range(1,b[-1]+1)]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C++, 194 bytes

#include <iostream>
#define p std::cout<<
int main(int c,char**a){c=2*atoi(a[1])+2;int x,f,i,n=atoi(a[c]);for(x=1;x<=n;x++){f=0;for(i=2;i<c;i+=2)if(x%atoi(a[i])<1)f=1,p a[i+1];if(!f)p x;p'\n';}}

Ungolfed:

#include <iostream>

int main(int c, char **a) {
    c = 2 * atoi(a[1]) + 2;
    int x, f, i, n = atoi(a[c]);
    for (x = 1; x <= n; x++) {
        f = 0;
        for (i = 2; i < c; i += 2)
            if (x % atoi(a[i]) < 1) f = 1, std::cout << a[i+1];
        if (!f) std::cout << x;
        std::cout << '\n';
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ If the result of x%atoi(a[i]) can't be negative, then just check x%atoi(a[i])<1. \$\endgroup\$
    – Yytsi
    Feb 12, 2017 at 12:46
  • \$\begingroup\$ @TuukkaX Good point, thanks. \$\endgroup\$ Feb 12, 2017 at 13:05
  • 1
    \$\begingroup\$ Also, I'm quite sure you can do p'\n' :) \$\endgroup\$
    – Yytsi
    Feb 12, 2017 at 13:06
1
\$\begingroup\$

PHP, 99 bytes

Based on primo´s FizzBuzz answer: õ is chr(245), a bit inverted newline.

for(;$i++<($a=$argv)[$z=$argc-1];){for($k=$s="";$z>$k+=2;)$s.=[$a[$k+1]][$i%$a[$k]];echo$s?:$i,~õ;}

ignores the first argument; run with -nr.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 105 97 bytes

g=(m,k,i=1)=>i<-~k?([...m.keys()].filter(j=>i%j<1).map(j=>m.get(j)).join``||i)+"\n"+g(m,k,i+1):""

Takes in a map of pairs m (integer, string) and an integer k. Comes with a trailing newline.

Here's a non-recursive version (105 bytes), but doesn't yield a trailing newline.

m=>k=>[...Array(k).keys()].map(x=>[...m.keys()].filter(j=>-~x%j<1).map(j=>m.get(j)).join``||x+1).join`\n`

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java, 331 bytes

Because Java.

import java.util.*;class A{A(int c,String x){i=c;v=x;}int i;String v;void x(String[]x){ArrayList<A>l=new ArrayList();int n=0;for(;++n<x.length-1;)l.add(new A(Integer.valueOf(x[n++]),x[n]));n=Integer.valueOf(x[n]);for(int i=1;i++<n;){String o="";boolean y=1>0;for(A a:l)if(i%a.i==0){y=1<0;o+=a.v;}if(y)o+=i;System.out.println(o);}}}

This is the full class required for this. However, in order to run it, you must call the method x on an existing instance of A. For the sake of testing, I have provided a command-line runnable class below, which is partially ungolfed.

import java.util.*;
class A{
A(int c,String x){i=c;v=x;}
int i;
String v;
void x(String[]x){
ArrayList<A>l=new ArrayList();
int n=0;
for(;++n<x.length-1;)
l.add(new A(Integer.valueOf(x[n++]),x[n]));
n=Integer.valueOf(x[n]);
for(int i=1;i++<n;){
String o="";
boolean y=1>0;
for(A a:l)if(i%a.i==0){y=1<0;o+=a.v;}
if(y)o+=i;
System.out.println(o);
}
}
public static void main(String[] args) {
new A(0,"").x(args);
}
}
\$\endgroup\$
0
\$\begingroup\$

stacked, 85 bytes

args rev...2*nsgroup rev 2 chunk@s~>{!s[1#]map s[0#n|]map keep''join:[n]\¬if out}map

Try it online! Alternatively, 86 bytes:

args behead...@k sgroup 2 chunk@s k~>{!s[1#]map s[0#n|]map keep''join:[n]\¬if out}map
\$\endgroup\$
0
\$\begingroup\$

dc, 121 bytes

?dstsw?[rdlt:Y:Rlt1-dst0<q]dsqx?sb1sm[ln;Y;RnlP1+sP]ss[lmn]sF[1sn0sP[lnd;Ylmr%0=s1+dsnlw!<g]dsgxlP0=Flm1+dsm10Plb!<i]dsix

Takes input on 3 separate lines, the first line containing the integer n, the second housing the int str tuples with the strings enclosed in square brackets ([]), and the third line consists of the integer k. For example, 3 2 Fizz 3 Buzz 5 Crackle 10 could be input as:

3
3 [Buzz] 2 [Fizz] 5 [Crackle]
10

Try it online!

Or taking input in a different order:

dc, 118 bytes

?dstsw[dlt:Y:Rlt1-dst0<q]dsqxsb1sm[ln;Y;RnlP1+sP]ss[lmn]sF[1sn0sP[lnd;Ylmr%0=s1+dsnlw!<g]dsgxlP0=Flm1+dsm10Plb!<i]dsix

This takes input in a different order, but on a single line in the format

k [str1] int1 [str2] int2 (...) [strn] intn n

For example, 3 2 Fizz 3 Buzz 5 Crackle 10 would be input as:

10 [Buzz] 3 [Fizz] 2 [Crackle] 5 3

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.