Archive

Archive for the ‘Java’ Category

NihilistCipher #Java #PoorlyWritten #underpressure :(

July 27, 2017 Leave a comment

When you have free time, you enjoy cracking ciphers. But to crack a cipher you need some ciphered text, and preparing it yourself is way too boring. So, you decide to implement an algorithm that will encode text for you.

After some research you choose the famous Nihilist cipher, and here is how it works (please note that this task uses the modified version of the cipher, so it can differ from what you might already know):

Build a 5 × 5 Polybius square for the given keyword consisting of distinct letters:
first, fill the square with the letters of the keyword row by row.
then, continue filling the square with all the letters in alphabetical order except for those that are already in the square.
add the final (26th) letter to the cell with coordinates (row, column).
Now each letter can be associated with two integers, the indices of a row and a column of the Polybius square containing it. In order to encode a letter, replace it with a concatenation of string representations of these two integers.
Make the lengths of the given plaintext message and the keyword equal (if one string is shorter than another one, consider the cyclic string obtained from it and take its prefix of the length equal to the length of the longer string).
Finally, encode the letters of both strings obtained on the previous step and add up the values at the same positions. The resulting list of integers is the desired ciphertext.
Given some plaintext to encode, a keyword and the coordinates of a Polybius square cell which will contain two letters, return the encoded text.

Example

For plaintext = “codefights”, keyword = “keyword”, row = 1 and column = 2, the output should be
nihilistCipher(plaintext, keyword, row, column) = [36, 27, 35, 26, 46, 55, 54, 44, 64, 64].

https://github.com/Grekz/codefights/blob/master/NihilistCipher.java

Categories: codefights, Java, Programación

Goldbach Conjecture #Java

July 27, 2017 Leave a comment

Return the number of ways to write the given n as the sum of two prime numbers.

Example
For n = 20, the output should be
gold(n) = 2.
There are two ways to represent 20 as the sum of two prime numbers:

3 + 17 = 20,
7 + 13 = 20.


int gold(int n) {
    if ( n <= 2) return 0;
    int res = 0;
    for(int i = 2; i <= n / 2; i++ ){
        if( isPrime(i) && isPrime(n - i) ){
            res++;
        }
    }
    return res;
}
boolean isPrime(int x){
    for (int i = 2; i * i <= x; i++)
        if ( x % i == 0)
            return false;
    return true;
}

WaitForTitle Selenium IDE to Java + testng

February 8, 2017 Leave a comment

This is the code translated from selenium IDE waitForTitle Command to Java code with TestNG.

    for (int second = 0;; second++) {
    	if (second >= 60) org.testng.Assert.fail("timeout");
    	try { if ("My Title here".equals(driver.getTitle())) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

To create a new array or fill it? Java Array Performance

October 12, 2016 Leave a comment

Recently I needed to empty an array to use it later, and asked myself, what is the better option, to create a new one or fill it?
Maybe you have known this long ago, I didn’t. But now, we both know!


int freq[] = new int[256];
//This is better:
                Arrays.fill(freq,0);
//than:
                freq = new int[256];
Categories: Java, Mini-tips, Programación

Read Java input

October 10, 2016 Leave a comment

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        // all ints from 0 to N should be insie array a
    }
Categories: Java, Mini-tips

Get duplicates ints in array in 126 Characters with Java 8 and Streams

August 10, 2016 Leave a comment
*(excluding line breaks, and spaces)

int[] GetDuplicates(int[] n) {
    return 
        Arrays.stream(n)
        .distinct()
        .filter(i -> {
            int c = 0;
            for(int x : n)
                c += x==i ? 1 : 0;
            return c>1;
        })
        .toArray();
}
Categories: Java, Mini-tips, Programación, Tips

Simple Moore Majority Algorithm

April 13, 2016 Leave a comment

Assuming there is always a majority number and that the array has length > 0


public int mooreMajority(int nums[]){
        int result = 0, count = 0;
        for(int x : nums){
            if(count == 0) result = x;
            if(x == result) count++;
            else count--;
        }
        return result;
}