Showing posts with label Programming. Show all posts

Calling PL/SQL procedures via JAVA  

Posted by Sourabh in ,

try {

con = DriverManager.getConnection("jdbc:odbc:dsn", "system", "hr");

cs=con.prepareCall("{call newteacherid(?,?,?)}");

cs.setString(1,teacherid.getText());
cs.setString(2,teacherid.getText());
cs.setInt(3,1);

cs.execute();
System.out.println("dfasfgasrg");
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"Invalid teacher ID","Error",JOptionPane.ERROR_MESSAGE );
Logger.getLogger(Administrator.class.getName()).log(Level.SEVERE, null, ex);
}

Full Screen Code in JAVA  

Posted by Sourabh in ,

import java.awt.*;

class ScreenRes {
public static void main(String args[]) {
new ScreenRes().doit();
}

public void doit() {
Frame frame = new Frame();
GraphicsDevice device;
device =
GraphicsEnvironment.
getLocalGraphicsEnvironment().
getDefaultScreenDevice();
if ( device.isFullScreenSupported() ) {
device.setFullScreenWindow(frame);
if (device.isDisplayChangeSupported()) {
device.setDisplayMode(
new DisplayMode( 0,0,
8, // bitDepth - 8 bits 256 colors
DisplayMode.REFRESH_RATE_UNKNOWN ));

}
else {
System.err.println("Change display mode not supported");
}
}
else {
System.err.println("Full screen not supported");
}
}
}

Sementica @ Pecfest 5  

Posted by Unknown in ,

As I said in one of the previous posts, Pecfest was not standardized. And this came out eminent this time. We came there for a programming contest after a standard prelim( Same old C++ trick questions, with some new lessons to learn, like printf("%%%%%"); a=(1,2,3,4,5); intellient use of i=050; j=0x50; with <<,>>,|,&, operators, recursive main and all). The expectations had risen for an intensive programming contest. We had been second in the prelims with a lots of mistakes, and a local 4th yr. team coming first, par again chalta hai. What came up is much different to expectations, not really bad but missed us a chance to have had another rivetting programming contest just the next day to the ICPC online.
Questions:
1. Syntax of printf: The return type was the trick.
2. Declare an array of pointers to function, that return a pointer to function returning a pointer to character.... Confused, that's ABC of code obfuscation. Mr. Vishnu, one of my seniors had taught this to us, though we had not taken it seriously as it seemed out of scope.
3. To find the sizeof() of int without the sizeof() operator: same old use of pointer++ or array elements, typecast to int and subtract.
4. We were to compart two function declarations, int func(int *arr[]); and int func(int *arr[3]);. we never expected a difference as nothing like this mattered in the protype and the pointer to be passed was to be the same type and rest depend on what we passed. I don't know ehether I was correct but what I tried and just said, seem to work.
5. Analog clock via Turbo C graphics--- who does it now- no one attempted this question, though the organizer was disappointed with that.
5. Given the width and length, egenrate a box of 0s and 1s and count maximum the number of 0s in the biggets rectangle you code make up- one proper coding question, but time had it on all teams as we managed the generator correct.


We ended up as the winners(I and Sourabh) just like th Google Doodle. Unlike google Doodle this efvent had a cash prize of Rs. 500, though that could just meet up our TA, food and all.

C++ int to string  

Posted by Unknown in

I took this from: http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/.
But this a great method to convert anything to string:
int i = 5;
std::string s;
std::stringstream out;
out << s =" out.str();" style="font-weight: bold;">

do not forget to include sstream

Reverse Gear Prelim Question Paper  

Posted by Unknown in ,

Reverse Gear Question Paper for the prelims in Aranya 2008.

reverse gear prelims



Answers:
1-b
2-c
3-c
4-d
5-b
6-a
7-b
8-e
9-b
10-c
11-b
12-e-0
13-a
14-a
15-d
16-a
17-d
18-c
19-d
20-a
21-a
22-d
23-c
24-e-5 20 1
25-b
26-d
27-a-2,3,7,8
28-d
29-d
30-c

