Tuesday, September 29, 2009

SCALA programming language

While coding in SPOJ. I came across a new programming language named SCALA. I searched a hell lot of things about the language.

Scala goes out of it's way to make sure you don't need to program in a functional style. This is the main criticism of Scala from functional folks, as a matter of fact: some do not consider a language functional unless it forces the programmer to write in functional style.
Scala already introduces some nice features, comparable to some stuff that's present in C++ but not Java, though they work in different fashion. In that respect, once you realize what such features are for and relate them to C++ stuff, you'll be much ahead of Java programmers, as you'll already know what to do with them.

Some of the downsides of Scala are not related at all to the relative youth of the language. After all, Scala, has about 5 years of age, and Java was very different 5 years into its own lifespan.
In particular, because Scala does not have the backing of an enterprise which considers it a strategic priority, so the support resources for it are rather lacking. For example:

* Lack of extensive tutorials
* Inferior quality of the documentation
* Non-existing localization of documentation
* Native libraries (Scala uses Java or .Net libraries as base for their own)

* * * * *

Sunday, September 20, 2009

Optimization

During coding, what we need to keep in our minds is optimize the code. The direct code which hits our brain at the first instance is generally not that optimized. If given a large test case to examine the code, it fails to complete the execution in time. I came to know about this while playing online a coding event in which besides code to be right one's code should run in an asked period of time. For this we need to optimize our code a great extent.
I was asked to print all the prime numbers between given two numbers. The first code that comes to our mind is to run a loop and find all the prime numbers by dividing each number with every other number. Mind you, this takes a hell lot of time. One can optimize the code easily by discluding the numbers that get divided by 2,3,5. This takes away numbers out from the loop that prevent us from checking them. The time limit set hence becomes a little reachable.
The second thing is logic. One can even look at the problem at different angle. You can yourself Google and find that there are many other good methods of finding a prime number. One method needs the loop to run only for (n)^(1/2) times. This also reduces the time a great deal.

CODE IT.. OPTIMIZE IT!!

* * * * *

Friday, September 11, 2009

Back up your Gmail account



Gmail doesn't have to go down globally for our email access to be cut off—our internet could go down, or our account could even be disabled. Whatever the case, it's a good idea to have a backup plan just in case the worst should happen.

They've got us covered with a number of methods for backing up our Gmail, starting with the really obvious solution of just using Thunderbird to back up our email with POP access, but we can backup our email with Fetchmail on Windows, Getmail on Linux, the standalone Gmail Backup tool, or even use Google Groups to backup your Gmail account.

So, don't think much.. just get urself backed up!!

* * * * *

Thursday, September 3, 2009

Google Code Jam 2009

This was the first time I registered for Google Code Jam. Researching about this great event I came to know that it gives way to free use of languages. Google allows its participants to code in any language they wish to. Starting from c, c++, java, batch files to php, perl, brainf*ck and all.

It was the qualification round in which I scratched my 'hairless' head throughout the day. The round consisted of three questions. One had to submit an output file of solution of test cases. Each question consisted of two test cases small and large. 4 minutes of time was given to a participant to upload his source code file and the output file in small test cases. Time was increased to 8 mins in case of large test cases. It was necessary to solve at least one small test case and one large test case of any question to qualify for next round. I was able to solve a small one.. :)

The question I solved was called "Alien Language". One can easily 'google' the whole question. I m giving the source code which I submitted :

using namespace std;
int change(string strConvert)
{
int intReturn;
intReturn = atoi(strConvert.c_str());
return(intReturn);
}

void main()
{
ifstream myfile("g1.txt");
ofstream opens("g2.txt");
string line;
int cases=0;
char words[800][800],letters[800][800],answers[800][800];
int L,D,N,r=0,c=0,f=0,m;
getline(myfile,line,' ');
L=change(line);
getline(myfile,line,' ');
D=change(line);
getline(myfile,line,'\n');
N=change(line);
for(long int i=0;i++)
{
getline(myfile,line,'\n');
for(int j=0;j++)
{
words[i][j]=line[j];
}
words[i][L]='\0';
}
for(int q=0;q++)
{
cases=0;
r=0;
getline(myfile,line,'\n');
f=0;
m=line.size();
while(f!=m)
{
if(line[f]=='(')
{
while(line[f]!=')')
{
f++;
if(line[f]!=')')
{
letters[r][c]=line[f];
c++;
}
}
letters[r][c]=NULL;
f++;
r++;
c=0;
}
else
{
letters[r][c++]=line[f];
letters[r][c]=NULL;
r++;
c=0;
f++;
}
}
for(long int s=0;s++)
{
f=0;
for(long int k=0;k+)
{
for(long int a=0;a++)
{
if(words[s][k]==letters[k][a])
{
f++;
break;
}
}
}
if(f==L)
cases++;
}
opens<<"Case #"<<<": "<<<"\n";
}
getch();
}

Explanation of code:
What I did in the program is that I took each word of language into a double dimension array called 'words'. Each test case was then bifercated into letters in another double dimension array called 'letters'. The first letter in words was then regularly checked in the different letters of each test case. The moment it matched the loop got broken with an increment of a flag called 'f'. The moment flag became equal to length of word it was sure that the given combination exists, hence, the case variable gets incremented which is ultimately our answer and gets written in the output file.
The only problem that came with this solution was during the execution of large test case. It would have been easily implemented through vectors but lack of time compelled me to quit upon it. Actually, due to continuous memory allocation sometimes due to large datasets, the values get written on read only part of memory which generates errors.

So, I settled with a 10 point mark.. BTW my first experience with Google Code Jam was awesome..!!
* * * * *