Python Forum

Full Version: split the line fixed length tokens
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

i'm solving a task on codefights.com, it's the following: given a long line and a number, split the line into tokens where each token has length of at most the given number, and each token can be two or more words if their total length us less than the given number. for example, if s = "this is an example feedback", n = 8, then the returned value should be ["this is", "an", "example", "feedback"].

here's the link to the task: https://codefights.com/arcade/python-arc...k69Jm3gNnm

this is the code i should complete:
import ...

def feedbackReview(feedback, size):
    return ...
the ellipsis is what i can replace. so i guess i need to import re, but how should i split the line into tokens using that regex? i can't figure out the regex
i needed the list elements to be some max length. how could i do it with split()?

anyways, i found textwrap.wrap() which does exactly what i needed
First split, then go through the list and check for length and make a new list.
There are many ways to code that.
yeah i know that. but as i mentioned, the ellipsis is what i ca replace. so i needed a way to do it in one line. well, now i'm thinking that i could do it using generators. (i'm just learning python and i've just read about generators that's why i thought of using them here)
Ok,I understand.