My experience on my daily works... helping others ease each other

Monday, January 13, 2020

Disruptive UI/UX - The new interactive design of a website

The faces of a website have evolved drastically from normal static one page to multiple page interactive and so on. With Big Data and ML, comes chatbot and more interactive website which more customer engaging concept.

It no time, we will definitely view a website through the Augmented Reality concept. Just read the name on a billboard, and then you will get to see the whole company profile in AR mode. Well, while waiting for this to happen, let us take a look at few revolutionize user engaging designs of a website.

As shared by Bruno Simon at https://bruno-simon.com/


Whom also the master of behind many 'abnormal' website like Orano


and Prior Holding



Go ahead browse the website and you will feel different.




Share:

Codility - PermCheck (Check whether array A is a permutation)


This is the second lesson in Codility. Given an array of integer N, you need to find if the given array is a permutation array or perfect array in sequence (if all numbers are sorted accordingly). The full explanation of the lesson is here.

It does not take me long compared to the previous lesson. I scored 100% on the first trial and here is the explanation of my code.

Since it already stated that the array starts from a positive number, I just set the expected int is 1 and missing to 0
int expectedInt = 1;
int missingInt = 0;
Then, the array is sorted accordingly. I’m using java.util.Arrays library
 Arrays.sort(A);
To find the missing int, just loop the sorted array and find the first occurrence of the missing int.
for (int x : A){ //loop to find the missing int
     if (x == expectedInt){
          expectedInt++;
     } else {
         missingInt = expectedInt;
     }
 }

This code is not perfected yet as it will continue to search despite it found the missing int. I should further improve it later. However, for codility purposes, it stops here. You can further enhance the code by implementing the break clause once found the first missing int.
The code
And here is the result. Ya, it shows 2 minutes; that is because I test it again to snapshot the result :). In actual, for a few trials, it took me around 2 hours to perfect it and scored 100%
Result

You can download the full code at
  1. Bitbucket — git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-permcheck.git
  2. Github — git clone https://github.com/masteramuk/codility-lessoncode.git





Share:

Codility - FrogRiverOne (Find the earliest time when a frog can jump to the other side of a river)

This is the fourth lesson in Codility. You need to find the fastest (earliest) time possible for a frog to start jumping towards the other side of the river. You will be given an array that reflecting the position of jumping/landing point for the frog. 

The frog will start to jump when all landing point is in the right position. Two input is given; X as the final jumping position and Y array of integer. For each element in the array, every index is considered as seconds. You need to arrange the number in the array and trigger when all are in sequence with X as the last element.


It took me another 1 full day and many try-n-error to get it perfect 100%

In the beginning, I’m using 2 Array of Integer as shown below. There are few test cases that failed because the time taken to process is more than the limit given.
100% accuracy but the overall score is 18%
With an improvement in the code, I managed to improve the overall score to 54%. I managed to reduce some of the performance issues.
100% accuracy but overall score is 54%
It looks like Array.copyOf and Arrays.stream do take a longer time to process.

Another improvement has lessened the length of the code and improve the overall score to 63%
63% overall score
The code above simply set the C array to value one of the position indexes of A. Here are the list of test that it fails
▶medium_range
 arithmetic sequences, X = 5,000✘TIMEOUT ERROR
 running time: 0.112 sec., time limit: 0.100 sec.
 1.0.112 sTIMEOUT ERROR, running time: 0.112 sec., time limit: 0.100 sec.
 ▶large_random
 10 and 100 random permutation, X = ~10,000✘TIMEOUT ERROR
 running time: 1.128 sec., time limit: 0.880 sec.
 1.1.128 sTIMEOUT ERROR, running time: 1.128 sec., time limit: 0.880 sec.
 2.0.200 sOK
 ▶large_permutation
 permutation tests✘TIMEOUT ERROR
 running time: 1.716 sec., time limit: 0.880 sec.
 1.1.716 sTIMEOUT ERROR, running time: 1.716 sec., time limit: 0.880 sec.
 2.6.000 sTIMEOUT ERROR, Killed. Hard limit reached: 6.000 sec.
 ▶large_range
 arithmetic sequences, X = 30,000✘TIMEOUT ERROR
 Killed. Hard limit reached: 6.000 sec.
 1.6.000 sTIMEOUT ERROR, Killed. Hard limit reached: 6.000 sec.
