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

edited February 20 in Chats
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

  • edited February 20

    I will first go and smoke IGBO to clear my head.

  • edited February 20
    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.
  • edited February 21


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

  • edited February 21
    Hmm.

    I think your code creates only the substring with the ellipsis. But what about the conditions?
  • edited February 21
    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 + '...'
  • edited February 22
    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));
  • edited February 24

    Mr wale come and mark your exam

  • @wale u yet to mark our exam na

    Our parents Don dey ask for report card

  • @Lord Jerry said:
    @wale u yet to mark our exam na

    Our parents Don dey ask for report card

    And no goats in the house to claim that na goat chop am.

  • Hahaha. Nice attempts.

  • @wale said:
    Hahaha. Nice attempts.

    Ehen? who scored highest?

Sign In or Register to comment.