Knight Coders Question Paper  

Posted by Unknown in ,

The question paper for Knight Coders Event Aranya 2008


A little batch and a lot of help  

Posted by Unknown in

Have you ever organized a contest. Sometimes it happens that while checking questions for the contest we prefer not to use the software like the pc2 or any contest s/w. Then the major problem is compiling. You cannot compile a file by typing its name line by line and doing the funny stuff around that. So here it is: use batch. All you have to do is copy the code to a file and rename it as something. batch. Drag and drop to see the output and you'll find your life easier:

a.in is the file containing the input. a.out contains the output.
Code: G++

del a.out
g++ %1 -o a.exe
a.exe,a.out
notepad a.out
Code GCC
del a.out
gcc -lm %1 -o a.exe
a.exea.out
notepad a.out

Sample Questions for Knight Coders  

Posted by Unknown in ,

Theses are the sample questions made as a booklet for those interested in the iKnight Coders and Knight Coders. A lot of planning was involved but it came out good.

Google Code Jam Answer 3  

Posted by Unknown in ,

This one was too good a question for me. i did something couldn't pass out. I have joined topcoder for practice - all people out there help me improve.....
Fly Swatter
Problem

What are your chances of hitting a fly with a tennis racquet?

To start with, ignore the racquet's handle. Assume the racquet is a perfect ring, of outer radius R and thickness t (so the inner radius of the ring is R−t).

The ring is covered with horizontal and vertical strings. Each string is a cylinder of radius r. Each string is a chord of the ring (a straight line connecting two points of the circle). There is a gap of length g between neighbouring strings. The strings are symmetric with respect to the center of the racquet i.e. there is a pair of strings whose centers meet at the center of the ring.

The fly is a sphere of radius f. Assume that the racquet is moving in a straight line perpendicular to the plane of the ring. Assume also that the fly's center is inside the outer radius of the racquet and is equally likely to be anywhere within that radius. Any overlap between the fly and the racquet (the ring or a string) counts as a hit.

Input

One line containing an integer N, the number of test cases in the input file.

The next N lines will each contain the numbers f, R, t, r and g separated by exactly one space. Also the numbers will have at most 6 digits after the decimal point.

Output

N lines, each of the form "Case #k: P", where k is the number of the test case and P is the probability of hitting the fly with a piece of the racquet.

Answers with a relative or absolute error of at most 10-6 will be considered correct.

Limits

f, R, t, r and g will be positive and smaller or equal to 10000.

t < style="font-weight:bold;">Nothing to speak man here is what rem wrote:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;

#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()

template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}

template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }

const double pi = 2*acos(0.0);
const double eps = 1e-9;

double asin2(double x) {
double res = asin(max(-1.0, min(1.0, x)));
assert(-pi/2 <= res && res <= pi/2);
return res;
}

double inters(double x1, double y1, double x2, double y2, double r) {
assert(x1 <= x2 && y1 <= y2);
assert(sqrt(sqr(x1)+sqr(y1)) <= r);
if (sqrt(sqr(x2)+sqr(y2)) <= r)
return (x2-x1)*(y2-y1);
vector<double> xs;
xs.push_back(x1);
xs.push_back(x2);
double x = sqrt(sqr(r)-sqr(y1));
if (y1 < r && x1 < x && x < x2)
xs.push_back(x);
x = sqrt(sqr(r)-sqr(y2));
if (y2 < r && x1 < x && x < x2)
xs.push_back(x);
if (x1 < r && r < x2)
xs.push_back(r);
sort(All(xs));
double res = 0;
Rep(i, Size(xs)-1) {
double x1 = xs[i], x2 = xs[i+1];
if (x1 > r-eps)
break;
double s1 = y1*(x2-x1), s2 = y2*(x2-x1);
double s4 = (0.5*x2*sqrt(sqr(r)-sqr(x2))+0.5*sqr(r)*asin2(x2/r))-(0.5*x1*sqrt(sqr(r)-sqr(x1))+0.5*sqr(r)*asin2(x1/r));
double s3 = -s4;
res += max(0.0, min(s2, s4)-max(s1, s3));
}
assert(res <= (x2-x1)*(y2-y1));
return res;
}

