Archive

Archive for the ‘snippets’ Category

Query to know what changed in your database objects

January 18, 2016 Leave a comment

select last_ddl_time, object_name, object_type
from dba_objects
where last_ddl_time is not null
order by 1 desc;

Advertisements

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

Simplify a fraction in Java

October 22, 2015 Leave a comment


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

/**
* This method allows you to simplify a fraction
*
* @author grekz
* Sample input: 10 2
* Sample output: 5 1
**/
class Solution {

public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int A = in.nextInt();
int B = in.nextInt();

int gcd = findGCD(A, B);
System.out.println( gcd > 1 ? (A/gcd) + " " + (B/gcd) : A + " " + B);
}
private static int findGCD(int number1, int number2) {
return number2 == 0 ? number1 : findGCD(number2, number1%number2);
}
}

Obtener el tiempo de lo que dura la ejecución de un método en Java

January 20, 2015 Leave a comment

		long ini = System.nanoTime();
		doYourStuffMethod();
		long fin = System.nanoTime();
		long duracion = TimeUnit.NANOSECONDS.toSeconds(fin - ini);
		System.out.println("Esa madre duró estos segundos:" + duracion);

Categories: Java, Mini-tips, snippets

Como leer archivo Zip o Unzip txt en Java

January 20, 2015 Leave a comment

Obtener el archivo de texto en de un archivo zip


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipFile {
	public static void main(String[] args) {
		String fileName = "./files/myZipFile.txt.zip";
		unzipTextFile(fileName);
	}

	public static void unzipTextFile(String zipName) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(zipName);
			ZipInputStream zis = new ZipInputStream(fis);
			ZipEntry ze = zis.getNextEntry();
			InputStreamReader isr = new InputStreamReader(zis);
			BufferedReader reader = new BufferedReader(isr);
			String outFileName = "./files/" + ze.getName();
			//"./files/myZipFile.txt" -> output file
			FileWriter fw = new FileWriter(outFileName);
			String line;
			while ((line = reader.readLine()) != null) {
				// manipula line
				fw.append(line).append('\n');
			}
			fw.flush();
			fw.close();
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Categories: Java, Mini-tips, snippets, Tips

Simple regex to remove begining of url

September 22, 2014 Leave a comment
Categories: Java, Mini-tips, snippets