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!

How I learn?

I was searching what experienced devs are saying how to learn programing? Most of them I think 90% are convinced that best practice is to write code. Every time they says like mantra. Write code write code write code. So I am doing it. I am writing code.

In my last post I write about adventcode. I didn`t  end it. It was to difficult to me. So I started exercises some easier thing.

First I write code in: https://codingbat.com/java There are many basic exercises so-called: warmups. Some of them are tricky some of them very easy. I bored very fast.

So I`v tried this: https://www.codewars.com This was accurate shot! Exercises are called Katas – from karate. You are starting with 8 KYU – the smallest degree in karate. Exercises difficulty level are devided by KYUs.  You can choose what Kata you want to resolve. Even You can start 3KYU kata if You feel you can resolve it.  I prefer this page to practice. After complete Kata there are solutions another Users. This is marvelous solution to learn and get know how another devs are writing code. Very useful I love that! I can learn much more. I recommend this site.

You can check my profile here: https://www.codewars.com/users/10bashi

Third site which I use is: http://stackoverflow.com 🙂 Yes google for programmers. Here there is everything You have to know about programming. Many solutions, examples, comments, and much more code! Very useful site.

Last but not least: https://google.com 🙂 I think this site do have to be comment.

I really really  recommend Codewars and Stackoverflow. 

Now I`m going back to write code and learn. Thanks for steping by!

After 3 Days of coding

My brain is boiling.

Yes. Thats true. This is possible. When You starting such difficult thing You have to prepare for very intensive thinking. All the time.  When I gets up I am thinking about my homework, problem, solving this problems, thinking about every aspect of things I had learned.

Some times I have doubts and sometimes I am frustrated. But as Louis Zamperini said: Never give up, no matter what!  And I am holding this words very hard. I will not stop to learn.

Because there is something what I like in coding.

Do You know what is it?

Satisfaction! That feeling when You compile code and code is working with out mistakes and exceptions.

I have to work on my code. Sometimes when I code I want to open opened door. And my code is not clean as want to be.

My biggest problem is that I know what I have to do, how algorithm or program have to work but when I sit down and want to code it I have wall a front of me. I think every beginning coder have same problems. My coach says that solution will come with time. I hope so, I believe it will come!

And what is most important! I want to do this I like it and I can’t wait for weekend for new lessons!

Scope of knowledge after 3 days is:

  • types
  • methods
  • conditions
  • loops
  • tables
  • basics object programming //only theory
  • setters and getters

My homework:

  1. Draw 6 numbers from the table with range 1-49 //maybe some day I win some $$ 😉
  2. You have table with number and You ask user to choose table index and number You want to add to table

As always You can use those code if You want.

Link to Github: github.com/abramovvicz

So first exercise is:

package domowe;

import java.util.Random;

public class Lotto {
    int table[] = new int[6];
    int table2[] = new int[6];

    public static void main(String[] args) {
        Lotto lotto = new Lotto();
        lotto.chooseNumbers();
        lotto.checkNumbers();
    }

    private void chooseNumbers() {
        Random random = new Random();

        for (int i = 0; i < table.length; i++) {
            table[i] = random.nextInt(49) + 1;
            table2[i] = table[i];
        }
    }
    
    private void checkNumbers() {
        System.out.println("    ");
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table2.length; j++) {
                if (i != j) {
                    if (table[i] == table2[j]) {
                        chooseNumbers();
                    }
                }
            }
            System.out.println("Prędzej wygram w totka niż zostanę programistą? Sprawdźmy to!: " + table[i]);
        }
    }
}

Second exercise:

package domowe;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ArrayTest {
    private int[] table1 = new int[6];

    public static void main(String[] args) {

        ArrayTest arrayTest = new ArrayTest();
        arrayTest.fillTable();
        arrayTest.write();
        Arrays.sort(arrayTest.table1);
        arrayTest.write();
        arrayTest.addNumberToIndex();
        arrayTest.write();
    }

    private void addNumberToIndex() {
        Scanner giveIndex = new Scanner(System.in);
        Scanner giveNumber = new Scanner(System.in);
        System.out.println("Enter index of the array: ");
        int tableIndex = giveIndex.nextInt();


        if (tableIndex < table1.length) {
            System.out.println("Table length is: " + table1.length + " \n you have exceeded the length of the table, give another index");
            return;
        } else {
            System.out.println("Enter number to add to table");
            int number = giveNumber.nextInt();
            int[] table2 = new int[table1.length + 1];
            for (int i = tableIndex; i < table2.length; i++) {
                if (tableIndex == 0 && i < table1.length) {
                    table2[i + 1] = table1[i];
                } else {
                    table2[i] = table1[i - 1];
                }
                for (int j = 0; j &lt; table1.length; j++) {
                    if (table2[j] == 0) {
                        table2[j] = table1[j];
                    }

                }
            }
            table2[tableIndex] = number;
            table1 = table2;
        }

    }

    private void fillTable() {
        int max = 1000;
        Random random = new Random();
        for (int i = 0; i < table1.length; i++) {
            table1[i] = random.nextInt(max);

        }
    }

    private void write() {
        System.out.println("tablica: " + Arrays.toString(table1));
    }

    private void addNumber(int a) {
        int[] tablica2 = Arrays.copyOf(table1, table1.length + 1);
        tablica2[tablica2.length - 1] = a;
        table1 = tablica2;
    }
}