Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Kanishk Kunal

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Algortihm Challenge 1: No division!

// http://gurus.wordpress.com/2007/06/28/algortihm-challenge-1-no-division/

P[0] = 1;
for (int i = 1; i < N; i++)
{
	P[i]=P[i-1]*X[i-1];
}
Q[N-1] = 1;
for (int i = N-2; i >= 1; i--)
{
	Q[i]=Q[i+1]*X[i+1];
}

for (int i = 0; i < N; i++)
{
	M[i]=P[i]*Q[i];
}

Double Array Manipulations

// Manipulations with double dimension arrays

//equate two arrays
private static boolean isEqual(char [][]arr1, char [][]arr2) {
		if(arr1.length!=arr2.length) return false;
		boolean res = true;
		for(int i=0; i<arr1.length; i++) {
			res = res & Arrays.equals(arr1[i], arr2[i]);
		}
		return res;
	}
	
//rotate clockwise 90 degrees
private static char [][]rotate90(char [][]arr) {
	char [][]ret = new char[arr.length][arr[0].length];
	for(int j=0; j<arr[0].length; j++) {
		for(int i=0; i<arr.length; i++) {
			ret[i][j] = arr[j][i];
		}
	}		
	//print(ret);
	return reflect(ret);
}
	
//reflect horizontally	
private static char [][]reflect(char [][]arr) {
	char [][]ret = new char[arr.length][arr[0].length];
	for(int i=0; i<arr.length; i++) {
		for(int j=0; j<arr[0].length; j++) {
			ret[i][j] = arr[i][arr[i].length-j-1];
		}
	}		
	//print(ret);
	return ret;
}

//prints for debugging	
private static void print(char [][]arr) {
	for(int i=0; i<arr.length; i++) {
		System.out.println(Arrays.toString(arr[i]));
	}
	System.out.println();
}

graham scan

// graham scan incomplete

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Stack;
/*
ID: kanishk1
LANG: JAVA
TASK: moat
*/

public class moat {
	static class Point implements Comparable {
		public int x; public int y;
		Point pv;
		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
		public void setPivot(Point pv) {
			this.pv = pv;
		}
		public int compareTo(Object o) {
			Point p = (Point)o;
			double a1 = ((y-pv.y)*1.0)/(x-pv.x);
			if(a1<0) a1 = 2+a1;
			double a2 = ((p.y-pv.y)*1.0)/(p.x-pv.x);
			if(a2<0) a2 = 2+a2;
			if(a1>a2) return 1;
			else if(a1<a2) return -1;
			else return y-p.y;
		}
	}
	static Point []points;
	 public static void main (String [] args) throws IOException {
		    
		 // Use BufferedReader rather than RandomAccessFile; it's much faster
		    BufferedReader f = new BufferedReader(new FileReader("moat.in"));
		                                                  // input file name goes above
		    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("moat.out")));

		    int N = Integer.parseInt(f.readLine());
		    points = new Point[N];
		    int lx = Integer.MAX_VALUE, ly = Integer.MAX_VALUE;
		    Point p = null;
		    for(int i=0; i<N; i++) {
		    	StringTokenizer st = new StringTokenizer(f.readLine());
		    	int x = Integer.parseInt(st.nextToken());
		    	int y = Integer.parseInt(st.nextToken());
		    	
		    	points[i] = new Point(x,y);
		    	if(y<ly) {
		    		ly =y;
		    		lx = x;
		    		p = points[i];
		    	}
		    	else if(y==ly && x<lx) {
		    		lx = x;
		    		p = points[i];
		    	}
		    }
		    for(int i=0; i<N; i++) {
		    	points[i].setPivot(p);
		    }
		    Arrays.sort(points);
		    for(int i=0; i<points.length; i++)
		    {
		    	System.out.println(points[i].x+" "+points[i].y);
		    }
		    Stack stack = new Stack();
		    stack.push(points[0]);
		    stack.push(points[1]);
		    for(int i=2; i<points.length; i++) {
		    	Point top = (Point)stack.pop();
		    	Point second = (Point)stack.pop();
		        int o = Cross_product(second, top, points[i]);
		        stack.push(second);
		        stack.push(top);
		        if(o == 0) {
		        	stack.pop();
		        	stack.push(points[i]);
		        }
		        else if (o > 0) {
		        	stack.push(points[i]);
		        }
		        else {
		                while (o <= 0 && stack.size() > 2) {
		                        stack.pop();
		                        top = (Point)stack.pop();
		        		    	second = (Point)stack.pop();
		                        o = Cross_product(second, top, points[i]);
		                        stack.push(second);
		        		        stack.push(top);
		                }
		                stack.push(points[i]);
		        }
		    }
		    System.out.println("Done");
			while(stack.size()>0) {
				Point pp = (Point)stack.pop();
				System.out.println(pp.x+" "+pp.y);
			}
		    //out.println(ans);                        
		    out.close();                                 
		    System.exit(0); 
	 }
	 
	 private static int Cross_product(Point p1, Point p2, Point p3) {
		 return (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y);	
	 }
}

