Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34952

Re: So I did "FizzBuzz", in Java!

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Daniel Pitts <usenet@coloraura.com>
Newsgroups comp.lang.java.programmer
Subject Re: So I did "FizzBuzz", in Java!
Date Mon, 18 Jul 2016 09:26:56 -0700
Organization A noiseless patient Spider
Lines 65
Message-ID <nmj00i$jae$1@dont-email.me> (permalink)
References <FizzBuzz-20160717163715@ram.dialup.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Mon, 18 Jul 2016 16:26:58 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="eb3485432b4e1ba723a38b5652b94a6b"; logging-data="19790"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WVZNx0VhHH5RMBOkmES5EoPWNYnqN+tA="
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Thunderbird/45.2.0
In-Reply-To <FizzBuzz-20160717163715@ram.dialup.fu-berlin.de>
Cancel-Lock sha1:BNUKmjFnZegAI2kcrTezonLj+2A=
Xref csiph.com comp.lang.java.programmer:34952

Show key headers only | View raw


On 7/17/16 8: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.
>
Here's one of my solutions:

public final class FizzBuzz {
     public static void main(final java.lang.String[] args) {
         java.util.stream.IntStream
             .rangeClosed(1, 100)
             .mapToObj(i -> {
                 final StringBuilder result = new StringBuilder();
                 if (0 == i % 3) {
                     result.append("Fizz");
                 }
                 if (0 == i % 5) {
                     result.append("Buzz");
                 }
                 if (result.length() == 0) {
                     result.append(i);
                 }

                 return result;
             }).forEachOrdered(System.out::println);
     }
}

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: So I did "FizzBuzz", in Java! Daniel Pitts <usenet@coloraura.com> - 2016-07-18 09:26 -0700
  Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-18 10:10 -0700
    Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-18 14:09 -0400
      Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-18 14:00 -0700
        Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-18 17:30 -0400
          Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-19 05:06 -0700
            Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-19 08:13 -0400
              Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-19 05:18 -0700
        Re: So I did "FizzBuzz", in Java! Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-18 21:59 +0000
        Re: So I did "FizzBuzz", in Java! Arne Vajhøj <arne@vajhoej.dk> - 2016-07-18 19:51 -0400
  Re: So I did "FizzBuzz", in Java! Arne Vajhøj <arne@vajhoej.dk> - 2016-07-18 19:46 -0400

csiph-web