Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting A String In Python
#1
Implement a function that, given a feedback
and the size  of the screen, splits the feedback
into lines so that:
  • each token (i.e. sequence of non-whitespace characters) belongs to one of the lines entirely;
  • each line is at most size characters long;
  • no line has trailing or leading spaces;
  • each line should have the maximum possible length, assuming that all lines before it were also the longest possible length.


I have to complete this function by filling up the ... spaces.

import ...

def feedbackReview(feedback, size):
    return ...
Examples:
For 

feedback = "This is an example feedback"
and 

size = 8
the output should be

feedbackReview(feedback, size) = ["This is", 
                                  "an", 
                                  "example", 
                                  "feedback"]
Thank you.
Quote
#2
Thanks for posting your homework assignment. Would you like us to solve it for you?
Mekire and buran like this post
Quote
#3
Um..yes?
Quote
#4
(Jan-13-2017, 02:43 PM)Saif133 Wrote: Um..yes?
Kebap used sarcasm.  It wasn't very effective.

You need to get us started.  Show your attempt and tell us what is going wrong.
We help with homework.  We don't do it for you.
Quote
#5
>>>I have to do it in just one line

>>If i just use feedback.split()
it returns words as individual elements of a list
BUT,
what I want is that the length of each element of the list should be of the parameter (s).
How do I configure length of each split?

>>The other thing is,
if two words have a joint length less than (size) , they should be stored as a single element,which the .split() function does not do.

>>If i just use a list compression,
I will have to write a long piece of code that will check for white spaces and then recognize the character behind the whitespaces as single words, check their length, add the next word if it can satisfy the (size) criteria.

[feedback[i:i+size] for i in range(0, len(feedback), size)]

it will just read individual characters and not words and then store them by size in a list .
Quote
#6
(Jan-13-2017, 04:04 PM)Saif133 Wrote: I have to do it in just one line
I do not believe that this is a requirement of your assignment.
Teachers aren't in the habit of asking students to write convoluted one liners.
Even if it is, figure out how to do it first; then worry about compressing it.

First question is, what modules can you use?
If you are allowed to use the textwrap module for instance then I guess you can indeed do it in one line but that is just because someone else wrote it somewhere else.

So if the goal of the assignment is to use all tools at your disposal (as one would in real life) look into text wrap. Otherwise, we need to start writing some actual code.
https://docs.python.org/3/library/textwrap.html
Quote
#7
Thumbs Up 
the textwrap.wrap() worked!!
Thank you so much
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)