David Jondreau Posted July 6, 2010 Posted July 6, 2010 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!
comment Posted July 6, 2010 Posted July 6, 2010 Where is the challenge? It seems like a simple task using the Mod() function.
David Jondreau Posted July 6, 2010 Author Posted July 6, 2010 This may not be a challenge for you, but it may for lots of other people. If you'd like to participate, please submit an answer.
comment Posted July 6, 2010 Posted July 6, 2010 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 ) ) ]
David Jondreau Posted July 6, 2010 Author Posted July 6, 2010 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.
comment Posted July 6, 2010 Posted July 6, 2010 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" ] :
Raybaudi Posted July 6, 2010 Posted July 6, 2010 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
David Jondreau Posted July 7, 2010 Author Posted July 7, 2010 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
comment Posted July 7, 2010 Posted July 7, 2010 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.
David Jondreau Posted July 7, 2010 Author Posted July 7, 2010 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.
TheTominator Posted July 7, 2010 Posted July 7, 2010 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]
comment Posted July 7, 2010 Posted July 7, 2010 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.
David Jondreau Posted July 7, 2010 Author Posted July 7, 2010 Cool! Not using Mod() is an interesting limiting condition.
TheTominator Posted July 7, 2010 Posted July 7, 2010 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.
Raybaudi Posted July 7, 2010 Posted July 7, 2010 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 !
comment Posted July 8, 2010 Posted July 8, 2010 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.
comment Posted July 8, 2010 Posted July 8, 2010 Your script with only one $var As a chindogu maker, you're excused. :
TheTominator Posted July 8, 2010 Posted July 8, 2010 (edited) 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 July 8, 2010 by Guest
comment Posted July 8, 2010 Posted July 8, 2010 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.
David Jondreau Posted July 8, 2010 Author Posted July 8, 2010 Give us another challenge : Here you go... http://fmforums.com/forum/showtopic.php?fid/62/tid/215580
Recommended Posts
This topic is 5250 days old. Please don't post here. Open a new topic instead.
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 accountSign in
Already have an account? Sign in here.
Sign In Now