12/30/10

12/28/10

Parent-Child Communication Using Pipes in UNIX

This is the simplest of program that you can get.

Here the parent-child communicates over a pipe.

NOTE:
if the child wants to communicate to the parent then it will have to closes(fd[0]) and parent will closes(fd[1]).

this will only work UBUNTU, LINUX(RedHat) , Solaris, FreeBDS etc all UNIX VERSIONS. DONT TRY to use it on any compiler like gcc,turbo c, etc...

Send data from Parent to Child over a pipe:


12/27/10

12/20/10

Codeforces #47, Problem E

1:  import java.io.BufferedReader;  
2:  import java.io.IOException;  
3:  import java.io.InputStreamReader;  
4:  import java.util.HashMap;  
5:  public class Main {  
6:       public static void main(String[] args) throws IOException {  
7:            BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));  
8:            String ans=bf.readLine();  
9:            String var[]=ans.split(" ");  
10:            HashMap<Integer, Double> val=new HashMap<Integer, Double>();  
11:            double dx=0;  
12:            int cnt=0;  
13:            double rt1=0;  
14:            double rt2=0;  
15:            val.put(null, (double)1);  
16:            val.put(null, (double) 2);  
17:            for(double i=1;i<=Integer.parseInt(var[0]);i++){  
18:                 for(double j=1;j<=Integer.parseInt(var[1]);j++){  
19:                      dx= Math.pow(2*i, 2)-(4*j);  
20:                      if(dx>0){  
21:                           rt1= (((-2*i)+ Math.sqrt(dx)))/2;  
22:                           rt2= (((-2*i) - Math.sqrt(dx)))/2;  
23:                           if(val.get(cnt)==null){  
24:                                cnt=cnt+1;  
25:                                val.put(cnt,(double)rt2);  
26:                           }  
27:                           if(val.get(cnt)==null){  
28:                                cnt=cnt+1;  
29:                                val.put(cnt,(double)rt1);  
30:                           }  
31:                           if(val.containsValue(rt1) &&val.containsValue(rt2)){  
32:                           }else if(val.containsValue(rt1)){  
33:                                cnt=cnt+1;  
34:                                val.put(cnt,(double)rt2);  
35:                           }else if(val.containsValue(rt2)){  
36:                                cnt=cnt+1;  
37:                                val.put(cnt,(double)rt1);  
38:                           }else{  
39:                                cnt=cnt+2;  
40:                           val.put(cnt,(double)rt1);  
41:                           val.put(cnt,(double)rt2);  
42:                           }  
43:                      }else if(dx==0){  
44:                           rt1=(double) ((-2*i)/2);  
45:                           if(val.get(1)==null){  
46:                                cnt=cnt+1;  
47:                                val.put(cnt,(double) rt1);       
48:                           }  
49:                           if(val.containsValue(rt1)){   
50:                           }else{  
51:                                cnt=cnt+1;  
52:                                val.put(cnt,(double)rt1);  
53:                           }  
54:                      }  
55:                 }  
56:            }  
57:            System.out.println(cnt);  
58:       }  
59:  }  

12/19/10

HashMap simple usage and understanding

Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated.
The HashMap class uses a hash table to implementation of the map interface. This allows the execution time of basic operations, such as get() and put() to be constant.

An example is below:


Q. Find the numbers Divisible by an array of numbers ie div[] between a range ie from 1 to N?
Ans.  This can be solved using many ways, but we will be solving using HashMap which will help u understand the concept of HashMap in a better way..

NOTE:
For this u can use your command prompt, eclipse, netbeans or any IDE that supports JAVA. Using eclipse is much easier.


CODE :


1:  import java.util.HashMap; 

2:  public class Main { 

3:    public static int usingHashmap(int N,int div[]){ 

4:      int count=0; 

5:      HashMap<Integer, Integer> key=new HashMap<Integer, Integer>(); 

6:      for(int i=1;i<N;i++){ 

7:        key.put(i, i); 

8:      } 

9:      for(int i=0;i<div.length;i++){ 

10:        for(int k=1;k<N;k++){ 

11:        if(key.get(k)==null){ 

12:          key.put(k, -1); 

13:        } 

14:          if(key.get(k)%div[i]==0){ 

15:            count++; 

16:            key.remove(k); 

17:          } 

18:        } 

19:      } 

20:      System.out.println(count); 

21:      return count; 

22:    } 

23:    public static void main(String[] args) { 

24:      new Main().usingHashmap(50, new int[]{3,2}); 

25:    } 

26:  } 

12/4/10

Detecting a Cookie

Here is a piece of code that can help people detect cookies. Cookies play an important part in the website.

*****************#####*****************
JS Code:
window.onload = showCookies;

function showCookies() {
    var outMsg = "";

    if (document.cookie == "") {
        outMsg = "There are no cookies here";
    }
    else {
        var thisCookie = document.cookie.split("; ");
        for (var i=0; i<thisCookie.length; i++) {
            outMsg += "Cookie name is '" + thisCookie[i].split("=")[0];
            outMsg += "', and the value is '" + thisCookie[i].split("=")[1] + "'<br />";
        }
    }
    document.getElementById("ELEMENT ID").innerHTML = outMsg;
}

*****************#####*****************