0

WARNING:I'm not asking for a better code, I'm asking for a shorter code for HackerRank just to learn what can be done to shorten it.

I'm newbie to Java and was trying out this FizzBuzz problem:

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”.

I wrote my solution as short as possible.

class Solution{
public static void main(String[]b){
for(int i=1;i<101;i++){
String a=(i%3==0)?(i%5==0)?"FizzBuzz":"Fizz":(i%5==0)?"Buzz":i+"";
System.out.println(a);}}}

and I got a 3.6 score. But obviously there's room to improve because some people wrote it with 27 characters less. How is that possible ? Any suggestions? I don't really care about the ranks, I just wanna know what I'm missing.

EDIT: So with your help, I made it like this:

class Solution{public static void main(String[]b){for(int i=1;i<101;i++){System.out.println((i%3==0)?(i%5==0)?"FizzBuzz":"Fizz":(i%5==0)?"Buzz":i);}}}

and it seems I got rid of 14 characters. God knows what the other people did to lose 13 more characters. Anyway, thanks.

dono
  • 149
  • 1
  • 4
  • 16
  • 1
    As a start, you can eliminate the variable and just print your funky string. System.out.println((i%3==0)?(i%5==0)?"FizzBuzz":"Fizz":(i%5==0)?"Buzz":i+""); – SBI Nov 05 '13 at 08:12
  • 8
    **improving code** and **using less characters** shouldn't be used in the same sentence ;) – Jan Groth Nov 05 '13 at 08:13
  • 4
    I would have failed you. Readability is a lot more important than than how many characters are used. – Scary Wombat Nov 05 '13 at 08:14
  • 2
    Adding to @SBI, you can remove the `+""` after the `i` in SOP. That's really uncalled for. – Rahul Nov 05 '13 at 08:14
  • 2
    People editing the question: **don't format the code** since the question is about using as few characters as possible. – JJJ Nov 05 '13 at 08:16
  • I think it's clear that you shouldn't to this, it's an interesting exercise none the less ;) – SBI Nov 05 '13 at 08:16
  • If this is for a HackerRank challenge, do know that the scoring is based on entries from **multiple** languages. Given that the score is determined by the total amount of characters, Java will always be disadvantaged compared to other languages such as Perl or Python – Kippie Nov 05 '13 at 08:16
  • user2310... , Forgot to mention it's from hackerrank website where shorter the code, the better score you get. I'm not interviewing for a job. I'm just trying to learn here. – dono Nov 05 '13 at 08:18
  • Kippie, I know, I filtered the scores by Java language and the top score was 6.3, which means they wrote it with 27 characters less than mine. – dono Nov 05 '13 at 08:19

4 Answers4

5

What about something like:

for(int i=0;i++<100;System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz")))

Warning: this code is just an exercise of trying to make the code shorter. It is neither good or readable as normal code should try to be!

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Yes, it is possible to make it even shorter. Proof: According to the leaderboard, the highest score for java is 7.00.

How? Spoiler: identifier(s), parentheses, line breaks, pre/post increment. The conditions may be written as i%3>0 or the opposite like i%3<1.

class S{public static void main(String[]a){for(int i=0;++i<101;)System.out.println(i%3>0?i%5>0?i:"Buzz":"Fizz"+(i%5>0?"":"Buzz"));}}

It may not be getting significantly shorter yet, most likely due to the boilerplate code for main and print method. Based on everything suggested on this QA so far, it is possible to achieve at least 6.90 in Java if not the current max which is 7.00.

For example,

class S{public static void main(String[]a){for(int i=0;++i<101;)System.out.println(i%3>0?i%5>0?i:"Buzz":i%5>0?"Fizz":"FizzBuzz");}}

If we are open to try out other languages, we may wish to try JS with caution/advisory.

Many more approaches have been discussed here and here.

Java, C, C++, C#, Python, Ruby, R, none of the submissions in these languages reached the top score yet which is 16.0. It leads us to the question, which submission led to the top score? The answer is bash scripting. Proof: leaderboard for bash

How? The hint has been kindly provided by the author of the top submission, Byron Formwalt at here.

If we are new to bash scripting, we may wish to get started with few resources mentioned here, here, and here.

Disclaimer: Even though this may be suitable for the purpose of the getting higher score in hackerrank or just for exercise, it may not be a good practice for Best_coding_practices. There are many scopes for improvement in this post. Suggestions are welcome. Acknowledgements/Thanks.

user145657
  • 31
  • 1
  • 1
  • 3
0

It just becomes an argument to migrate to Kotlin!

fun fizzBuzz(number: Int) = when {
    number.divisibleBy(3) and number.divisibleBy(5) -> "Fizz-Buzz"
    number.divisibleBy(3) -> "Fizz"
    number.divisibleBy(5) -> "Buzz"
    else -> number.toString()
}

fun Int.divisibleBy(number: Int) = this % number == 0

fun main() {
    (1..100).forEach {
        println(fizzBuzz(it))
    }
}
Vin Norman
  • 2,749
  • 1
  • 22
  • 33
-1
function fizzBuzz(n) {
  for (i = 1; i <= n; i++) {
    let result = "";
    if (i % 3 === 0) result += "Fizz";
    if (i % 5 === 0) result += "Buzz";
    if (i % 7 === 0) result += "Foo";
    console.log(result || i);
  }
}

fizzBuzz(105);

It will check every condition, if all are true then all will be added together as well as it works with the single true condition as well as two true conditions.