I changed my strategy. Instead of using a normal Array of integer, I implement List & ArrayList.

Bad improvement - 54% overall
Instead of getting better, it is getting worse. I google on it and found that List do have performance issues and found few suggestions on it. Either use Hashmap, HashSet, TreeSet or GapList. 


I do improve the process on my laptop and surprisingly, it was way faster than List or ArrayList. Unfortunately, Codility does not support the library. Hence, I need to look for another alternative.

Based on the performance comparison between Hashmap, HashSet, and TreeSet, HashSet seems promising. And so it did. My final code is using HashSet and finally, the result shown is 100%. Here is part of the code:
1. Definition
HashSet list= new HashSet();

2. Used
           for(int idx = 0; idx < A.length; idx++){
         if ( !list.contains(A[idx]) ){
            list.add(A[idx]);
         }
         if ( list.size() == X ){
            return idx;
         }
      }

I also found a few solutions which score 100%
    This solution was shared by someone and it claims score 100/100
    public int solution(int X, int[] A) {
        int[] B = A.Distinct().ToArray();
        return (B.Length != X) ? -1 : Array.IndexOf(A, B[B.Length - 1]);
    }
    
    This solution was shared too and score 100/100 for correctness, task, and performance
    public int solution(int X, int[] A) {
        HashSet unique= new HashSet();
        for (int i = 1; i<= X; i++){
            unique.add(i);
        }
        for(int j = 0; j< A.length; j++){
            if(unique.contains(A[j])){
                unique.remove(A[j]);
                   if(unique.isEmpty()){
                         return j;
                    }
            }
        }
        return -1;
    }


Full source code is accessible at
  1. Bitbucket — git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-frogriverone.git
  2. Github — git clone https://github.com/masteramuk/codility-lessoncode.git


Share:

Sunday, January 12, 2020

Codility - TapeEquilibirium (Finding the lowest difference in an array)


Given an array of int (ranging from -ve to +ve value) with the minimum number of an element is 2 and the maximum element is 100,000, you need to find the lowest difference between two sets of value (of the total sum of the array).

The actual description can be seen at Codility website (https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/)

It took me 1 full day to resolve it despite the time given was only 120 minutes. On the first trial, I got 88% correct. It resolves all but two out of all test was considered as a failure due to time taken was more than expected. Next few trials, I score between 66% and 84%. The only issue was the double element array and small element array.
After a while, I figure out. The line below
int totalSum = Arrays.stream(A).sum();
is causing the time taken more than expected. Although it passed, it was 0.020 seconds more than the limit.

After a few modifications, wallawei, finally, I achieved 100%.

Snapshot of the code

Result

Full code is accessible at
  1. Bitbucket — git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-tapeequilibrium.git
  2. Github — git clone https://github.com/masteramuk/codility-lessoncode.git




Share:

Saturday, January 11, 2020

Codility - PermMissingElem (Find the missing element in a given permutation)

This is a lesson in codility for the Time Complexity algorithm. Given an array of integer, you need to find the lowest missing integer.

I managed to score 100% for it.

Here is the snapshot of the code:
Based on the length, for all value in Array A, start the search and compare the initial value; that is 1 (expectedInt). If the value exists, the expectedInt is added 1 value.
                 if( A.length > 0 ){
           for (int x : A){
             //if found a value based on expected value 
             if (x == expectedInt){ 
                expectedInt++;
             } else { //if found a mising value 
                missingInt = expectedInt;
             }
           }
         }
If the Array is empty, we will just set the missingInt to 1.
        if (A.length == 0) {
          missingInt = 1;
     }
If no missing int found, then we just add additional 1 to the last value found
       if (missingInt == 0){
           missingInt = A[A.length — 1] + 1;
       }

The complete code is shown below
Codility - PermMissingElem sample code
My result is shown below
Result


You can download the code from here:

  1. Bitbucket - git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-permmissingelem.git
  2. Github - git clone https://github.com/masteramuk/codility-lessoncode.git



Share:

Codility - FrogJmp (Count minimal number of jumps from position X to Y)

Codility - FrogJmp

