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.
Here's the program.
import java.io.*;
public class HowEasy
{
public int pointVal(String inputstring)
{
int i, no_of_words=0, len_of_word=0, temp_len=0;
for(i=0;i
if((inputstring.charAt(i)>64&inputstring.charAt(i)<91)||(inputstring.charat(i)>96&inputstring.charAt(i)<123))
{
if(i
temp_len+=1;
continue;
}
else
{
temp_len+=1;
no_of_words+=1;
len_of_word=len_of_word+temp_len;
}
}
if(inputstring.charAt(i)>47&inputstring.charAt(i)<58)
{
while(i
temp_len=0;
continue;
}
if(inputstring.charAt(i)=='.')
{
if(i!=0&&i!=inputstring.length()-1)
{
if(inputstring.charAt(i-1)!=' '&&inputstring.charAt(i+1)==' ')
{
no_of_words+=1;
len_of_word=len_of_word+temp_len;
temp_len=0;
}
else
{
while(i
temp_len=0;
continue;
}
}
else if(i==0)
{
while(i
temp_len=0;
continue;
}
else
{
if(inputstring.charAt(i-1)!=' ')
{
no_of_words+=1;
len_of_word=len_of_word+temp_len;
temp_len=0;
}
}
}
if(inputstring.charAt(i)==' ')
{
if(i!=0)
{
no_of_words+=1;
len_of_word=len_of_word+temp_len;
temp_len=0;
}
}
}
if(no_of_words==0)
return 250;
else
{
if(len_of_word/no_of_words<=3)
return 250;
else if(len_of_word/no_of_words==4||len_of_word/no_of_words==5)
return 500;
else
return 1000;
}
}
public static void main(String args[])
{
int difficulty;
try
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String inputstring= bf.readLine();
HowEasy instan=new HowEasy();
difficulty=instan.pointVal(inputstring);
System.out.println("the difficulty level is "+difficulty);
}
catch(IOException e)
{
System.out.println("the exception has been catched");
}
}
}
No comments:
Post a Comment