Jump to content


Photo

fizzbuzz solution


  • Please log in to reply
3 replies to this topic

#1 DZHEZUS

DZHEZUS
  • Members
  • 10 posts

Posted 14 August 2012 - 10:19 PM

"FizzBuzz" is a common interview question to test basic skills.

Print numbers from 1 to 100, but:
if the number is even, print "Fizz" instead of the number
if the number is divisible by three, print "Buzz" instead of the number
if the number is even AND divisible by three, print "FizzBuzz" instead of the number

here's my solution:

SetWorkingDir %A_ScriptDir% 
filedelete, testdoc.txt

var = 1

while (var < 101)
{
	if (mod( var, 2) = 0) && ( mod( var, 3) = 0)
		fileappend, FizzBuzz`n, testdoc.txt
	else if ( mod( var, 2) = 0)
		fileappend, Fizz`n, testdoc.txt
	else if ( mod( var, 3) = 0)
		fileappend, Buzz`n, testdoc.txt
	else
		fileappend, %var%`n, testdoc.txt
	
	var += 1

}

variation from <!-- m -->http://www.codinghor... ... ogram.html<!-- m -->

#2 dylan904

dylan904
  • Members
  • 706 posts

Posted 15 August 2012 - 06:50 PM

Never heard of FizzBuzz, But this will also serve the same purpose...
SetWorkingDir %A_ScriptDir% 

filedelete, testdoc.txt



Loop, 100

   fileappend, % (!mod(A_Index, 2)) && (!mod(A_Index, 3)) ? "FizzBuzz`n" : (!mod(A_Index, 2)) ? "Fizz`n" : (!mod(A_Index, 3)) ? "Buzz`n" : A_Index . "`n", testdoc.txt

return


#3 MasterFocus

MasterFocus
  • Moderators
  • 4131 posts

Posted 15 August 2012 - 07:27 PM

<!-- m -->http://rosettacode.o...Buzz#AutoHotkey<!-- m -->

@dylan904:
Loop, 100 {
    [color=#BF8000]ToolTip[/color], % (!Mod(A_Index,1+([color=#FF0000]M[/color]:=1))? "Fizz" ([color=#FF0000]M[/color]:=""): "") (!Mod(A_Index,3)? "Buzz" ([color=#FF0000]M[/color]:=""): "") [color=#FF0000]M[/color]*A_Index [color=#008000]; . "`n"[/color]
    Sleep, 150
}


#4 VxE

VxE
  • Fellows
  • 3508 posts

Posted 16 August 2012 - 08:04 AM

Loop % Ceil( 100 / 6 )

	str .= ( A_Index * 6 - 5 ) "`nFizz`nBuzz`nFizz`n" ( A_Index * 6 - 1 ) "`nFizzBuzz`n"

Gui, Add, Edit, w80 r37

Gui, Show

GuiControl,, Edit1, % SubStr( str, 1, InStr( str, "`n", 0, 1, 100 ) - 1 )

Return

GuiClose:

Exitapp
*Requires AHK-L