Count the minimal number of jumps from position X to Y

FrogJmp is the third lesson number 1 out of three in the list for Time Complexity algorithm. Basically, it is an algorithm to count the number of the jump from one X location to Y location when the number of the step taken is given as Z for each single jump

public int solution(int X, int Y, int D){
        int a = 0;
        Y = Y - X; //setting the initial value test

        if (Y >= 1){ //evaluating the Y value
            a = Y/D; //setting the initial return value
            if (Y % D > 0){ //evaluating the possible number of jump to add additional value
                a++;
            }
        }
        if ( a == 0 && (Y%D==0) && Y > X ){ //final check
            a = 1;
        }
        return a;
    };

Result of the code

Code is downloadable from
Github - https://github.com/masteramuk/codility-lessoncode.git
Share:

Thursday, January 9, 2020

Codility - OddOccurrencesInArray (Find value that occurs in odd number of elements)

Given an array, find a value that has no duplication or unpaired value. I scored 100%

public int solution (int[] A){
        int a = 0;
        //sort the array
        Arrays.sort(A);
       
        int[] sortA = A;
        int x = 0;
        int cnt = 1;
        int[] oddA = new int[]{};
        //int idxOdd = 0;
       
        while ( x < sortA.length ){
            if ( x != 0 ){
                if (a == sortA[x]){
                    cnt++;
                } else {
                    if (cnt % 2 > 0){
                        oddA = Arrays.copyOf(oddA, oddA.length + 1 ); //sortA[ x - 1];
                        oddA[oddA.length - 1] = sortA [x - 1];
                    }
                    a = sortA[x];
                    cnt = 1;
                }
            } else {
                a = sortA[x];
            }
            x++;
        }
        if ( x == sortA.length && cnt == 1) {
            oddA = Arrays.copyOf(oddA, oddA.length + 1 ); //sortA[ x - 1];
            oddA[oddA.length - 1] = sortA[x - 1];
        }
        System.err.println("sortA: " + Arrays.toString(sortA) + System.lineSeparator() + "Result: " + Arrays.toString(oddA));
        return oddA[0];
    };

The code is downloadable from
Share:

Codility - CyclicRotation (Rotate an array to the right by a given number of steps)

This is the second lesson in Codility; that is to rearrange an array to the right based on the number of steps given. It will be two input and I scored 100% for it.

public int[] solution (int[] A, int K){
        int a = 0;
        int[] sortA = A;
        int x = 0;
     
        while (x < K && x < A.length){
            a = sortA[sortA.length - 1];
         
            Arrays.copyOfRange(sortA, 0, sortA.length);
            sortA = Arrays.copyOf(sortA, sortA.length);
            System.arraycopy(sortA, 0, sortA, 1, sortA.length - 1);
            sortA[0] = a;
         
            System.err.println( x + " : " + Arrays.toString(sortA) + " - " + a);
         
            x++;
        }
        return sortA;
    };


  100% CyclicRotation

You can get the code at
Bitbucket - git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-cyclicrotation.git
Github - git clone https://github.com/masteramuk/codility-lessoncode.git

Share:

Codility - Find longest sequence of zeros in binary representation of an integer.

I'm taking the coding test at Codility for a job offered by a Japanese company (of which I did not get it). For practice, I tried all and this are the code (in Java) for binary gap

public int solution (int N){
//Convert the value to binary and split the value between number 1
        String s2[] = Integer.toBinaryString(N).substring(0, Integer.toBinaryString(N).lastIndexOf('1') ).split("1");
        int x = 0;
        int a = 0;
        
//find and count the longest zeros
        while(x < s2.length){
            a = a > s2[x].length() ? a : s2[x].length();
            x++;
        }
        
        System.out.println("Number: " + N + System.lineSeparator() + "Binary: " + Integer.toBinaryString(N) 
                + System.lineSeparator() + "Zero: " + a);
        return a;
    };

It score 100% for codility test.

You can download the code here
Bitbucket - git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-binarygap.git
Github - git clone https://github.com/masteramuk/codility-lessoncode.git
Share:

Wednesday, January 1, 2020

Tableau For Beginner

I'll be publishing an ebook on Visualizing using Tableau. To those interested, please PM ya. Here is the front page.




Share:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates