4
$\begingroup$

I am trying to do fizzbuzz on a list of numbers, so if a number is divisible by 3 evenly, it gets replaced by "Fizz", if it is divisible by 5 evenly, it gets replaced by "Buzz", and if it is divisible by both, it gets replaced with "FizzBuzz".

For example, {1, 2, 3, 4, 5, 15} would turn into {1, 2, Fizz, 4, Buzz, FizzBuzz}.

However, my code isn't working as intended. The result variable in the For loop is always empty, even if the condition in the If function is true, so the same list that was passed into the function is returned. Why is this happening?

Code:

fizzbuzz[x_] := Module[{newList}, newList = x; For[i=1,i<=Length[x],i++,Module[{result},result="";result<> If[Mod[x[[i]], 3]==0, "Fizz", ""];result<>If[Mod[x[[i]], 5]==0, "Buzz", ""];If[result!="", newList[[i]]=result,]]];newList]
$\endgroup$
5
  • 1
    $\begingroup$ You're not setting result to its updated values. Replace the instances of result <> ... with result = result <> .... $\endgroup$
    – march
    Mar 9 at 0:58
  • $\begingroup$ Related, or even possible duplicates: 18761, 105608. $\endgroup$
    – march
    Mar 9 at 7:11
  • $\begingroup$ Just some comments about your code: 1) Do not use Module if you can avoid it, certainly avoid a nested Module, especially for something as simple as this. 2) Use functional and more expressive primitives such as Table, Map, etc. instead of procedural things like For loops, 3) Do not append to lists if you can avoid it because it's slow and makes copies - use Table/Map/Fold etc. or failing that, Reap and Sow, or failing that, use the mutable data structures available via CreateDataStructure $\endgroup$
    – flinty
    Mar 9 at 11:45
  • $\begingroup$ @march Thank you, that fixed my problem. I forgot that <> was a shorthand for StringJoin, so I thought it just appended the string after <> to the string before the symbol $\endgroup$
    – user97962
    Mar 9 at 22:26
  • $\begingroup$ This seems like a problem on the Wolfram High School Summer Research Program application. See math.meta.stackexchange.com/questions/16774/…. $\endgroup$
    – user97978
    Mar 10 at 3:34

3 Answers 3

6
$\begingroup$
fizzbuzz[x_] := Switch[{Mod[x, 3] == 0, Mod[x, 5] == 0},
  {True, True}, "FizzBuzz",
  {True, False}, "Fizz",
  {False, True}, "Buzz",
  _, x]
fizzbuzz /@ {1, 2, 3, 4, 5, 15}

fizzbuzz /@ {1, 2, 3, 4, 5, 15}

$\endgroup$
5
$\begingroup$
list = {1, 2, 3, 4, 5, 15};

This doesn't exactly answer your question, but a clearer and more Mathematica-like way to solve your problem would be:

ClearAll[f]

f[x_] /; Mod[x, 3] == 0 && Mod[x, 5] == 0 := "FizzBuzz"
f[x_] /; Mod[x, 3] == 0 := "Fizz"
f[x_] /; Mod[x, 5] == 0 := "Buzz"
f[x_] := x

f /@ list

{1, 2, "Fizz", 4, "Buzz", "FizzBuzz"}

The /; pattern stands for Condition

$\endgroup$
3
$\begingroup$
list = Range[50];

list /. {x_Integer /; Mod[x, 15] == 0 :> "FizzBuzz"} /. {x_Integer /; 
     Mod[x, 3] == 0 :> "Fizz"} /. {x_Integer /; Mod[x, 5] == 0 :> 
   "Buzz"}

or

list /. {x_Integer /; Divisible[x, 15] :> "FizzBuzz"} /. {x_Integer /;
      Divisible[x, 3] :> "Fizz"} /. {x_Integer /; Divisible[x, 5] :> 
   "Buzz"}

{1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz",
13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23,
"Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34,
"Buzz", "Fizz", 37, 38, "Fizz", "Buzz", 41, "Fizz", 43, 44,
"FizzBuzz", 46, 47, "Fizz", 49, "Buzz"}

$\endgroup$