Jumped back on Topcoder.com and did a practice question this evening. I haven’t done any of these competitions since my college days. I forgot how much I loved working on these small little problems. I’m going to make an effort to work my way through these problems to get my brain back in gear for my upcoming adventures. I can’t reprint the question on my blag but the jist of it was I had to return one of three different interger values based on the number of words and chars in a given string.
Here is my tested and accepted C# solution (I got 210/250 for it) it’s a pretty direct solution for this problem.
public class HowEasy
{
public int pointVal(string problemStatement)
{
int totalchars = 0;
int totalwords = 0;
int currentword = 0;
for( int i = 0; i < problemStatement.Length; ++i )
{
int ccode = (int)problemStatement[i];
if( (ccode >= 65 && ccode <= 90) || (ccode >= 97 && ccode <= 122) ) //a-z A-Z
{
totalchars++;
currentword++;
}
if( ccode == 32 ) //space
{
if( currentword >= 1 )
{
totalwords++;
}
currentword = 0;
}
}
if( totalwords == 0 || totalchars == 0 )
{
return 250;
}
int result = totalchars / totalwords;
if( result <= 3 )
{
return 250;
}
else if( result >= 4 && result <= 5 )
{
return 500;
}
else
{
return 1000;
}
}
}


