Path: csiph.com!xmission!news.alt.net!not-for-mail From: owl Newsgroups: comp.os.linux.advocacy,sci.physics Subject: Re: 2 lines, 2^2 states, easily modified to 3^2 states. Date: Sat, 5 Aug 2017 05:42:11 +0000 (UTC) Organization: O.W.L. Lines: 54 Message-ID: References: NNTP-Posting-Host: boom.rooftop.invalid X-Face: $BHf)vM$6p_?9L`jmOQ54JeNyL+%MZGGRfH8&grb[Y%-jU`MCPAQN=/SVDa{(/67bP(YB}rjV^h>cUh User-Agent: tin/2.2.1-20140504 ("Tober an Righ") (UNIX) (Linux/3.16.0-4-amd64 (x86_64)) Xref: csiph.com comp.os.linux.advocacy:425869 sci.physics:644478 In comp.os.linux.advocacy Jeff-Relf.Me wrote: > You ( Owl ) replied ( to me ): > > > int x = 0, F, B ; > > > while ( x++ < 100 ) printf("\n"), F = !(x%3), B = !(x%5), printf( > > > F && B ? "FizBuz" : F ? "Fiz" : B ? "Buz" : "%d", x ); > > > > Because nothing says "readable" and "maintainable" like a bunch of nested > > ternary operations.... Try it with four values, where your state count > > goes to 16. See how "readable" that is with your approach. > > How about ? : > > int x = 0 ; char *Say[4] = { "%d\n", "Buz\n", "Fiz\n", "FizBuz\n" }; > while ( x++ < 100 ) printf( Say[ 2*!(x%3) + !(x%5) ], x ); > > 2 lines, 2^2 states, easily modified to 3^2 states. Better, but you're still hard-coding values. And 2^3 will need an 8-member array and 2^4 will need 16. See my other post showing 2^4. > Full Answer: http://Jeff-Relf.Me/FizBuz.HTM > > The I/O is the limiting factor, not "x%3", so: > > int x = 0 ; char *Say[4] = { "%d\n", "Buz\n", "Fiz\n", "FizBuz\n" }; > while ( x++ < 100 ) printf( Say[ 2*!(x%3) + !(x%5) ], x ); > > 2 lines, 2^2 states, easily modified to 2^3 states: > > 4*!(x%3) + 2*!(x%5) + !(x%7) > You're still limited by just having printf() pointing into an array of strings. I use an array of function pointers so that you can do something other than printf() -- or in addition to printf() -- for different combinations. For example, here a match on all four values calls zenity to display a dialog alerting on a match: http://imgur.com/a/rHRmP /*void onetwofoureight(int x){printf("FizzBuzzGoozKaaz\n");} */ void onetwofoureight(int x) { char cmd[100]={0}; printf("FizzBuzzGoozKaaz\n"); sprintf(cmd,"zenity --info --text=\"Found a FizzBuzzGoozKaaz at %d\"",x); system(cmd); }