4

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]
  • 1
    You're not setting result to its updated values. Replace the instances of result <> ... with result = result <> .... – march Mar 09 '24 at 00:58
  • Related, or even possible duplicates: 18761, 105608. – march Mar 09 '24 at 07:11
  • 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 – flinty Mar 09 '24 at 11:45
  • @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 –  Mar 09 '24 at 22:26
  • This seems like a problem on the Wolfram High School Summer Research Program application. See https://math.meta.stackexchange.com/questions/16774/contest-problem-policy?noredirect=1&lq=1. –  Mar 10 '24 at 03:34

3 Answers3

6
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}

MelaGo
  • 8,586
  • 1
  • 11
  • 24
5
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

eldo
  • 67,911
  • 5
  • 60
  • 168
3
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"}

Syed
  • 52,495
  • 4
  • 30
  • 85