Search ProofOfProgress Blog

Sunday, March 1, 2015

CodeEval ReadMore: Hardest Easy Problem Ever

I am going to save you the headache of this easy problem that took me about 4 hours to solve:

Here is the description:

READ MORE

CHALLENGE DESCRIPTION:

You are given a text. Write a program which outputs its lines according to the following rules:
  1. If line length is ≤ 55 characters, print it without any changes.
  2. If the line length is > 55 characters, change it as follows:
    1. Trim the line to 40 characters.
    2. If there are spaces ‘ ’ in the resulting string, trim it once again to the last space (the space should be trimmed too).
    3. Add a string ‘... ’ to the end of the resulting string and print it.
Trim did not mean what I thought it meant. To me, trimming is removing white spaces.
Trimming to 40 characters to me would mean... line.substring(0,40); //(JAVA)

I was really confused at why non of the read-more example output stopped in the middle of a word.
Because of a bad example, I couldn't figure out if that was co-incidental or by design.

Example: With the input:
123456789 123456789 123456789 123 Supercalifragilisticexpialidocious
I was pretty sure due to the instructions, the proper output would be something like this:
123456789 123456789 123456789 123 Superc... Read More
But the correct answer is more like:
123456789 123456789 123456789 123   //Notice the absence of the sliced in half "Superc"

 So... What they are actually asking you to do is:
If the line is > 55 characters:
1. Tokenize the line using " " as a delimiter.
2. Build the string back up, stopping before you go over 40 characters.
3. If you get a zero-length string this way, just do line.substring.split(0,40);

I hope this helps someone.
Judging by everything I read on the internet.
I seem to be the only one who couldn't make sense of the instructions.
I really hate problems where the majority of why I can't solve it is because I don't know
what it is asking.

Today definitely re-affirms my grandfather's saying:
"A problem well stated is a problem half solved."

3 comments:

  1. Thank you for this. I was going insane trying to figure this one out. I kept getting 95% due to the bad directions. I wanted you to know you are not the only one who read it that way.

    ReplyDelete
  2. Same here. Been doing this for almost 2 hours and COULD NOT figure out what I was doing wrong.

    ReplyDelete
  3. Anyone know how to report a badly worded challenge? Cause yeah, you guys are right. FYI - found a java answer that someone can use as reference:

    https://github.com/mirandaio/codeeval/blob/master/easy/ReadMore/Main.java

    ReplyDelete