Archive

Posts Tagged ‘java’

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

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

Unary encoded for 7-bit ASCII

November 17, 2015 Leave a comment

Chuck Norris challenge in codingame.com

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * unary encoded
 * @author grekz.wordpress.com
 **/
class Bytes {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String MESSAGE = in.nextLine();
        StringBuilder binary = new StringBuilder(), result = new StringBuilder();
        
        for (byte b : MESSAGE.getBytes()) {
            int val = b << 1;
            for (int i = 0; i < 7; i++) {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
        }
        //grekz.wordpress.com
        char current = '2';
        for (int i = 0; i < binary.length(); i++) {
            char c = binary.charAt(i);
            if (current != c) {
                current = c;
                result.append(" ").append(current == '1' ? "0" : "00")
                        .append(" ");
            }
            result.append("0");
        }
        System.out.println(result.toString().trim());
        in.close();
    }
}
Categories: Beyond, CodinGame, Java, snippets

ASCII Art – Challenge

November 17, 2015 Leave a comment

This is the solution of the ASCII Art problem in the CodinGame page.
https://www.codingame.com/games/puzzles

import java.util.*;
import java.io.*;
import java.math.*;

/** @author Juan Mendoza grekz.wordpress.com
* ASCII art allows you to represent forms by using characters. To be precise, in our case, these forms are words. For example, the word “MANHATTAN” could be displayed as follows in ASCII art:
*
* # # # ### # # # ### ### # ###
* ### # # # # # # # # # # # # # #
* ### ### # # ### ### # # ### # #
* # # # # # # # # # # # # # # # #
* # # # # # # # # # # # # # # # #
*
* Your mission is to write a program that can display a line of text in ASCII art.
**/
class Solution {

  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int L = in.nextInt();
    in.nextLine();
    int H = in.nextInt();
    in.nextLine();
    String T = in.nextLine().toUpperCase();
    String [][] chars= new String[27][H];
    for (int i = 0; i < H; i++) {
        String ROW = in.nextLine();
        for(int j = 0; j < 27; j++){
            chars[j][i] = ROW.substring(j*L, (j+1) * L);
        }
    }
    for (int i = 0; i < H; i++) {
        for(char x : T.toCharArray() ){
            x -= 65;
            if(x < 0 || x > 26) x = 26;
            System.out.print(chars[x][i]);
        }
        System.out.println();
    }
  }
}
Categories: Beyond, CodinGame, Java, snippets