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. 

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

Advent of Code 2018

I find this challenge on twitter: adventofcode.com

Awesome challenge! I decided to manage with it. But for the beginner is very hard! After 5 days java lessons I couldn’t resolve second advent day! But I am not going to quit it! I think this is good opportunity to learn thinking as a developer. So after 7 days java lessons I resolve second advent day! I am very proud of myself. 

To be honest,  for one day adventOfCode You get two puzzles. One puzzle is for silver star (basic and easier puzzle and You have to resolve it!) second is for gold star (much difficult puzzle but this is additional puzzle) I didn’t resolve second puzzles in first and second day. Maybe later I will come back and resolve them. So I going to do this right now! 

  1. first day of advent code: title Frequency: https://adventofcode.com/2018/day/1

My code is: 

 
/*
 * Copyright (c) 2018. by 10bashi - I just started 
learning programming. So please forgive for my mistakes!
 */

package advent.of.code.day1;

public class Frequency {
    private static int frequency = 0;
    private static String input = "-14\n" +
            //you should paste in Your input here
12\n" +
            "-114806";
    private String[] tableOfFrequency;
    private int[] intTable;
    private int[] frequences;

    public static void main(String[] args) {
        zmienTablice();
        wpiszDoTablicy();
    }


    private static void zmienTablice() {
        String[]table = input.split("\n");
        tableOfFrequency = new String[table.length];
        intTable = new int[table.length];
        for (int i = 0; i<table.length; i++) {
            intTable[i] = Integer.parseInt(table[i]);
        }
    }

    private static void wpiszDoTablicy() {
        int i = 0;
        for (int b : intTable) {
            frequency = frequency + (b);
            i++;
        }
        System.out.println(i);
        frequences = new int[i - 1];
        System.out.println(frequences);
        checkExist(frequency);
    }

    private static boolean checkExist(int a) {

        for (int b : frequences) {   // for each
            if (b == a) {
                System.out.println(true);
                return true;
            }
        }
        System.out.println(false);
        return false;
    }
}
 

2. second day of advent code – title: Checksum: https://adventofcode.com/2018/day/2

/*
 * Copyright (c) 2018. by 10bashi - I just started learning programming. So please forgive for my mistakes!
 */

package advent.of.code.day2;

import java.util.HashMap;
import java.util.Map;

public class Checksum {

   private int allTwo = 0;
    private int allThree = 0;
   private int checksum = 0;

    private static String input = "ubkfmdjxyzlbgkrotcepvswaqx\n" +
// you should paste in Your input here - You can get in from url 
            "uikfmdkuyzlbgerotcepvswaqh\n" ;

    public static void main(String[] args) {

        String[] data = input.split("\n");

        for (int i = 0; i<data.length; i++) {
            System.out.println(i + " /////////");
            outPutMap2(countNumberOfLetters(data[i]));

        }

    }

    private static void outPutMap2(Map<Character, Integer> map) {

        int wystapenieDwochTakichSamychLiter = 0;
        int wystapienieTrzechTakichSamychLiter = 0;
        for (Map.Entry<Character, Integer>entry : map.entrySet()) {  
            if (entry.getValue() == 2) {
                wystapenieDwochTakichSamychLiter++;
            }
            if (entry.getValue() == 3) {
                wystapienieTrzechTakichSamychLiter++;
            }

        }

        if (wystapenieDwochTakichSamychLiter <= 1) {
            allTwo += 1;
        }
        if (wystapienieTrzechTakichSamychLiter>= 1) {
            allThree += 1;
        }


        checksum = allTwo * allThree;
        System.out.println("ilość wystąpienia 2 takich samych liter: " + wystapenieDwochTakichSamychLiter);
        System.out.println("ilośc wystąpenia 3 takich samich liter: " + wystapienieTrzechTakichSamychLiter);

        System.out.println("suma wszystkich par:" + allTwo);
        System.out.println("suma wszystkich potrójnych :" + allThree);

        System.out.println("checksum is: " + checksum);
    }

    private static Map<Character, Integer> countNumberOfLetters(String text) {
        Map<Character, Integer>map = new HashMap<>();
        for (int i = 0; i <text.length(); i++) {
            char s = text.charAt(i);
            if (map.get(s) == null) {
                map.put(s, 1);
            } else {
                //int liczbaWystapein = map.get(s);
                map.put(s, map.get(s) + 1); 
            }
        }
        return map;
    }

}
 

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;
    }
}

Lesson #1 – headache from Java Basics

