<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Kanishkkunal's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 28 Aug 2008 11:12:52 GMT</pubDate>
    <description>DZone Snippets: Kanishkkunal's Code Snippets</description>
    <item>
      <title>Algortihm Challenge 1: No division!</title>
      <link>http://snippets.dzone.com/posts/show/4226</link>
      <description>// http://gurus.wordpress.com/2007/06/28/algortihm-challenge-1-no-division/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;P[0] = 1;&lt;br /&gt;for (int i = 1; i &lt; N; i++)&lt;br /&gt;{&lt;br /&gt;	P[i]=P[i-1]*X[i-1];&lt;br /&gt;}&lt;br /&gt;Q[N-1] = 1;&lt;br /&gt;for (int i = N-2; i &gt;= 1; i--)&lt;br /&gt;{&lt;br /&gt;	Q[i]=Q[i+1]*X[i+1];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &lt; N; i++)&lt;br /&gt;{&lt;br /&gt;	M[i]=P[i]*Q[i];&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Fri, 29 Jun 2007 09:58:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4226</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>Double Array Manipulations</title>
      <link>http://snippets.dzone.com/posts/show/2894</link>
      <description>// Manipulations with double dimension arrays&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//equate two arrays&lt;br /&gt;private static boolean isEqual(char [][]arr1, char [][]arr2) {&lt;br /&gt;		if(arr1.length!=arr2.length) return false;&lt;br /&gt;		boolean res = true;&lt;br /&gt;		for(int i=0; i&lt;arr1.length; i++) {&lt;br /&gt;			res = res &amp; Arrays.equals(arr1[i], arr2[i]);&lt;br /&gt;		}&lt;br /&gt;		return res;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;//rotate clockwise 90 degrees&lt;br /&gt;private static char [][]rotate90(char [][]arr) {&lt;br /&gt;	char [][]ret = new char[arr.length][arr[0].length];&lt;br /&gt;	for(int j=0; j&lt;arr[0].length; j++) {&lt;br /&gt;		for(int i=0; i&lt;arr.length; i++) {&lt;br /&gt;			ret[i][j] = arr[j][i];&lt;br /&gt;		}&lt;br /&gt;	}		&lt;br /&gt;	//print(ret);&lt;br /&gt;	return reflect(ret);&lt;br /&gt;}&lt;br /&gt;	&lt;br /&gt;//reflect horizontally	&lt;br /&gt;private static char [][]reflect(char [][]arr) {&lt;br /&gt;	char [][]ret = new char[arr.length][arr[0].length];&lt;br /&gt;	for(int i=0; i&lt;arr.length; i++) {&lt;br /&gt;		for(int j=0; j&lt;arr[0].length; j++) {&lt;br /&gt;			ret[i][j] = arr[i][arr[i].length-j-1];&lt;br /&gt;		}&lt;br /&gt;	}		&lt;br /&gt;	//print(ret);&lt;br /&gt;	return ret;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//prints for debugging	&lt;br /&gt;private static void print(char [][]arr) {&lt;br /&gt;	for(int i=0; i&lt;arr.length; i++) {&lt;br /&gt;		System.out.println(Arrays.toString(arr[i]));&lt;br /&gt;	}&lt;br /&gt;	System.out.println();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 Oct 2006 02:48:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2894</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>graham scan</title>
      <link>http://snippets.dzone.com/posts/show/2838</link>
      <description>// graham scan incomplete&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.BufferedWriter;&lt;br /&gt;import java.io.FileReader;&lt;br /&gt;import java.io.FileWriter;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;import java.util.Arrays;&lt;br /&gt;import java.util.StringTokenizer;&lt;br /&gt;import java.util.Stack;&lt;br /&gt;/*&lt;br /&gt;ID: kanishk1&lt;br /&gt;LANG: JAVA&lt;br /&gt;TASK: moat&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public class moat {&lt;br /&gt;	static class Point implements Comparable {&lt;br /&gt;		public int x; public int y;&lt;br /&gt;		Point pv;&lt;br /&gt;		public Point(int x, int y) {&lt;br /&gt;			this.x = x;&lt;br /&gt;			this.y = y;&lt;br /&gt;		}&lt;br /&gt;		public void setPivot(Point pv) {&lt;br /&gt;			this.pv = pv;&lt;br /&gt;		}&lt;br /&gt;		public int compareTo(Object o) {&lt;br /&gt;			Point p = (Point)o;&lt;br /&gt;			double a1 = ((y-pv.y)*1.0)/(x-pv.x);&lt;br /&gt;			if(a1&lt;0) a1 = 2+a1;&lt;br /&gt;			double a2 = ((p.y-pv.y)*1.0)/(p.x-pv.x);&lt;br /&gt;			if(a2&lt;0) a2 = 2+a2;&lt;br /&gt;			if(a1&gt;a2) return 1;&lt;br /&gt;			else if(a1&lt;a2) return -1;&lt;br /&gt;			else return y-p.y;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	static Point []points;&lt;br /&gt;	 public static void main (String [] args) throws IOException {&lt;br /&gt;		    &lt;br /&gt;		 // Use BufferedReader rather than RandomAccessFile; it's much faster&lt;br /&gt;		    BufferedReader f = new BufferedReader(new FileReader("moat.in"));&lt;br /&gt;		                                                  // input file name goes above&lt;br /&gt;		    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("moat.out")));&lt;br /&gt;&lt;br /&gt;		    int N = Integer.parseInt(f.readLine());&lt;br /&gt;		    points = new Point[N];&lt;br /&gt;		    int lx = Integer.MAX_VALUE, ly = Integer.MAX_VALUE;&lt;br /&gt;		    Point p = null;&lt;br /&gt;		    for(int i=0; i&lt;N; i++) {&lt;br /&gt;		    	StringTokenizer st = new StringTokenizer(f.readLine());&lt;br /&gt;		    	int x = Integer.parseInt(st.nextToken());&lt;br /&gt;		    	int y = Integer.parseInt(st.nextToken());&lt;br /&gt;		    	&lt;br /&gt;		    	points[i] = new Point(x,y);&lt;br /&gt;		    	if(y&lt;ly) {&lt;br /&gt;		    		ly =y;&lt;br /&gt;		    		lx = x;&lt;br /&gt;		    		p = points[i];&lt;br /&gt;		    	}&lt;br /&gt;		    	else if(y==ly &amp;&amp; x&lt;lx) {&lt;br /&gt;		    		lx = x;&lt;br /&gt;		    		p = points[i];&lt;br /&gt;		    	}&lt;br /&gt;		    }&lt;br /&gt;		    for(int i=0; i&lt;N; i++) {&lt;br /&gt;		    	points[i].setPivot(p);&lt;br /&gt;		    }&lt;br /&gt;		    Arrays.sort(points);&lt;br /&gt;		    for(int i=0; i&lt;points.length; i++)&lt;br /&gt;		    {&lt;br /&gt;		    	System.out.println(points[i].x+" "+points[i].y);&lt;br /&gt;		    }&lt;br /&gt;		    Stack stack = new Stack();&lt;br /&gt;		    stack.push(points[0]);&lt;br /&gt;		    stack.push(points[1]);&lt;br /&gt;		    for(int i=2; i&lt;points.length; i++) {&lt;br /&gt;		    	Point top = (Point)stack.pop();&lt;br /&gt;		    	Point second = (Point)stack.pop();&lt;br /&gt;		        int o = Cross_product(second, top, points[i]);&lt;br /&gt;		        stack.push(second);&lt;br /&gt;		        stack.push(top);&lt;br /&gt;		        if(o == 0) {&lt;br /&gt;		        	stack.pop();&lt;br /&gt;		        	stack.push(points[i]);&lt;br /&gt;		        }&lt;br /&gt;		        else if (o &gt; 0) {&lt;br /&gt;		        	stack.push(points[i]);&lt;br /&gt;		        }&lt;br /&gt;		        else {&lt;br /&gt;		                while (o &lt;= 0 &amp;&amp; stack.size() &gt; 2) {&lt;br /&gt;		                        stack.pop();&lt;br /&gt;		                        top = (Point)stack.pop();&lt;br /&gt;		        		    	second = (Point)stack.pop();&lt;br /&gt;		                        o = Cross_product(second, top, points[i]);&lt;br /&gt;		                        stack.push(second);&lt;br /&gt;		        		        stack.push(top);&lt;br /&gt;		                }&lt;br /&gt;		                stack.push(points[i]);&lt;br /&gt;		        }&lt;br /&gt;		    }&lt;br /&gt;		    System.out.println("Done");&lt;br /&gt;			while(stack.size()&gt;0) {&lt;br /&gt;				Point pp = (Point)stack.pop();&lt;br /&gt;				System.out.println(pp.x+" "+pp.y);&lt;br /&gt;			}&lt;br /&gt;		    //out.println(ans);                        &lt;br /&gt;		    out.close();                                 &lt;br /&gt;		    System.exit(0); &lt;br /&gt;	 }&lt;br /&gt;	 &lt;br /&gt;	 private static int Cross_product(Point p1, Point p2, Point p3) {&lt;br /&gt;		 return (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y);	&lt;br /&gt;	 }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Oct 2006 00:52:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2838</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>graph traversal</title>
      <link>http://snippets.dzone.com/posts/show/2837</link>
      <description>// Graph traversal&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.BufferedWriter;&lt;br /&gt;import java.io.FileReader;&lt;br /&gt;import java.io.FileWriter;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.PrintWriter;&lt;br /&gt;import java.util.StringTokenizer;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;ID: kanishk1&lt;br /&gt;LANG: JAVA&lt;br /&gt;TASK: skate&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;public class skate {&lt;br /&gt;	 static char [][]arr;&lt;br /&gt;	 static ArrayList list = new ArrayList();&lt;br /&gt;	 static PrintWriter out;&lt;br /&gt;	 public static void main (String [] args) throws IOException {&lt;br /&gt;	    // Use BufferedReader rather than RandomAccessFile; it's much faster&lt;br /&gt;	    BufferedReader f = new BufferedReader(new FileReader("skate.in"));&lt;br /&gt;	                                                  // input file name goes above&lt;br /&gt;	    out = new PrintWriter(new BufferedWriter(new FileWriter("skate.out")));&lt;br /&gt;	    // Use StringTokenizer vs. readLine/split -- lots faster&lt;br /&gt;	    StringTokenizer st = new StringTokenizer(f.readLine());&lt;br /&gt;	    int R = Integer.parseInt(st.nextToken());   &lt;br /&gt;	    int C = Integer.parseInt(st.nextToken());   &lt;br /&gt;	    //System.out.println(R+" "+C);&lt;br /&gt;	    arr = new char[R][C];&lt;br /&gt;	    for(int i=0; i&lt;R; i++) {&lt;br /&gt;	    	arr[i] = f.readLine().trim().toCharArray();&lt;br /&gt;	    }&lt;br /&gt;	    list.add(pair(0,0));&lt;br /&gt;	    doTask(0, 0);&lt;br /&gt;	  }		  &lt;br /&gt;	 &lt;br /&gt;	 private static void doTask(int i, int j) {&lt;br /&gt;		 //System.out.println(pair(i,j));&lt;br /&gt;		 if(i==arr.length-1 &amp;&amp; j==arr[0].length-1) {&lt;br /&gt;			 //System.out.println("here"); &lt;br /&gt;			 out.println(printAnswer(list));                        &lt;br /&gt;			 out.close();                                 &lt;br /&gt;			 System.exit(0); &lt;br /&gt;		 }&lt;br /&gt;		 if(isValid(i+1, j)) {&lt;br /&gt;			 list.add(pair(i+1,j));&lt;br /&gt;			 arr[i+1][j] = '@';&lt;br /&gt;			 doTask(i+1,j);&lt;br /&gt;			 arr[i+1][j] = '.';&lt;br /&gt;			 list.remove(pair(i+1,j));&lt;br /&gt;		 }&lt;br /&gt;		 if(isValid(i-1, j)) {&lt;br /&gt;			 list.add(pair(i-1,j));&lt;br /&gt;			 arr[i-1][j] = '@';&lt;br /&gt;			 doTask(i-1,j);&lt;br /&gt;			 arr[i-1][j] = '.';&lt;br /&gt;			 list.remove(pair(i-1,j));&lt;br /&gt;		 }&lt;br /&gt;		 if(isValid(i, j+1)) {&lt;br /&gt;			 arr[i][j+1] = '@';&lt;br /&gt;			 list.add(pair(i,j+1));&lt;br /&gt;			 doTask(i,j+1);&lt;br /&gt;			 arr[i][j+1] = '.';&lt;br /&gt;			 list.remove(pair(i,j+1));&lt;br /&gt;		 }&lt;br /&gt;		 if(isValid(i, j-1)) {&lt;br /&gt;			 arr[i][j-1] = '@';&lt;br /&gt;			 list.add(pair(i,j-1));&lt;br /&gt;			 doTask(i,j-1);&lt;br /&gt;			 arr[i][j-1] = '.';&lt;br /&gt;			 list.remove(pair(i,j-1));&lt;br /&gt;		 }&lt;br /&gt;		 return;&lt;br /&gt;	 }&lt;br /&gt;	 &lt;br /&gt;	 private static boolean isValid(int i, int j) {&lt;br /&gt;		 if(i&lt;0 || j&lt;0 || i&gt;=arr.length || j&gt;=arr[0].length) return false;&lt;br /&gt;		 if(arr[i][j]!='.') return false;&lt;br /&gt;		 return true;&lt;br /&gt;	 }&lt;br /&gt;	 &lt;br /&gt;	 private static String pair(int i, int j) {&lt;br /&gt;		 return (i+1)+" "+(j+1);&lt;br /&gt;	 }&lt;br /&gt;	 &lt;br /&gt;	 private static String printAnswer(ArrayList list) {&lt;br /&gt;		 String ans = "";&lt;br /&gt;		 for(int i=0; i&lt;list.size(); i++) {&lt;br /&gt;			 ans+=list.get(i);&lt;br /&gt;			 if(i!=list.size()-1) ans+="\n";&lt;br /&gt;		 }&lt;br /&gt;		 return ans;&lt;br /&gt;	 }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Oct 2006 00:50:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2837</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>PHP GET and POST Variables</title>
      <link>http://snippets.dzone.com/posts/show/2639</link>
      <description>// 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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$q = explode("&amp;",$_SERVER["QUERY_STRING"]);&lt;br /&gt;foreach ($q as $qi)&lt;br /&gt;{&lt;br /&gt;  if ($qi != "")&lt;br /&gt;  {&lt;br /&gt;    $qa = explode("=",$qi);&lt;br /&gt;    list ($key, $val) = $qa;&lt;br /&gt;    if ($val)&lt;br /&gt;      $$key = urldecode($val);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;reset ($_POST);&lt;br /&gt;while (list ($key, $val) = each ($_POST))&lt;br /&gt;{&lt;br /&gt;  if ($val)&lt;br /&gt;    $$key = $val;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Sep 2006 07:04:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2639</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>isPrime</title>
      <link>http://snippets.dzone.com/posts/show/2577</link>
      <description>// finding whether 'n' is prime or not&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private boolean isPrime (int n)&lt;br /&gt;{&lt;br /&gt;   if (n&lt;=1) return false;&lt;br /&gt;   if (n==2) return true;&lt;br /&gt;   if (n%2==0) return false;&lt;br /&gt;   int m=(int)Math.round(Math.sqrt(n));&lt;br /&gt;&lt;br /&gt;   for (int i=3; i&lt;=m; i+=2)&lt;br /&gt;      if (n%i==0)&lt;br /&gt;         return false;&lt;br /&gt;&lt;br /&gt;   return true;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 05:06:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2577</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
    <item>
      <title>GCD of two numbers.</title>
      <link>http://snippets.dzone.com/posts/show/2574</link>
      <description>// finds GCD of a and b using Euclidian algorithm&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public int GCD(int a, int b)&lt;br /&gt;{&lt;br /&gt;   if (b==0) return a;&lt;br /&gt;   return GCD(b,a%b);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 03:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2574</guid>
      <author>kanishkkunal (Kanishk Kunal)</author>
    </item>
  </channel>
</rss>
