Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: So I did "FizzBuzz", in Java! Date: Sun, 17 Jul 2016 15:10:18 -0400 Organization: Aioe.org NNTP Server Lines: 52 Message-ID: References: NNTP-Posting-Host: 7SK6yTFfqwlIPNBLl3l7Qg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:34942 On 7/17/2016 11:49 AM, Stefan Ram wrote: > I don't know if you have heard of »FizzBuzz«. Otherwise > a web search might explain it to you: > > »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"«. > > Allegedly it's used to test job applicants and allegedly > even programmers with university degrees in the field fail > to be able to program it. > > I have read about it for years, but today I did write it. > > My solution tries to avoid code repetitions and to be > run-time efficient. It's not the smallest solution when > the code size is taken into account. > > public final class Main > { public static void main( final java.lang.String[] args ) > { final java.lang.String eol = java.lang.System.lineSeparator(); > final java.lang.StringBuilder builder > = new java.lang.StringBuilder( 1024 ); > final java.lang.String fizz = "Fizz" + eol; > final java.lang.String buzz = "Buzz" + eol; > final java.lang.String fizzbuzz = "FizzBuzz" + eol; > for( int i = 1; i < 101; ++i ) > { final boolean d3 = i % 3 == 0; > final boolean d5 = i % 5 == 0; > final boolean db = d3 && d5; > if( db )builder.append( fizzbuzz ); > else if( d3 )builder.append( fizz ); > else if( d5 )builder.append( buzz ); > else { builder.append( i ); builder.append( eol ); }} > java.lang.System.out.println( builder ); }} > > I did not avoid to have the string literal »"FizzBuzz"«, > even though one might have combined that from the strings > »Fizz« and »Buzz«, which are already there, but to do so > might be no real improvement. It looks like a good candidate for "most obfuscated FizzBuzz code". Who would hire anyone writing code like that???? Just code it as simple as possible. And format code properly. Arne