Python Forum
split the line fixed length tokens - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: split the line fixed length tokens (/thread-6487.html)



split the line fixed length tokens - bb8 - Nov-25-2017


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-arcade/slithering-in-strings/Rzf4YKMk69Jm3gNnm

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


RE: split the line fixed length tokens - heiner55 - Nov-25-2017

See http://www.pythonforbeginners.com/dictionary/python-split


RE: split the line fixed length tokens - bb8 - Nov-25-2017

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


RE: split the line fixed length tokens - heiner55 - Nov-25-2017

First split, then go through the list and check for length and make a new list.
There are many ways to code that.


RE: split the line fixed length tokens - bb8 - Nov-25-2017

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)


RE: split the line fixed length tokens - heiner55 - Nov-25-2017

Ok,I understand.