Python Forum
split the line fixed length tokens
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
split the line fixed length tokens
#1

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
Reply
#2
See http://www.pythonforbeginners.com/dictio...thon-split
Reply
#3
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
Reply
#4
First split, then go through the list and check for length and make a new list.
There are many ways to code that.
Reply
#5
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)
Reply
#6
Ok,I understand.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Table with fixed columns Mike2607 3 2,784 Nov-26-2019, 08:17 PM
Last Post: ichabod801
  Same line length in slope fields schniefen 1 2,118 Apr-24-2019, 04:37 AM
Last Post: scidam
  ValueError-trying to split a line samu3l 7 5,447 Feb-06-2018, 07:28 PM
Last Post: Larz60+
  How to access each line in for loop and split string SriRajesh 2 3,151 Aug-14-2017, 06:05 PM
Last Post: tetrmnot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020