开发者

How to write this Python excerpt [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonab开发者_如何学运维ly answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

Write a Python excerpt that gathers together words into no more than width character lines. You may assume words is a list of words, in order, to be output and width is the maximum number of characters a line may have. Print each line just when you can't add another word to the line without exceeding the character limit for a line.


Check out the textwrap module.

import textwrap

words = 'some words to print out'
width = 10

for line in textwrap.wrap(words, width):
    print line


In this example, you're basically implementing naïve word-wrap.

This is the crude and inelegant way I'd do it:

  1. Create a holding variable which you use to dump a single line's output into.
  2. While the length of that variable is less than or equal to the maximum line length, pop(-1) the first element out of the list of words, and append it to the placeholder string.
  3. When this condition isn't true (test it before popping the element), print the line and start all over again.

I'm not doing it for you, but this is the way I would start.


May be you should separate creating line from

def wrap(words, maxWidth):
    res = []
    line = []
    cur_len = 0
    for word in words:
        if cur_len + len(word) <= maxWidth:
            line.append(word)
            cur_len += len(word)
        else:
            res.append(line)
            line = [word]
            cur_len = len(word)
    res.append(line)
    return res

words = "quick brown fox jumps over the lazy dog".split(" ")
print '\n'.join(map(lambda line: ' '.join(line), process(words, 12)))


Ok, so let's assume you have a string like this:

string = 'the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog'

It's long so there's something to go on. Let's call the width w.

Since you're going on words, you would want to split the string on the whitespace character ' '. This would give you a list of words.

You want to check the length of each word in the list against the number of possible letters left in the line. So if your width was 30, and you print 'the', your amount left is 27. If you can subtract the word length from the number of remaining characters and still be positive, then print the word. Else, go onto a new line.


I suggest looking into the a for loop and the len() functions, this should take no more than 10 lines of code. something like

for word in wordList:
    if((len(word)+len(currLine))<maxLen):
//more code here that I ain't giving you

You should append words that fie(plus a space to currLine. Once you don't have enough space print currLine and set currLine to word. Hope that's enough help, if not talk to your professor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