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 / ride4ruby.rb
Created January 11, 2011 05:46
January 2011 Engine Yard Ruby Puzzle
require 'net/http'
require 'rexml/document'
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
map_key = "#{YOUR GOOGLE MAP API KEY}"
Position = Struct.new(:address, :latitude, :longitude)
western_most = Position.new("", 0.0, 180.0)
eastern_most = Position.new("", 0.0, -180.0)
@vaskoz
vaskoz / Switch.java
Created December 29, 2012 20:37
Compare JDK7 String switch to If-Then-Else
public class Switch {
public String switchTest(String input) {
String result = null;
switch(input) {
case "foo": result = "foo1"; break;
case "bar": result = "bar1"; break;
case "foo:bar": result = "foo1:bar1"; break;
default: result = "no match"; break;
}
return result;
@vaskoz
vaskoz / Switch.bytecode
Created December 29, 2012 20:44
Bytecode of 2 methods in Switch.java
public java.lang.String switchTest(java.lang.String);
Code:
0: aconst_null
1: astore_2
2: aload_1
3: astore_3
4: iconst_m1
5: istore 4
7: aload_3
8: invokevirtual #2 // Method java/lang/String.hashCode:()I
@vaskoz
vaskoz / Gemfile
Created January 24, 2013 04:25
Base Gemfile to run both MRI Ruby and Torquebox JRuby
source 'https://rubygems.org'
gem 'rails', '3.2.11'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'jruby-openssl'
@vaskoz
vaskoz / torquebox.env
Created January 24, 2013 04:35
Torquebox environment setup file.
export TORQUEBOX_HOME=/opt/torquebox-2.x.incremental.1334
export JAVA_OPTS="-Xmx2048m -XX:MaxPermSize=400m"
export JBOSS_HOME=$TORQUEBOX_HOME/jboss
export JRUBY_HOME=$TORQUEBOX_HOME/jruby
export PATH=$JRUBY_HOME/bin:$PATH
@vaskoz
vaskoz / DualPivotQuicksort
Created February 15, 2013 06:42
Thresholds of DualPivotQuicksort
/**
* If the length of an array to be sorted is less than this
* constant, Quicksort is used in preference to merge sort.
*/
private static final int QUICKSORT_THRESHOLD = 286;
/**
* If the length of an array to be sorted is less than this
* constant, insertion sort is used in preference to Quicksort.
*/
@vaskoz
vaskoz / Hash.java
Last active December 15, 2015 21:49
Compare Java hashing functions between JVM invocations. NOTE: This example will only run on Java 7.
public class Hash {
public static void main(String[] args) {
String str = "Java Programming Language";
System.out.println(str + " hashCode(): "
+ str.hashCode()
+ " new Java 7 hashCode used by collections: "
+ sun.misc.Hashing.stringHash32(str));
}
}
@vaskoz
vaskoz / StringHashCollisionGenerator.java
Created June 4, 2013 03:44
Generates Java Strings with the same hashCode.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class StringHashCollisionGenerator {
public static void main(String[] args) {
if (args.length != 1) {
printUsage();
System.exit(1);
@vaskoz
vaskoz / HashMapVictim.java
Created June 4, 2013 03:46
Loads a list of Strings into a HashMap. Usage: java HashMapVictim < collisionStrings.txt
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashMapVictim {
final static Object PRESENT = new Object();
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
Scanner sc = new Scanner(System.in);
@vaskoz
vaskoz / Anagrams.java
Created June 4, 2013 23:11
Print all the anagrams present in a list of words. Usage: java Anagarams < /usr/share/dict/words
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Anagrams {
public static void main(String[] args) {
Map<String, Set<String>> anagrams = new HashMap<String, Set<String>>();