Announcement

Collapse
No announcement yet.

Fizz Buzz Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fizz Buzz Problem

    Hi everyone. I was asked to solve the famous "Fizz Buzz" problem in java in a class Im taking presently. So, I decided to take a ***** at it in AutoPlay Media Studio... just for fun!
    The idea is to make it in the shortest code possible.
    Here is my best solution. Can anybody come up with a way to make it better?
    Please dont judge, im just a noob...

    Code:
    Paragraph.SetText("list", ""); a = 0; b = 0; N1 = 1;
    TopValue = Input.GetText("Input1"); N2 = String.ToNumber(TopValue);
    if(N2 == "") or (N2 == 0) then N2 = 1 end; if(N2 > 1000) then N2 = 1000 end
    repeat
       if(N1 % 3 == 0) then a = 1 end; if(N1 % 5 == 0) then b = 2 end; c = a + b; list1 = Paragraph.GetText("list");
       if(c == 0) then list2 = list1.."\n"..N1; end
       if(c == 1) then list2 = list1.."\nFizz"; end
       if(c == 2) then list2 = list1.."\nBuzz"; end
       if(c == 3) then list2 = list1.."\nFizzBuzz"; end
       Paragraph.SetText("list", list2); a = 0; b = 0; N1 = N1 + 1;
    until N1 == N2 + 1;
    Click image for larger version

Name:	fizzbuzz.png
Views:	88
Size:	48.7 KB
ID:	301603
    Attached Files

  • #2
    An interesting challenge (though some 'correctly formatted' code wouldn't have gone astray. LOL, think you might get the prize for the most 'butchered' formatting I've seen this year!

    You said the challenge was to make it in the shortest code possible, yeah? I'd be remiss not to point out that had you formatted correctly, your solution actually comes out at 39 lines of code.

    Anyway, using the modulus (%) operator seems to be your preference for this challenge. So in keeping with that spirit, we can actually do this much more efficiently. By employing the modulus operator within a for/do loop, we can get that down to just 11 (LOL, 'correctly formatted') lines of code:

    Code:
    for i = 1, 19 do
        if (i % 3) == 0 and (i % 5) == 0 then
            ListBox.AddItem("ListBox1", "FizzBuzz");
        elseif (i % 3) == 0 then
            ListBox.AddItem("ListBox1", "Fizz");
        elseif (i % 5) == 0 then
            ListBox.AddItem("ListBox1", "Buzz");
        else    
            ListBox.AddItem("ListBox1", i);
        end
    end
    Attached is a side-by-side comparison. Your code (properly formatted now, XD) versus my code.
    Hope I'm not doing your homework for you!
    Attached Files

    Comment


    • #3
      Edit,
      Or you could go even more compact by replacing Line-2:
      Code:
      if (i % 3) == 0 and (i % 5) == 0 then
      with this:
      Code:
      if (i % 15) == 0 then

      Comment


      • #4
        Or,
        You could even wrap the whole thing up as a function, like this:

        Code:
        function fizzbuzz(n)
            if (n % 15) == 0 then
                return "FizzBuzz";
            elseif (n % 3) == 0 then
                return "Fizz";
            elseif (n % 5) == 0 then
                return "Buzz";
            else
                return n;
            end
        end
        
        for i  =  1, 19 do
            ListBox.AddItem("ListBox1", fizzbuzz(i));
        end

        Comment

        Working...
        X