Monday, October 29, 2007

Here is the solution to question no 1 of MGPT 2007....

MGPT 2007 question no 1 :

smaller code (75 lines):
This code I developed after the MGPT............
I subitted the second question in MGPT....
There is another post containing that code......

import ncst.pgdst.*;
class Router{
String rule[][]=new String[20][7];
int row;
static SimpleInput in=new SimpleInput();
void input()throws IOException{
row=in.readInt();
for(int i=0;i rule[i][0]=in.readWord();
for(int j=1;j<6;j++)
rule[i][j]=in.readWord();
if( ! rule[i][0].equals("FORWARD"))
rule[i][6]="XXXXXXXXXXXXXXXXXXXXXXX";
else
rule[i][6]=in.readWord()+" "+in.readWord();
}
sort();
}
void sort(){
int index=-1;
String rule1[][]=new String[20][7];
for(int i=0;i if(rule[i][0].equals("REJECT"))
rule1[++index]=rule[i];
for(int i=0;i if(rule[i][0].equals("ACCEPT"))
rule1[++index]=rule[i];
for(int i=0;i if(rule[i][0].equals("FORWARD"))
rule1[++index]=rule[i];
rule=rule1;
}
String respond(String ip){
int octet[]=new int[6];
int index=ip.indexOf(".");
octet[0]=Integer.parseInt(ip.substring(0,ip.indexOf(".")));
ip=ip.substring(index+1);
octet[1]=Integer.parseInt(ip.substring(0,ip.indexOf(".")));
ip=ip.substring(index+1);
octet[2]=Integer.parseInt(ip.substring(0,ip.indexOf(".")));
ip=ip.substring(index+1);
octet[3]=Integer.parseInt(ip.substring(0,ip.indexOf(" ")));
octet[4]=Integer.parseInt(ip.substring(ip.indexOf(" ")+1));
int pos=-1;
boolean acpt,reje,fwd;
for(int i=0;i boolean go[]=new boolean[5];
for(int j=0;j<5;j++)
if(rule[i][j+1].equals("*"))
go[j]=true;
else if(rule[i][j+1].indexOf("-") != -1){
int f=Integer.parseInt(rule[i][j+1].substring(0,rule[i][j+1].indexOf("-")));
int s=Integer.parseInt(rule[i][j+1].substring(rule[i][j+1].indexOf("-")+1));
if(octet[j]>=f && octet[j]<=s)
go[j]=true;
}else if(octet[j]==Integer.parseInt(rule[i][j+1]))
go[j]=true;
if(go[0]==true && go[1]==true && go[2]==true && go[3]==true && go[4]==true){
if(rule[i][0].equals("REJECT")) return "REJECT";
else if(rule[i][0].equals("ACCEPT")) return "ACCEPT";
else if(rule[i][0].equals("FORWARD")) return rule[i][6];
}
}
return "REJECT";
}
public static void main(String[]args)throws IOException{
Router router=new Router();
router.input();
int n=in.readInt();
for(int i=0;i String ip=in.readWord()+" "+in.readWord();
System.out.println(router.respond(ip));
}
}
}



If you are unable to see the program in a proper format, please click this link
http://chandra-shekhar.spaces.live.com/default.aspx

2 comments:

Mohit said...

hi
i am student of b.e.
sir if you have Self assessment Assignments and MGPT problems of NCST-banglore than please mail me at
mehtamohit2010@gmail.com

Anonymous said...

hi all chandra-shekhar.blogspot.com owner found your blog via yahoo but it was hard to find and I see you could have more visitors because there are not so many comments yet. I have discovered site which offer to dramatically increase traffic to your website http://mass-backlinks.com they claim they managed to get close to 4000 visitors/day using their services you could also get lot more targeted traffic from search engines as you have now. I used their services and got significantly more visitors to my website. Hope this helps :) They offer most cost effective services to increase website traffic at this website http://mass-backlinks.com