0
votes

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.

2
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
improving code and using less characters shouldn't be used in the same sentence ;) - Jan Groth
I would have failed you. Readability is a lot more important than than how many characters are used. - Scary Wombat
Adding to @SBI, you can remove the +"" after the i in SOP. That's really uncalled for. - RainMaker
People editing the question: don't format the code since the question is about using as few characters as possible. - JJJ

2 Answers

5
votes

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!

0
votes

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.