Jump to content

Fizzbuzz


spudw2k
 Share

Recommended Posts

I know fizzbuzz has been mentioned a few times in the forum; I just saw a Youtube video from Tom Scott about it. I paused the video before he discussed his solution and came up with my own first.  My first version already had some modularity in mind, but I decided to go further and make one with maximal modularity in mind--multiple words and multiple round conditions. 

To keep things simple, the only requirement is that the words and rounds have to be defined in such a way that the expected order is correct (word order and round order). It was a simple but fun exercise. 

Here is the video if anyone is interested:

Spoiler

 

And here is my solution:

Spoiler
Const Enum $UBOUND_ROWS = 1, $UBOUND_COLUMNS        ;Constants for UBOUND function parameters
Const $VERBSANDROUNDS[][] = [["Fizz", 3],["Buzz", 5],["Pop", 9, 11]]        ;Verb List - Two-Dimension Array
Const $ROUNDS = 100, $VERBWORD = 0, $VERBS= UBound($VERBSANDROUNDS,$UBOUND_ROWS) - 1, $VERBROUNDS = UBound($VERBSANDROUNDS, $UBOUND_COLUMNS) - 1        ;Other Constants

For $iRound = 1 to $ROUNDS      ;For each round
    Local $sRoundOutput = ""        ;Initialize blank string
    For $iVerb = 0 To $VERBS        ;For each Verb Word
        For $iVerbRound = 1 To $VERBROUNDS      ;For each Verb Round to Evaluate
            If Not Mod($iRound, $VERBSANDROUNDS[$iVerb][$iVerbRound]) Then $sRoundOutput &= $VERBSANDROUNDS[$iVerb][$VERBWORD]      ;If Round is Divisble by Verb Round, append Verb Word to string
        Next
    Next
    If Not $sRoundOutput Then $sRoundOutput = $iRound   ;If string is blank (no verb words) set string value to round number
    ConsoleWrite($sRoundOutput & @CRLF)     ;Output string
Next

 

 

Edited by spudw2k
made the solution code more meaningful / readable
Link to comment
Share on other sites

  • 1 year later...

Close, but notice that your logic only outputs when fizz, buzz, or fizzbuzz are present.

Also, when fizz/buzz/fizzbuzz are output, there should not be an integer displayed.  Take note of the output in the video here: 

Spoiler

 

My code is meant to be more modular/flexible so that it could accommodate different conditions, but I suppose it is a fair criticism that if the running conditions are set in stone, this doesn't add any value.  It was just a fun exercise.

 

Link to comment
Share on other sites

Quote

Close, but notice that your logic only outputs when fizz, buzz, or fizzbuzz are present.

Ohh...I know I designed it that way, just as a proof of concept for my self...Originally the first line in the loop was $String = "$i - " so that it output every number

3 minutes later i came up with this 

 

Spoiler

 

For $i = 1 to 101
    $String = ""
    If Mod($i, 3) = 0 Then $String = $String & "Fizz"
    If Mod($i, 5) = 0 Then $String = $String & "Buzz"

    If $String = "" Then $String = $i
    ConsoleWrite(@CRLF & $String)
Next

 

I was just goofin and got bored one night

Great work though

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...