# Puzzler: Fizzbuzz

This topic is 3616 days old. Please don't post here. Open a new topic instead.

## Recommended Posts

Here's the challenge...

Write a script that populate a field (TABLE::Result) with the numbers from 1 to 100 with these conditions:

-for multiples of 3 print “Fizz” instead of the number

-for multiples of 5 print “Buzz” instead of the number

-for multiples of 3 and 5 print “FizzBuzz” instead of the number

Go!

##### Share on other sites

Where is the challenge? It seems like a simple task using the Mod() function.

##### Share on other sites

This may not be a challenge for you, but it may for lots of other people.

##### Share on other sites

```
Show All Records

Replace Field Contents [ TABLE::Result ; Replace with calculation:

Let (

n = Get ( RecordNumber )

;

Case (

not Mod ( n ; 15 ) ; "FizzBuzz" ;

not Mod ( n ; 5 ) ; "Buzz" ;

not Mod ( n ; 3 ) ; "Fizz" ;

n )

) ]```

Hi DJ

why so rude ?

##### Share on other sites

I'm not sure how I was being rude... :

I'm not looking for an answer to a problem I'm having...I'm presenting a Puzzler.

I was inspired by this site:

http:www.itasoftware.com/careers/hiringpuzzles.html

Something for people to work on. It may not be a challenge for some, but it's a way for people to exercise their Filemaker brain.

FizzBuzz is a problem that, apocryphally, is used to test potential rogramming hires.

I can think of a couple ways to solve this problem. I'm interested in how others would do it.

comment's is pretty elegant, though I was thinking of having the list output to a single field, rather than a found set.

##### Share on other sites

I was thinking of having the list output to a single field, rather than a found set.

```
Set Field [ Table::Result ; "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¶Fizz¶52¶53¶Fizz¶Buzz¶56¶Fizz¶58¶59¶FizzBuzz¶61¶62¶Fizz¶64¶Buzz¶Fizz¶67¶68¶Fizz¶Buzz¶71¶Fizz¶73¶74¶FizzBuzz¶76¶77¶Fizz¶79¶Buzz¶Fizz¶82¶83¶Fizz¶Buzz¶86¶Fizz¶88¶89¶FizzBuzz¶91¶92¶Fizz¶94¶Buzz¶Fizz¶97¶98¶Fizz¶Buzz" ]```

:

##### Share on other sites

I'm not sure how I was being rude...

Sorry, so seemed to me.

This is an example with a repeating field.

( same calc of Michael )

FizzBuzz.zip

##### Share on other sites

OK, here's mine (which I wrote before posting this)...

#START SCRIPT

#

Set Variable [ \$i; Value:1 ]

Set Variable [ \$list; Value:1 ]

#

Loop

Set Variable [ \$i; Value:\$i + 1 ]

Set Variable [ \$result; Value:Case(Mod(\$i;15)= 0; "FizzBuzz"; Mod(\$i;5) = 0; "Buzz"; Mod(\$i; 3) = 0; "Fizz"; \$i) ]

Set Variable [ \$list; Value:\$list & ¶ & \$result ]

Exit Loop If [ \$i = 100 ]

End Loop

#

Set Field [ table::Field; \$list ]

#

#END SCRIPT

##### Share on other sites

I think your script can be shortened to:

Loop

Exit Loop If [ \$i > 100 ]

Set Variable [ \$i; Value:\$i + 1 ]

Set Variable [ \$result; Value: List ( \$result ;

Case (

not Mod ( \$i ; 15 ) ; "FizzBuzz" ;

not Mod ( \$i ; 5 ) ; "Buzz" ;

not Mod ( \$i ; 3 ) ; "Fizz" ;

\$i )

) ]

End Loop

#

Set Field [ table::Result; \$result ]

However, the problem with this puzzle that it is not defined well enough. Suppose I wanted to do this with a custom function: what would be the function's parameters? Is the range 1..100 a variable? Are the numbers 3, 5 and 15? If everything is given (i.e. can be hard-coded), then the solution I posted above in jest IS the best solution.

##### Share on other sites

I'm not sure there is a "best" solution.

For me, the point isn't to find "the" optimal solution, but to have a diversion and see different attempts at solving this and there was! Repeating fields, Loop, Get(FoundCount), hard-coded.

I never thought of using List() to manipulate the leading/trailing "pilcrow issue" in a loop (or a CF).

Perhaps an addition of "include your time to solve" would help in measuring a poster's success?

I appreciate the feedback.

##### Share on other sites

Give us another challenge :

##### Share on other sites

Here's one that doesn't use Mod() nor Int() and can be given an arbitrary list of numbers (0< n < 100 but could be modified trivially to handle higher).

Set Variable [ \$i; Value:1 ]