int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);

int t;
scanf("%d", &t);
For(test, 1, t) {
double f, R, t, r, g;
scanf("%lf%lf%lf%lf%lf", &f, &R, &t, &r, &g);
double sum = 0;
if (g > 2*f) {
double x = r;
while (x+f < R-t-f) {
double y = r;
while (sqrt(sqr(x+f)+sqr(y+f)) < R-t-f) {
sum += inters(x+f, y+f, x+g-f, y+g-f, R-t-f);
y += 2*r+g;
}
x += 2*r+g;
}
}
printf("Case #%d: %.6lf\n", test, (pi*sqr(R)-4*sum)/(pi*sqr(R)));
}

exit(0);
}



Everyone out there help me improve!!!

Google Code Jam Answer 2  

Posted by Unknown in ,

Again a bad answer by me. But was easy to write and correct in attempt 1:
Train Timetable
Problem

A train line has two stations on it, A and B. Trains can take trips from A to B or from B to A multiple times during a day. When a train arrives at B from A (or arrives at A from B), it needs a certain amount of time before it is ready to take the return journey - this is the turnaround time. For example, if a train arrives at 12:00 and the turnaround time is 0 minutes, it can leave immediately, at 12:00.

A train timetable specifies departure and arrival time of all trips between A and B. The train company needs to know how many trains have to start the day at A and B in order to make the timetable work: whenever a train is supposed to leave A or B, there must actually be one there ready to go. There are passing sections on the track, so trains don't necessarily arrive in the same order that they leave. Trains may not travel on trips that do not appear on the schedule.

Input

The first line of input gives the number of cases, N. N test cases follow.

Each case contains a number of lines. The first line is the turnaround time, T, in minutes. The next line has two numbers on it, NA and NB. NA is the number of trips from A to B, and NB is the number of trips from B to A. Then there are NA lines giving the details of the trips from A to B.

Each line contains two fields, giving the HH:MM departure and arrival time for that trip. The departure time for each trip will be earlier than the arrival time. All arrivals and departures occur on the same day. The trips may appear in any order - they are not necessarily sorted by time. The hour and minute values are both two digits, zero-padded, and are on a 24-hour clock (00:00 through 23:59).


After these NA lines, there are NB lines giving the departure and arrival times for the trips from B to A.


Output
For each test case, output one line containing "Case #x: " followed by the number of trains that must start at A and the number of trains that must start at B.

Limits

1 ≤ N ≤ 100

Small dataset

0 ≤ NA, NB ≤ 20

0 ≤ T ≤ 5

Large dataset

0 ≤ NA, NB ≤ 100

0 ≤ T ≤ 60

Sample
Input
2
5
3 2
09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
2
2 0
09:00 09:01
12:00 12:02

Output
Case #1: 2 2
Case #2: 2 0

My answer - agin bad but I got a hold of OOPS

