Friday, September 25, 2009

HowEasy - Topcoder Arena

Problem Statement :
  
***Note: Please keep programs under 7000 characters in length. Thank you

Class Name: HowEasy
Method Name: pointVal
Parameters: String
Returns: int

TopCoder has decided to automate the process of assigning problem difficulty
levels to problems. TopCoder developers have concluded that problem difficulty
is related only to the Average Word Length of Words in the problem statement:

If the Average Word Length is less than or equal to 3, the problem is a 250
point problem.
If the Average Word Length is equal to 4 or 5, the problem is a 500 point
problem.
If the Average Word Length is greater than or equal to 6, the problem is a 1000
point problem.

Definitions:
Token - a set of characters bound on either side by spaces, the beginning of
the input String parameter or the end of the input String parameter.
Word - a Token that contains only letters (a-z or A-Z) and may end with a
single period. A Word must have at least one letter.
Word Length - the number of letters in a Word. (NOTE: a period is NOT a letter)

The following are Words :
"ab", "ab."

The following are not Words :
"ab..", "a.b", ".ab", "a.b.", "a2b.", "."

Average Word Length - the sum of the Word Lengths of every Word in the problem
statement divided by the number of Words in the problem statement. The
division is integer division. If the number of Words is 0, the Average Word
Length is 0.

Implement a class HowEasy, which contains a method pointVal. The method takes
a String as a parameter that is the problem statement and returns an int that
is the point value of the problem (250, 500, or 1000). The problem statement
should be processed from left to right.

Here is the method signature (be sure your method is public):
int pointVal(String problemStatement);

problemStatement is a String containing between 1 and 50 letters, numbers,
spaces, or periods. TopCoder will ensure the input is valid.

Examples:

If problemStatement="This is a problem statement", the Average Word Length is
23/5=4, so the method should return 500.
If problemStatement="523hi.", there are no Words, so the Average Word Length is
0, and the method should return 250.
If problemStatement="Implement a class H5 which contains some method." the
Average Word Length is 38/7=5 and the method should return 500.
If problemStatement=" no9 . wor7ds he8re. hj.." the Average Word Length is 0,
and the method should return 250.
Definition
  
Class: HowEasy
Method: pointVal
Parameters: string
Returns: int
Method signature: int pointVal(string param0)
(be sure your method is public)
 
 
#include <iostream>
#include <sstream>
#include <string>

const static 
std::string g_LegalCharacters =  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

class HowEasy
{
    public:
        int pointVal(std::string problemStmt);

    private:
        inline bool isValid(std::string &token);
        inline int problemValue (unsigned int length);
};

bool HowEasy::isValid(std::string &token)
{
    if (token.empty()) return 0;

    size_t pos = token.find_first_not_of(g_LegalCharacters);
    return (pos == std::string::npos);
}

int HowEasy::pointVal(std::string problemStmt)
{
    std::istringstream iss;
    iss.str( problemStmt );

    std::string token;
    unsigned int nWords = 0;
    unsigned int lWords = 0;
    unsigned int average = 0;
    int points = 0;
    std::string validString;
    while (!iss.eof())
    {
        iss >> token;
        if (token.length())
        {
            if (token[token.length() - 1] == '.')
                token.erase(token.end() - 1);

            if (isValid(token))
            {
                ++nWords;
                lWords += token.length();
                validString += token;
            }
        }
    }
    
    if (lWords && nWords)
        average = lWords / nWords;
        
    points =  problemValue (average);        
    return points;    
}

int HowEasy::problemValue (unsigned int length)
{
    if (length <= 3)
        return 250;
    else if (length <= 5)
        return 500;
    else
        return 1000;
}



Wednesday, September 09, 2009

Compare Vector V1 and V2 for equality

Here are the ways to compare two vectors.

Method 1:
 
#include < iostream >
using std::cout;
using std::endl;

#include < algorithm >
#include < vector >
#include < iterator >

int main()
{
   int a1[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   int a2[ 10 ] = { 1, 2, 3, 4, 9, 6, 7, 8, 9, 10 };
   std::vector<> v1( a1, a1 + 10 );
   std::vector<> v2( a1, a1 + 10 );
   std::vector<> v3( a2, a2 + 10 );
   std::ostream_iterator<> output( cout, " " );

   cout << "Vector v1 contains: ";    
   std::copy( v1.begin(), v1.end(), output );    
   cout << "\nVector v2 contains: ";
   std::copy( v2.begin(), v2.end(), output );
   cout << "\nVector v3 contains: ";
   std::copy( v3.begin(), v3.end(), output );

   // compare vectors v1 and v2 for equality    
   bool result = std::equal( v1.begin(), v1.end(), v2.begin() );    
   cout << "\n\nVector v1 " 
        << ( result ? "is" : "is not" )
        << " equal to vector v2.\n"
        return 0;
}


Method 2:

Overload the == operator for comparision

 

inline bool operator == (const std::vector< std::string >, 
                         const std::vector< std::string >)
{
    if (lhs.size() != rhs.size())
        return false;

    int count = lhs.size();
    std::vector< std::string >::const_iterator lhsi = lhs.begin();
    std::vector< std::string >::const_iterator rhsi = rhs.begin();
    while (count--)
    {
        if ( *lhsi != *rhsi)
            return false;
        lhsi++; rhsi++;
    }
    return true;
}