So, I`m after first lesson of Java Basics. What can I say? It will be tuff road to achieve goal.

For now we have theory with some small coding. Coach introduced a lot of material that U can find in Udemy Java Basics. He explain everything from beginning. Said about basics type, methods, packages, basic math operations, declaration of class type, method type, differences of private, public, protected. But He always repeat – “You will learn about it later, we will talk about the details later!” So to be honest I cant wait for the weekend to go to school and learn.

Tip for beginner.  You should watch Udemy course before Your course. It will be much more easier to keep up with coach.

But I know theory is theory practice is practice! After 7 hours (sic) of lessons I get headache but I know  it`s worth it!

I write two tiny codes:

  1. Calculator:
  2. Homework

Calculator:

package day1;

public class Calculator {

    static int a = 5; //declaration class type
    static int b = 6; //declaration class type
    
    public static int sum() { //declaration method
    }

    public static int substraction() { // return method
        return a - b;
    }

    public static int multiplitaction() {
        return a * b;
    }

    public static double division() {
        double c = (double)a/b;  // variable projection 
        return c;
    }

    public static void main(String[] args) {
        int resultSum = sum();
        System.out.println("sum " + resultSum);
        int resultSubstraction = substraction();
        System.out.println("substraction " + resultSubstraction);
        int resultMultiplication = multiplitaction();
        System.out.println("multiplication " + resultMultiplication);
        double resultDivision = division();
        System.out.println("division " +  resultDivision);
    }
}

Homework – at first glance I thought that is easy to do. But after trying to write it I had to think a lot and I get headache 😀

Topic: Write method accepting two arguments int type and which returns result of divide those types in a fraction format. Example: 1 2/5. And You have to remember when You divide two int You get int and You cant division by zero. Catchy isn`t it?

Answer:

package homeworks;
public class Homework1 {
    public static void main(String[] args) {
        System.out.println(divide(7, 5));
        System.out.println(divide(10, 5));
        System.out.println(divide(10, 0));
    }
    static String divide(int i, int j) {
        if (j != 0) { // check denominator it cant be 0
            int div = i / j;
            if (i % j == 0) { // check modulo, when it is 0 return string whit out fraction
                return "" + div;
            } else { // else modulo is not 0 your result should be fraction
                int mod = i % j;
                return "" + div + " " + mod + "/" + j;
            }
        } else {
            System.out.println("You have been trying to divide by zero");
            return "";
        }
    }
}

Two difficult things. First. Method should returns String! Why? Because You have to return division in fraction as example is 1 2/5 not 1.4. Second I didn`t know that You can combine String with int(double etc).

Example:

 return "" + someNumber + "/" + someNumber2

Modulo is very ancillary calculation. Coach said that we are going to use it a lot of time.

Check this:  7%5 = 2. You know after that Your result is fraction. So You should return total number ( 1 because 5 in 7 is 1) + modulo + denominator.  Know You will ask what about result for ex.  4/8 ? You should correct to 1/2. Yes, You have right. Know I am going to correct this!  Stay tuned!

Prework #3

String welcomingMessage = "Hi everybody, today I started learn Java!"
System.out.println(welcomingMessage);

 

How excited it is.
So today I learned basics about variables and methods.

I will tell You what I know now!

Variables

Variables for me is variable == some data, some value – this is not the end of the truth but I understand it that way.

In Java there are couple types of variables:

  • int – integer number eg. 10, 5, 4, 198273 etc.
  • double – floating numbers  eg. 10.5, 1.25, 45.4, etc.
  • stringtext variable eg. “this is my blog welcome” String is class represent character string
  • boolean – this variable has only two value true or false

So if You want to declare variable You should think about it type. I didn’t write all types in Java here. But I will later.

For e.g. my age – age is a number, it is not float number is integer – so You have to write it like this:

 int myAge = 36;

In this declaration I do one additional thing. I declared variable as integer with name variable myAge (first thing), but second I initialized it to the value 36. I do this by equal sign “=”.

Variables can be local variables or class variables. But this is history for another time.

Methods

Method is a function which You can execute, induce. You declare method because You want to do sth, count sth or show sth. I understand method as doing sth.

static void doSthAsIWant(){ // do something with variables, count sth, }

Ok, basic example. You want to say hello to a stranger girl!

static void sayHelloToStrangerGirl(){

System.out.println("Hi Stranger Girl, How are You?!");

}

To execute this method You write only:

sayHelloToStrangerGirl();

To understand look at this code:

public class Methods {

    public static void main(String[] args) {

        sayHelloToStrangerGirl(); // this is execute of method
    }

    static void sayHelloToStrangerGirl() { //this is Your method
        System.out.println("Hi Stranger Girl, How are You?!");

    }
    
}

to be continued…

Prework #2

Git is git!

Yes I know. Old joke. But in polish git colloquially means – cool.

In this lesson I will show basic comments in git.

But first is first. What is git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

source https://git-scm.com

So first off all there are two types of repository. Local and remote.

Local is placed on Your computer and and it is not linked to remote repo.

All commands and operation I will show based on terminal. I user iTerm on Mac. Powerfull app.

To create local repo You have to:

mkdir name_folder - create folder

cd name_folder - open folder 

git init - initialization folder for git - name of folder is name of Your folder

touch name_file.htmlcreate some file - html, txt whatever e.g

git add name_file.html - add file to repository

git commit -m "comment" - commit Your repo with comment

git status - checking git status

example 1. Creating local repo:

That`s it You create new repo in git. But You can create some branches in Your repo!

Branches are something that tree has. You have root (master) then tree is growing has one two, “n” branches. To create branch use:

git branch name_branch - how can be more simple?

git checkout name_branch - switch to you new branch

git checkout -b name_new_branch - this is combine of this two first command creating a branch and switching to it.

You can check difference with branches. Just compare two of them:

git diff master name_branch

git diff name_new_branch name_branch

Combine two branches? Simple:

gif marge name_branch name_new_branch

You have to remember. To merge two brenches You schould checkout to master. Or if You want to merge branch with master You should do the same.

Prework #1

So it`s time to do prework #1. I got instructions to install some programs before
I start course #SDA.

Programs are:

  1. Slack Communicator
  2. Java JDK8
  3. Intellij IDEA
  4. Git
  5. Maven (sic!!!) – it was very difficult – about this installation a few lines below.
  6. MySQL
  7. MongoDB
  8. Apache Tomcat
  9. Wildfly
  10. Wireshark
  11. MidnightCommander (MC) – they said – write in terminal brew install mc – yes sure mhm – it didn’t work but there is solution so scroll down to see what You have to do to installed it!

Ok, so the tough stuff to install are Maven and MC.  Rest of this program are easy to install so I will not write how to install it.

To install Maven You should install Java SDK8. You should set JAVA_HOME. This is priority. When You do this it’s not enough to work Java properIy. I have to say I did some research to complete this task. I found two sites about installing Maven.

This sites helps me to install this Maven “headake”.

You should remember that instruction on official site is very poor and its not explain everything.  You can look at here: https://maven.apache.org/install.html   For beginers who don’t now what is bash or what is terminal, vim etc it will be a nightmare. I cant imagine people who haven’t learn some basic bash or they haven’t have any contact with computer science and they install it as simple as Maven site says.

So this is install method which I used.

Set JAVA_HOME, MAVEN_HOME and M2_HOME using vim. In terminal:

vim .profile

And write this one (in vim click es and the i to insert):

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
export JAVA_HOME
export MAVEN_HOME="/Users/your_name_profile/your path to extract files apache maven/apache-maven-3.5.4"    
export MAVEN_HOME=$M2_HOME
export M2=$M2_HOME/bin
export MAVEN_OPTS="-Xms256m -Xmx512m"
export M2_HOME=/Users/your_name_profile/your path to extract files apache maven/apache-maven-3.5.4
export PATH=$PATH:$M2_HOME/bin

After copying it write :wq (write and quit) from vim. Then write source .profile in terminal enter.

Test Your installation: In terminal write java -version then mvn -version. You should have something like this:

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
users-MacBook-Pro:~ you_profile_name$
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)
Maven home: /Users/your_name_profile/your path to extract files apache maven/apache-maven-3.5.4
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre
Default locale: pl_PL, platform encoding: UTF-8
OS name: "mac os x", version: "10.14", arch: "x86_64", family: "mac"

End of story Maven!

So time for:

  • Midnight Commander

In PDF file from SDA there was sth like this:

In terminal enter: brew install mc – It didnt work. Why? Beacouse You didn’t install Homebrew. This was let`s say – tricky. Please go to: https://brew.sh/index  and follow instructions. Read documentation. Then come back to terminal and install Midnight Commander. It was nice to see old fashion commander as Norton Commander was. I remebered it. I grew up on Norton Commander.

So thats it. I hope I helped You to install this two tricky things.