// train.cpp : Defines the entry polong int for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
class train
{
public:
long int presentstation;
long int availableafter;
long int turnaround;
public:
void reachstation(long int station,long int time)
{
presentstation = station;
availableafter = time+turnaround;
}
void settrain(long int tt)
{
turnaround = tt;
}

};
class time
{
public:
long int start;
long int end;
long int station;
public:
void settime(long int Station,char* time)
{
station =Station;
start = (time[0] - '0') *600 + (time[1] -'0')*60 + (time[3] - '0')*10 + (time[4]-'0');
end = (time[6] - '0') *600 + (time[7] -'0')*60 + (time[9] - '0')*10 + (time[10]-'0');
}
};
int comp(const void *A,const void *B)
{
time *p = (time*) A;
time *q = (time*) B;
return (int) (p->start - q->start);
}
long int main()
{
long int no =0;
cin>>no;
for(long int counter = 1; counter<=no;counter++)
{
printf("Case #%d: ",counter);
long int turnaround = 0;
long int NA,NB;
cin>>turnaround;
cin>>NA>>NB;
char useless[7];
if(NA+NB != 0 )
{gets_s(useless);
}
time Time[200];
train Train[200];
long int NoTrains = 0;
char a[100];
for(long int i=0;i<NA; i++)
{
gets_s(a);
Time[i].settime(0,a);
strcpy_s(a,"");
}
for(long int i=NA;i<(NB+NA); i++)
{
gets_s(a);
Time[i].settime(1,a);
}
qsort(Time,NA+NB,sizeof(time),&comp);
//printf("%d\t%d\t%d",Time[0].start,Time[1].start,Time[2].start);
long int flag;
long int startA=0,startB=0;
for(long int i=0; i<NA+NB; i++)
{
flag=0;
for(long int j=0;j<NoTrains; j++)
{
if((Train[j].availableafter <= Time[i].start) && (Train[j].presentstation == Time[i].station))
{
Train[j].reachstation(1-Time[i].station,Time[i].end);
flag=1;
break;
}
}
if(flag==0)
{
Train[NoTrains].settrain(turnaround);
Train[NoTrains].reachstation(1-Time[i].station,Time[i].end);
NoTrains++;
if(Time[i].station == 0)
startA++;
else
startB++;
}
}
printf("%d %d\n",startA,startB);

}
return 0;

}


and the great answer by rem


#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,int> pii;

#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()

template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}

template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }

int readTime() {
int h, m;
scanf("%d:%d", &h, &m);
return m+60*h;
}

int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);

int t;
scanf("%d", &t);
For(test, 1, t) {
int t;
scanf("%d", &t);
int n1, n2;
scanf("%d%d", &n1, &n2);
vector<pii> t1(n1), t2(n2);
Rep(i, n1) {
t1[i].first = readTime();
t1[i].second = readTime();
}
Rep(i, n2) {
t2[i].first = readTime();
t2[i].second = readTime();
}
sort(All(t1));
sort(All(t2));
int r1 = 0, r2 = 0;
while (Size(t1) > 0 || Size(t2) > 0) {
int x;
if (Size(t2) > 0 && (Size(t1) == 0 || t2[0] < t1[0])) {
++r2;
x = t2[0].second;
t2.erase(t2.begin());
} else {
++r1;
x = -t;
}
for (;;) {
int i = 0;
while (i < Size(t1) && t1[i].first < x+t)
++i;
if (i == Size(t1))
break;
x = t1[i].second;
t1.erase(t1.begin()+i);
i = 0;
while (i < Size(t2) && t2[i].first < x+t)
++i;
if (i == Size(t2))
break;
x = t2[i].second;
t2.erase(t2.begin()+i);
}
}
printf("Case #%d: %d %d\n", test, r1, r2);
}

exit(0);
}

See how much I need to improve answer 3 in next post

Google code Jam : i am not a contest programmer  

Posted by Unknown in ,

I may be among the best programmers of my batch in my college but definitely I am not worth calling myself as a programmer. I am far from a standard contest programmer for my programs are still those written on college papers. Plus I am still new to the STl and the contest world. i have just had a few outings. But I plan to have massive changes to this state this year. I have started with the google code jam, a big programming contest popularised by google for programmer's all over the world. The contest had its qualifiers on thursday and indeed as said qualifiers are for only to sort out people who don't know what programming is, it came out. The first question was sort of easy and so was the second one( Even i could do that). But the third one was gud. Just not really as an article I write this posting to my blog to help me trace if i can improve as a programmaer this year. You can expect to see top coder contest codes to be here as well, for I just joined that.
Here is the link to the files Ques+Mine as well as ideal answers + Hope I see this next year:http://w14.easy-share.com/1700957331.html
I have converted the cpp to html. I have used a project at codeproject. The files are at:http://w14.easy-share.com/1700966549.html
So now I can paste the stuff here:
Saving the universe
Problem

