Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34943 > unrolled thread
| Started by | Qu0ll <qu0ll64@gmail.com> |
|---|---|
| First post | 2016-07-18 05:23 +1000 |
| Last post | 2016-07-18 05:23 +1000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.java.programmer
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: So I did "FizzBuzz", in Java! Qu0ll <qu0ll64@gmail.com> - 2016-07-18 05:23 +1000
| From | Qu0ll <qu0ll64@gmail.com> |
|---|---|
| Date | 2016-07-18 05:23 +1000 |
| Subject | Re: So I did "FizzBuzz", in Java! |
| Message-ID | <nmglv7$noc$1@gioia.aioe.org> |
On 18/07/2016 01:49, 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.
>
ZOMG.
You think *anyone* would hire you with something they couldn't even read?
What's with all the fully qualified Class names?
Why are "fizz", "buzz" etc. not static constants and in UPPER CASE?
Did you know that Java includes '}' and that it's very dangerous to not
surround any if/else clause without them?
And, hey, did you *deliberately* try to format the code in as random a
way as possible?
Oh wait, I know there have been "most obfuscated code" competitions for
years, but, sorry, there is at least one "sensible" variable name in
there (the 'i' in the "for loop").
--
And loving it,
-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@gmail.com
[Replace the "SixFour" with numbers to email me]
Back to top | Article view | comp.lang.java.programmer
csiph-web