Skip to content

Instantly share code, notes, and snippets.

View vaskoz's full-sized avatar
🏠
Working from home

Vasko Zdravevski vaskoz

🏠
Working from home
View GitHub Profile
@vaskoz
vaskoz / Palindromes.java
Created June 4, 2013 23:27
Print out the palindromes for a list of new line separated words read in from standard input. Usage: java Palindromes < /usr/share/dict/words
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Palindromes {
public static void main(String[] args) {
Set<String> words = new HashSet<String>();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String word = scanner.next().toLowerCase();
@vaskoz
vaskoz / AlgorithmL.java
Created June 5, 2013 06:41
Implementation of Lexicographic Permutation Generation. Algorithm L by Knuth. Usage: java AlgorithmL "122233344555"
import java.util.Arrays;
public class AlgorithmL {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: provide a string (quoted if multiword)");
System.exit(1);
}
char[] letters = args[0].toCharArray();
Arrays.sort(letters);
@vaskoz
vaskoz / OpenCvExample.groovy
Created June 8, 2013 22:41
This Groovy code results in Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
import org.opencv.core.Core
import org.opencv.core.CvType
import org.opencv.core.Mat
class Main {
static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String[] args) {
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("m = " + m.dump());
@vaskoz
vaskoz / OpenCvExample.java
Created June 8, 2013 22:43
Java version of OpenCvExample. Works as expected.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class OtherMain {
static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String[] args) {
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("m = " + m.dump());
import org.opencv.core.Core
import org.opencv.core.CvType
import org.opencv.core.Mat
class Main {
static {
Runtime.getRuntime().loadLibrary0(groovy.lang.GroovyClassLoader.class, Core.NATIVE_LIBRARY_NAME)
}
public static void main(String[] args) {
@vaskoz
vaskoz / OneSwapMinNumber.java
Last active December 19, 2015 11:19
Swap any num[i] and num[j] such that you return the smallest number possible.
import java.util.Arrays;
public class OneSwapMinNumber {
/**
* Worst case running time
* O(11*k) - k is the number of digits in the number (length of string)
*/
public static String min(String num) {
char[] digits = num.toCharArray();
int[] rindex = new int[10];
@vaskoz
vaskoz / InvokeStatic.java
Created July 9, 2013 04:58
Calling a static method is the one and only use case of invokestatic.
public class InvokeStatic {
public static void someStaticMethod() {}
public static void main(String[] args) {
someStaticMethod();
}
}
//Compiled from "InvokeStatic.java"
//public class InvokeStatic {
// public InvokeStatic();
@vaskoz
vaskoz / InvokeInterface.java
Last active December 19, 2015 12:28
Only gets used when calling a method on an object from an Interface type.
public class InvokeInterface {
public void methodA() {
Runnable r = new Runnable() {
@Override public void run() {}
};
r.run();
}
}
//Compiled from "InvokeInterface.java"
@vaskoz
vaskoz / InvokeSpecial.java
Created July 9, 2013 05:14
Invokespecial used in 3 cases: call to super method, call to constructor, and call to private methods.
class A {
public void superMethod() {}
}
public class InvokeSpecial extends A {
// Implicit super constructor call - invokespecial
private byte b = 0;
{ // instance initializer - invokespecial
b = 1;
}
@vaskoz
vaskoz / For.java
Created July 15, 2013 03:39
Nested for loops (N^2) where N is Integer.MAX_VALUE. javap -c Output of bytecode produced by javac compiler below that is the output produced by the PrintAssembly (hsdis) plugin.
public class For {
public static void main(String[] args) {
char x = 'a';
for (int i = 0; i < Integer.MAX_VALUE; i++)
for (int j = 0; j < Integer.MAX_VALUE; j++)
x = 'b';
}
}
//***************************************************************************************
// javap -c For