Set Variable [ \$list; Value:"" ]

Loop

#

Set Variable [ \$isMultipleOf5; Value:

Case(

Right(\$i;1) = "0"; 1;

Right(\$i;1) = "5"; 1;

0

) ]

#

Set Variable [ \$isMultipleOf3; Value:

Let(

[sumDigits1 =

Case(

\$i<10; \$i;

Left(\$i; 1) + Right(\$i; 1)

);

sumDigits2 =

Case(

sumDigits1<10; sumDigits1;

Left(sumDigits1; 1) + Right(sumDigits1; 1)

)];

Case(

sumDigits2 = 3; 1;

sumDigits2 = 6; 1;

sumDigits2 = 9; 1;

0

))

]

#

Set Variable [ \$result; Value:

Case(

\$isMultipleOf5 and \$isMultipleOf3; "FizzBuzz";

\$isMultipleOf3; "Buzz";

\$isMultipleOf5; "Fizz";

\$i

)]

#

Set Variable [ \$list; Value:List(\$list; \$result) ]

#

Set Variable [ \$i; Value:\$i + 1 ]

Exit Loop If [\$i > 100]

End Loop

Set Field [TABLE::Result; \$list]

##### Share on other sites

Here's one that doesn't use Mod()

You're saying it like it's a good thing.

---

BTW,

Set Variable [ \$list; Value:"" ]

at the beginning of a script does nothing.

##### Share on other sites

Cool!

Not using Mod() is an interesting limiting condition.

##### Share on other sites

You're saying it like it's a good thing.

Not actually. I'm describing why this solution is different from the others in its approach. The spirit I am embracing is to explore different ways of solving the same problem.

Set Variable [ \$list; Value:"" ]

at the beginning of a script does nothing.

You say that like it's a bad thing. :-)

It does nothing in this script for FileMaker. It does follow a convention of initializing a variable before its first use so that the code is more portable and reusable.

##### Share on other sites

Your script with only one \$var

Loop

Exit Loop If [ ValueCount ( \$i ) = 100 ]

Set Variable [ \$i ; Value:

Let(

v = ValueCount ( \$i ) + 1 ;

List ( \$i ;

Case (

not Mod ( v ; 15 ) ; "FizzBuzz" ;

not Mod ( v ; 5 ) ; "Buzz" ;

not Mod ( v ; 3 ) ; "Fizz" ;

v

) ) ) ]

End Loop

#

Set Field [ table::Result; \$i ]

P.S.: The List ( ) idea is now a stolen idea !

##### Share on other sites

You say that like it's a bad thing. :-)

Yes, I believe a totally redundant line of code IS a bad thing. The line does not "initialize a variable" - on the contrary: if there were a variable \$list at this point, this line of code would take it off the board.

The spirit I am embracing is to explore different ways of solving the same problem.

In the same spirit, you can do 4 + 4 + 4 instead of 3 * 4. Or drive from New York to Washington, DC through Los Angeles. I fail to see what purpose it serves.

##### Share on other sites

Your script with only one \$var

As a chindogu maker, you're excused. :

##### Share on other sites

Yes, I believe a totally redundant line of code IS a bad thing. The line does not "initialize a variable" - on the contrary: if there were a variable \$list at this point, this line of code would take it off the board.

It sounds like you don't like my use of the word "initialize". Feel free to substitute your own word.

The usage of \$list inside the loop requires that the \$list variable be empty before the first go around in the loop. If it had an existing value, the code wouldn't work as expected.

Why might it have a preexisting value? It might if the code were reused. This might occur if the code was wrapped in another loop to process 20 such lists of numbers (e.g. one per record in a dataset).

Some other languages require a value to be initialized before their first usage. Having the assignment in there would be useful if the code was treated as pseudocode for another language and ported line by line.

There was a rationale behind my writing that it makes sense for code reuse or portability.

I think initializing variables it is a good practice to follow since, in my opinion, it leads to less coding errors on the whole. I think of it as "defensive programming."

The spirit I am embracing is to explore different ways of solving the same problem.

In the same spirit, you can do 4 + 4 + 4 instead of 3 * 4. Or drive from New York to Washington, DC through Los Angeles. I fail to see what purpose it serves.

Edited by Guest

##### Share on other sites

This might occur if the code was wrapped in another loop.

That's why I said "at the beginning of a script".

As for code reuse or portability, I really don't want to get into that. Suffice it to say that I wouldn't say anything if your "initializing" line were commented out.

##### Share on other sites

Give us another challenge :

Here you go...

http://fmforums.com/forum/showtopic.php?fid/62/tid/215580

##### Share on other sites

This topic is 3616 days old. Please don't post here. Open a new topic instead.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

• ### Who Viewed the Topic

×
×
• Create New...