wale

Let's have fun! Given one or more paragraphs, and your job is to create an clea …

Wed 20 Feb, 2019 06:42 pm
Let's have fun!



Given one or more paragraphs, and your job is to create an clean excerpt, subject to a maximum of 45 characters, including spaces. Then append an ellipsis (...) for the excerpt to make sense to the user.



For example, consider the following (please ignore the quote in this case):



"A set of words that is complete in itself, typically containing a subject and predicate, conveying a statement, question, exclamation, or command, and consisting of a main clause and sometimes one or more subordinate clauses."



An excerpt of 45 characters, including spaces:

"A set of words that is complete in itself, ty..."



The excerpt is good but we want the last word to be at least 3 characters long. That is, "typ" instead of "ty". It can be more than 3 characters though.



So, if that condition is not true, then we want to remove the last word ("ty") from the excerpt. In this case, we should have the following as excerpt:

"A set of words that is complete in itself,...".



As a bonus, you can also remove the comma, in this case to make thing even cleaner. Make sure your excerpt doesn't end with a comma before appending the ellipsis.



"A set of words that is complete in itself..."



How would you implement the above in your language. Sorry if the explanation is not clear. It's just for fun!

Comments

wale

Wed 20 Feb, 2019 08:38 pm
Hmm.
It's just for fun. You should be the one to start!
You can also show us your solution in multiple languages, if you don't mind.

goodmuyis

Thu 21 Feb, 2019 02:07 pm


function cr8_excerpt($gabbage_in){
    $gabbage_out = substr(  rtrim( strip_tags($gabbage_in) , ',.;:') ), 0, 45);
    //TODO: start an explosion
    return $gabbage_out. "...";
}

wale

Thu 21 Feb, 2019 03:17 pm
Hmm.
I think your code creates only the substring with the ellipsis. But what about the conditions?

netghost

Thu 21 Feb, 2019 04:13 pm
def spliter(first, nth):
for start in range(0, len(first), nth):
yield first[start:start+nth]

sen = "A set of words that is complete in itself, typically containing
a subject and predicate, conveying a statement, question, exclamation,
or command, and consisting of a main clause and sometimes one or more
subordinate clauses."

for spliters in spliter(sen, 45):
last_word = spliters.split()
a = str(last_word[-1])
if len(a) < 3:
print ' '.join(spliters.split(' ')[:-1]) + '...'
else:
print spliters + '...'

Lord Jerry

Fri 22 Feb, 2019 07:26 pm
const trunc = (text,len) => {
let arr = text.slice(0,len).split(' ');
if (arr[arr.length-1].length < 3) arr[arr.length-1] = '';
arr[arr.length - 2] = arr[arr.length-2].replace(',','');
return `${arr.join(' ').trim()}...`;

}
console.log(trunc('A set of words that is complete in itself, typically containing a subject and predicate, conveying a statement, question, exclamation, or command, and consisting of a main clause and sometimes one or more subordinate clauses.',45));