graph traversal

// Graph traversal

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/*
ID: kanishk1
LANG: JAVA
TASK: skate
*/

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

public class skate {
	 static char [][]arr;
	 static ArrayList list = new ArrayList();
	 static PrintWriter out;
	 public static void main (String [] args) throws IOException {
	    // Use BufferedReader rather than RandomAccessFile; it's much faster
	    BufferedReader f = new BufferedReader(new FileReader("skate.in"));
	                                                  // input file name goes above
	    out = new PrintWriter(new BufferedWriter(new FileWriter("skate.out")));
	    // Use StringTokenizer vs. readLine/split -- lots faster
	    StringTokenizer st = new StringTokenizer(f.readLine());
	    int R = Integer.parseInt(st.nextToken());   
	    int C = Integer.parseInt(st.nextToken());   
	    //System.out.println(R+" "+C);
	    arr = new char[R][C];
	    for(int i=0; i<R; i++) {
	    	arr[i] = f.readLine().trim().toCharArray();
	    }
	    list.add(pair(0,0));
	    doTask(0, 0);
	  }		  
	 
	 private static void doTask(int i, int j) {
		 //System.out.println(pair(i,j));
		 if(i==arr.length-1 && j==arr[0].length-1) {
			 //System.out.println("here"); 
			 out.println(printAnswer(list));                        
			 out.close();                                 
			 System.exit(0); 
		 }
		 if(isValid(i+1, j)) {
			 list.add(pair(i+1,j));
			 arr[i+1][j] = '@';
			 doTask(i+1,j);
			 arr[i+1][j] = '.';
			 list.remove(pair(i+1,j));
		 }
		 if(isValid(i-1, j)) {
			 list.add(pair(i-1,j));
			 arr[i-1][j] = '@';
			 doTask(i-1,j);
			 arr[i-1][j] = '.';
			 list.remove(pair(i-1,j));
		 }
		 if(isValid(i, j+1)) {
			 arr[i][j+1] = '@';
			 list.add(pair(i,j+1));
			 doTask(i,j+1);
			 arr[i][j+1] = '.';
			 list.remove(pair(i,j+1));
		 }
		 if(isValid(i, j-1)) {
			 arr[i][j-1] = '@';
			 list.add(pair(i,j-1));
			 doTask(i,j-1);
			 arr[i][j-1] = '.';
			 list.remove(pair(i,j-1));
		 }
		 return;
	 }
	 
	 private static boolean isValid(int i, int j) {
		 if(i<0 || j<0 || i>=arr.length || j>=arr[0].length) return false;
		 if(arr[i][j]!='.') return false;
		 return true;
	 }
	 
	 private static String pair(int i, int j) {
		 return (i+1)+" "+(j+1);
	 }
	 
	 private static String printAnswer(ArrayList list) {
		 String ans = "";
		 for(int i=0; i<list.size(); i++) {
			 ans+=list.get(i);
			 if(i!=list.size()-1) ans+="\n";
		 }
		 return ans;
	 }
}

PHP GET and POST Variables

// The following code will easily retrieve all of the GET and POST data for you and load it into appropriately named PHP variables. The same code will also work to get parameters added to the end of URLs via other methods other than using GET with a form.

$q = explode("&",$_SERVER["QUERY_STRING"]);
foreach ($q as $qi)
{
  if ($qi != "")
  {
    $qa = explode("=",$qi);
    list ($key, $val) = $qa;
    if ($val)
      $$key = urldecode($val);
  }
}
 
reset ($_POST);
while (list ($key, $val) = each ($_POST))
{
  if ($val)
    $$key = $val;
}

isPrime

// finding whether 'n' is prime or not

private boolean isPrime (int n)
{
   if (n<=1) return false;
   if (n==2) return true;
   if (n%2==0) return false;
   int m=(int)Math.round(Math.sqrt(n));

   for (int i=3; i<=m; i+=2)
      if (n%i==0)
         return false;

   return true;
}

GCD of two numbers.

// finds GCD of a and b using Euclidian algorithm

public int GCD(int a, int b)
{
   if (b==0) return a;
   return GCD(b,a%b);
}
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS