Page 1 of 1

The FizzBuzz test

PostPosted: Tue Mar 15, 2016 7:59 am
by dpayer
Someone here pointed me to Quora and every day I get an email with interesting questions in areas of my interest. I made programming one area and the question came up about people hiring programmers, how do you decide if they know what they are doing in the interview. It was said that many could not pass the FizzBuzz test. I didn't know what that was so I searched it out:

http://imranontech.com/2007/01/24/using ... ok-coding/

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.


They said: "Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes."

So I decided to put myself to the test. It was a good one. It took me 30 minutes, including debugging (proving I am one with modest skills :) )

I'd be curious to see what others here think of this and if they'd like to test themselves. I'd be happy to post my code after anyone responds with interest. (Neobook script of 25 lines)

Re: The FizzBuzz test

PostPosted: Tue Mar 15, 2016 10:28 am
by GKCS
5 Minutes, 11 lines of NB Script

Code: Select all
loop "1" "100" "[z]"
   math "[z]*5" "0" "[tmp]"
   setvar "[n[tmp]]" "Buzz"
   math "[z]*3" "0" "[tmp]"
   setvar "[n[tmp]]" "Fizz [n[tmp]]"
   if "[n[z]]" "=" ""
          ListBoxAddItem "ListBox1" "0" "[z]"
   else
          ListBoxAddItem "ListBox1" "0" "[n[z]]"
   endif
endloop


G.

Re: The FizzBuzz test

PostPosted: Tue Mar 15, 2016 12:29 pm
by dpayer
Good one!

Honestly, I haven't been coding too much recently and it took me a good 10 minutes just to frame the argument in my mind.

I'd like to see others examples as well. Gaev sent me one as well. I may post them all in one app/pub when done. There are some different approaches by each.

UPDATE: I've put all the examples (still working on the TSQL one for inclusion) into a pub. To see them, create a new pub 600x350 and paste the following code into it:

Code: Select all
{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=3
Name=PushButton1
X=320
Y=24
W=120
H=48
Anchor=0
Text=David P
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=SetVar "[Result]" ""¶Loop "1" "100" "[CTR]"¶SetVar "[Result1]" ""¶SetVar "[Result2]" ""¶Math "[CTR]/3" "2" "[RSLT3_2]"¶Math "[CTR]/3" "0" "[RSLT3_0]"¶Math "[CTR]/5" "2" "[RSLT5_2]"¶Math "[CTR]/5" "0" "[RSLT5_0]"¶If "[RSLT3_2]" "=" "[RSLT3_0]"¶SetVar "[result1]" "Fizz"¶Endif¶If "[RSLT5_2]" "=" "[RSLT5_0]"¶SetVar "[result2]" "Buzz"¶Endif¶StrLen "[result1]" "[result1len]"¶StrLen "[result2]" "[result2len]"¶¶¶IfEx "([result1len]=0)AND([result2len]=0)"¶SetVar "[result]" "[result][ctr][#13][#10]"¶Else¶SetVar "[result]" "[result][result1][result2][#13][#10]"¶Endif¶Endloop¶RefreshObject "TextEntry1"
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=1
ObjectType=9
Name=TextEntry1
X=16
Y=24
W=136
H=304
Anchor=0
VarName=[Result]
Align=1
EditMultiLine=Yes
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=3
Name=PushButton2
X=320
Y=88
W=120
H=48
Anchor=0
Text=Gaev
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=.my code to clear text object - David¶SetVar "[Result]" ""¶.end of my code¶¶Loop "1" "100" "[thisNumber]"¶  SetVar "[thisLine]" "[thisNumber]"¶  Math "[thisNumber]/3" "0" "[thisInteger]"¶  Math "[thisNumber] - ([thisInteger]*3)" "2" "[thisResult]"¶  If "[thisResult]" "=" "0"¶     Setvar "[thisLine]" "Fizz"¶     Math "[thisNumber]/5" "0" "[thisInteger]"¶     Math "[thisNumber] - ([thisInteger]*5)" "2" "[thisResult]"¶     If "[thisResult]" "=" "0"¶        Setvar "[thisLine]" "FizzBuzz"¶     Endif¶  EndIf¶  SetVar "[printList]" "[printList][thisLine][#13][#10]"¶EndLoop¶¶.David adds this to fit example app¶SetVar "[Result]" "[printlist]"
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=3
ObjectType=3
Name=PushButton3
X=320
Y=160
W=120
H=48
Anchor=0
Text=GKCS
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=.my addition to clear it for next try¶SetVar "[listboxstart]" ""¶RefreshObject "ListBox1"¶. end of my code - David¶¶loop "1" "100" "[z]"¶   math "[z]*5" "0" "[tmp]"¶   setvar "[n[tmp]]" "Buzz"¶   math "[z]*3" "0" "[tmp]"¶   setvar "[n[tmp]]" "Fizz [n[tmp]]"¶   if "[n[z]]" "=" ""¶          ListBoxAddItem "ListBox1" "0" "[z]"¶   else¶          ListBoxAddItem "ListBox1" "0" "[n[z]]"¶   endif¶endloop
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=5
ObjectType=10
Name=ListBox1
X=184
Y=24
W=120
H=304
Anchor=0
Text=[listboxstart]
VarName=[ListBox1]
VarType=0
XPTheme=Yes
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=4
ObjectType=3
Name=PushButton4
X=320
Y=224
W=120
H=48
Anchor=0
Text=mishem
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=.my addition to clear it for next try¶SetVar "[listboxstart]" ""¶RefreshObject "ListBox1"¶. end of my code - David¶¶Loop "1" "100" "[i]"¶    Math "Frac([i]/5)" "-1" "[Result]"¶    If "[Result]" "=" "0"¶        SetVar "[Temp]" "Buzz"¶    EndIf¶    Math "Frac([i]/3)" "-1" "[Result]"¶    If "[Result]" "=" "0"¶        SetVar "[Temp]" "[Temp]Fizz"¶    EndIf¶    If "[Temp]" "=" ""¶        ListBoxAddItem "ListBox1" "0" "[i]"¶    Else¶        ListBoxAddItem "ListBox1" "0" "[Temp]"¶    EndIf¶    SetVar "[Temp]" ""¶EndLoop
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12632256
FillPattern=0
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=7

Re: The FizzBuzz test

PostPosted: Tue Mar 15, 2016 5:05 pm
by stu
17 Minutes pure TSQL:

Code: Select all
DECLARE @s INT=1
DECLARE @e INT=100
;
WITH g AS (
    SELECT @s AS n
    UNION ALL
    SELECT n+1 FROM g WHERE n+1<=@e
)

SELECT
   CASE
   WHEN n IN (SELECT n FROM g WHERE (n % 3) = 0 INTERSECT SELECT n FROM g WHERE (n % 5) = 0) THEN 'FizzBuzz'
   WHEN n IN (SELECT n FROM g WHERE (n % 3) = 0) THEN 'Fizz'
   WHEN n IN (SELECT n FROM g WHERE (n % 5) = 0) THEN 'Buzz'
   ELSE CAST(n AS VARCHAR)
   END AS num
FROM g


I confess I instantly knew this was the job for a CTE but I never produce one to generate a range of numbers so I used a #TempTable with inserted values but I knew the 'best approach' was to use the CTE so I had to research on it but the rest is all mine. =)

Re: The FizzBuzz test

PostPosted: Tue Mar 15, 2016 6:29 pm
by mishem
Code: Select all
Loop "1" "100" "[i]"
    Math "Frac([i]/5)" "-1" "[Result]"
    If "[Result]" "=" "0"
        SetVar "[Temp]" "Buzz"
    EndIf
    Math "Frac([i]/3)" "-1" "[Result]"
    If "[Result]" "=" "0"
        SetVar "[Temp]" "[Temp]Fizz"
    EndIf
    If "[Temp]" "=" ""
        ListBoxAddItem "ListBox1" "0" "[i]"
    Else
        ListBoxAddItem "ListBox1" "0" "[Temp]"
    EndIf
    SetVar "[Temp]" ""
EndLoop

Re: The FizzBuzz test

PostPosted: Wed Mar 16, 2016 4:59 pm
by Neosoft Support
I thought 'FizzBuzz' was when a bee lands in your soda.

Re: The FizzBuzz test

PostPosted: Wed Mar 16, 2016 6:39 pm
by Gaev
I thought 'FizzBuzz' was when a bee lands in your soda.

Code: Select all
While "[BeeInSodaCup]" "<>" "True"
   Call "GetBeeGPS" "[BeeGPS]"
   Call "GetSodaCupGPS" "[GPSSodaCup]"
   Call "CompareBeeAndSodaCupGPS" "[BeeInSodaCup]"
EndWhile
AlertBox "Mission" "Accomplished"