I love CodeWars

I love to learn with this site – CodeWars!

I was learning today with this kata:

https://www.codewars.com/kata/automorphic-number-special-numbers-series-number-6/java

In short You have to check if square of number is automorphic or not! For example:

  • 25 squared is 625 , Ends with the same number’s digits which are 25 . – it`s automorphic.
  • 13 squared is 169 , Not ending with the same number’s digits which are 69 .- it`s not.

So my code of this kata is:


int squer = number * number;
String squerString = squer + "";
String numberString = number + "";
if (squerString.length() / numberString.length() == 2) {
    squerString = squerString.substring(numberString.length());
} else {
    squerString = squerString.substring(numberString.length() - 1);
}

if (squerString.equals(number + "")) {
    return "Automorphic";
}

return "Not!!";
}

For beginner I think it`s ok but as always I could write it better. I could write like this:

String squerString = number * number + "";
String numberString = number + "";
if (squerString.length() / numberString.length() == 2) {
    squerString = squerString.substring(numberString.length());
} else {
    squerString = squerString.substring(numberString.length() - 1);
}
return squerString.equals(number + "")?"Automorphic" : "Not!";
}

BUT 1 !

After You accomplished Your kata You can check how other devs resolved this kata. I am learning by reading and checking this awesome codes. I think this is the best part of learning with CodeWars. So know I learned that I could write kata like this (I write it myself after check others solutions):

public static String autoMorphic(int number) {

        String squerString = number * number + "";
        String numberString = number + "";
        return squerString.endsWith(numberString) ? "Automorphic" : "Not!";

    }

Three line of code!

Brilliant! Astonishing. I love to check how others writes. I think most efficient method to learn. You are writing code them check how You could write it better. I recommend CodeWars!

BUT 2 !

You can write it in one 😀

public static String autoMorphic(int number) {
      return ("" + (number * number)).endsWith("" + number) ? "Automorphic" : "Not!!";
    }

Tada! No words. This is fantastic!