The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding.

The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode!

To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they're received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.

Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.

Input

The first line of the input file contains the number of cases, N. N test cases follow.

Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.

The following line contains a number Q -- the number of incoming queries. The next Q lines will each contain a query. Each query will be the name of a search engine in the case.

Output

For each input case, you should output:
Case #X: Y
where X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.


Limits

0 <>Input
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol

Output
Case #1: 1
Case #2: 0

So here is what I managed: Not good but i need to keep a track of myself.



// savetheuniverse.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"
#include<iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int no =0;
cin>>no;
for(int counter = 1; counter<=no;counter++)
{
printf("Case #%d: ",counter);
int NoOfEngines = 0;
cin>>NoOfEngines;
char useless[7];
if(NoOfEngines != 0 )
gets_s(useless);
//cout<<NoOfEngines<<endl;
char Eng[1000][100];
int CalledEng[1000] = {0};
for (int Engine = 0;Engine<NoOfEngines;Engine++)
{
gets_s(Eng[Engine]);
//cout<<Eng[Engine]<<endl;
}
int NoOfSearches;
cin>>NoOfSearches;
//cout<<NoOfSearches<<endl;
if(NoOfSearches != 0 )
gets_s(useless);
char Sear[1000][100];
for(int Search = 0;Search <NoOfSearches; Search++)
{
gets_s(Sear[Search]);
//cout<<Sear[Search]<<endl;
}

int Present = 1;
int LastElementIndex=0;
while(1)
{

for(int i=LastElementIndex;i<NoOfSearches;i++)
{
for(int j=0;j<NoOfEngines;j++)
{
if(strcmp(Sear[i],Eng[j]) == 0)
{

if(CalledEng[j] != Present)
{
CalledEng[j] =Present;
LastElementIndex = i;
}
break;
}
}
}
//if(counter==8)
//printf("%d\t%d\n",Present,LastElementIndex);
int flag=0;
for(int i=0;i<NoOfEngines;i++)
{
if(CalledEng[i]<Present) {flag =1 ;}
}
if(flag==1) {break;}
Present++;
if(LastElementIndex >= NoOfSearches-1) break;
}
printf("%d\n",Present-1);
}
return 0;
}



And this was the ideal answer

#include <map>

#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <ctime>
using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;

#define _CRT_SECURE_NO_WARNINGS
#define For(i,a,b) for (int i(a),_b(b); i <= _b; ++i)
#define Ford(i,a,b) for (int i(a),_b(b); i >= _b; --i)
#define Rep(i,n) for (int i(0),_n(n); i < _n; ++i)
#define Repd(i,n) for (int i((n)-1); i >= 0; --i)
#define Fill(a,c) memset(&a, c, sizeof(a))
#define MP(x, y) make_pair((x), (y))
#define All(v) (v).begin(), (v).end()

template<typename T, typename S> T cast(S s) {
stringstream ss;
ss << s;
T res;
ss >> res;
return res;
}

template<typename T> inline T sqr(T a) { return a*a; }
template<typename T> inline int Size(const T& c) { return (int)c.size(); }
template<typename T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template<typename T> inline void checkMax(T& a, T b) { if (b > a) a = b; }

char buf[1024*1024];

int main() {
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);

gets(buf);
For(test, 1, atoi(buf)) {
int s, q;
map<string,int> num;
gets(buf);
s = atoi(buf);
Rep(i, s) {
gets(buf);
num[buf] = i;
}
gets(buf);
q = atoi(buf);
vi dp(s, 0);
Rep(i, q) {
gets(buf);
assert(num.count(buf) > 0);
int id = num[buf];
Rep(j, s)
if (j != id)
checkMin(dp[j], dp[id]+1);
dp[id] = 1000000;
}
printf("Case #%d: %d\n", test, *min_element(All(dp)));
}

exit(0);
}

I'll write answer 2 and 3 in a seperate post