Skip to content

Instantly share code, notes, and snippets.

@longda
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save longda/a8dda4937fd18cb64d10 to your computer and use it in GitHub Desktop.
Save longda/a8dda4937fd18cb64d10 to your computer and use it in GitHub Desktop.
Prep
//http://ideone.com/AW2VoY
/* package whatever dijkstra w/ priority queue; // don't place package name! */
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Vertex implements Comparable<Vertex>
{
public final String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(String argName) { name = argName; }
public String toString() { return name; }
public int compareTo(Vertex other) {
return Double.compare(minDistance, other.minDistance);
}
}
class Edge
{
public final Vertex target;
public final double weight;
public Edge(Vertex argTarget, double argWeight) {
target = argTarget;
weight = argWeight;
}
}
/* Name of the class has to be "Main" only if the class is public. */
class PathFinding
{
static void computePaths(Vertex source) {
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
for (Edge e : u.adjacencies) {
Vertex v = e.target;
double weight = e.weight;
double distance = u.minDistance + weight;
if (distance < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distance;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
static List<Vertex> getShortestPathTo(Vertex target) {
List<Vertex> path = new ArrayList<Vertex>();
for (Vertex vertex = target; vertex != null; vertex = vertex.previous) {
path.add(vertex);
}
Collections.reverse(path);
return path;
}
public static void main(String[] args) throws java.lang.Exception
{
// mark all the vertices
Vertex A = new Vertex("A");
Vertex B = new Vertex("B");
Vertex D = new Vertex("D");
Vertex F = new Vertex("F");
Vertex K = new Vertex("K");
Vertex J = new Vertex("J");
Vertex M = new Vertex("M");
Vertex O = new Vertex("O");
Vertex P = new Vertex("P");
Vertex R = new Vertex("R");
Vertex Z = new Vertex("Z");
// set the edges and weight
A.adjacencies = new Edge[]{ new Edge(M, 8) };
B.adjacencies = new Edge[]{ new Edge(D, 11) };
D.adjacencies = new Edge[]{ new Edge(B, 11) };
F.adjacencies = new Edge[]{ new Edge(K, 23) };
K.adjacencies = new Edge[]{ new Edge(O, 40) };
J.adjacencies = new Edge[]{ new Edge(K, 25) };
M.adjacencies = new Edge[]{ new Edge(R, 8) };
O.adjacencies = new Edge[]{ new Edge(K, 40) };
P.adjacencies = new Edge[]{ new Edge(Z, 18) };
R.adjacencies = new Edge[]{ new Edge(P, 15) };
Z.adjacencies = new Edge[]{ new Edge(P, 18) };
computePaths(A); // run Dijkstra
System.out.println("Distance to " + Z + ": " + Z.minDistance);
List<Vertex> path = getShortestPathTo(Z);
System.out.println("Path: " + path);
}
}
//http://ideone.com/IOol9y
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
}
protected class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
Node end = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
}
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) {
return head.next;
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head;
}
n = n.next;
}
return head;
}
}
}
//http://ideone.com/i6a9pP
/* package whatever.strings; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(sort("foobar"));
System.out.println(isPermutation("dog", "god"));
System.out.println(isPermutation("catnip", "cheesenips"));
System.out.println(isPermutation2("dog", "god"));
System.out.println(isPermutation2("catnip", "cheesenips"));
System.out.println(reverse("MONSTERCAT"));
System.out.println(reverse("MONSTERCATS"));
System.out.println(reverse(reverse("MONSTERCAT")));
for (int i = 0; i < 11; i++) {
if (i > 0) System.out.print(", ");
System.out.print(fib(i));
}
System.out.println();
for (int i = 0; i < 11; i++) {
if (i > 0) System.out.print(", ");
System.out.print(fib2(i));
}
System.out.println();
timesTable(5);
timesTable(12);
System.out.println(1 << 1);
System.out.println(1 << 2);
System.out.println(1 << 3);
System.out.println(1 << 4);
System.out.println(getBit(8, 1));
System.out.println(getBit(8, 2));
System.out.println(getBit(8, 3));
System.out.println(getBit(8, 4));
System.out.println(getBit(1 << 2, 2));
System.out.println(getBit(1 << 3, 3));
System.out.println(getBit(1 << 4, 4));
System.out.println(getBit(1 << 5, 5));
System.out.println(setBit(0, 1));
System.out.println(setBit(0, 2));
System.out.println(setBit(0, 3));
System.out.println(setBit(0, 4));
}
public static String sort(String s) {
char[] content = s.toCharArray();
java.util.Arrays.sort(content);
return new String(content);
}
// sorting method
public static boolean isPermutation(String a, String b) {
if (a.length() != b.length()) return false;
return sort(a).equals(sort(b));
}
// character count method
public static boolean isPermutation2(String a, String b) {
if (a.length() != b.length()) return false;
int[] letters = new int[256];
char[] a_chars = a.toCharArray();
for (char c : a_chars) {
letters[c]++;
}
for (int i = 0; i < b.length(); i++) {
int c = (int)b.charAt(i);
if (--letters[c] < 0) return false;
}
return true;
}
public static String reverse(String s) {
int length = s.length(), last = length - 1;
char[] chars = s.toCharArray();
for (int i = 0; i < length / 2; i++) {
char c = chars[i];
chars[i] = chars[last -i];
chars[last - i] = c;
}
return new String(chars);
}
// TODO: dlong - move to recursion?
public static long fib(int n) {
return n <= 1 ? n : fib(n-1) + fib(n-2);
}
private static HashMap<Integer, Long> hm = new HashMap<Integer, Long>();
public static long fib2(int n) {
if (hm.containsKey(n)) return hm.get(n);
hm.put(n, n <= 1 ? n : fib2(n-1) + fib2(n-2));
return hm.get(n);
}
public static void timesTable(int max) {
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= max; j++) {
System.out.print(String.format("%4d", j * i));
}
System.out.println();
}
}
// TODO: dlong - move to bits
public static boolean getBit(int num, int i) {
return ((num & (1 << i)) != 0);
}
public static int setBit(int num, int i) {
return num | (1 << i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment