I want understand how this word wrap program playing on input

MRAB python at mrabarnett.plus.com
Thu Apr 4 15:14:36 EDT 2019


On 2019-04-04 19:53, David Raymond wrote:
> The function is constructing a list of the lines, which it will combine at the end. Answering the questions in reverse order:
> 
> 3. Also why that `if` test is required there.
> The if statement is saying "I don't have room on my current line for the next word, so time to start a new line"
> 
> 2. In the if clause why `current_line_length` is set back to 0 every time the condition is true.
> current_line_length is, well, the length of the current line. Where it's set to 0 is when you've just started a new line, so it's saying "I'm now on a new line and have used up 0 characters so far on that line"
> 
> 1. why  `line_length` is set to `current_line_length` initially?
> lines_of_words starts out empty. Setting current_line_length to line_length triggers the "hey, I need to make a new line" on the first word, effectively creating the first line.
> 
> 
> 
> Also, I feel like there's a bug in this, where
> 
> current_line_length += len(word)
> 
> should be...
> 
> current_line_length += (len(word) + 1)
> 
> Otherwise the line length doesn't count the spaces. And a lot of little words will result in longer lines than you asked for.
> 
Yep, spotted that too! :-) BTW, your fix also a bug: the last word on a 
line won't be followed by a space (off-by-one). The easiest fix for that 
is to add 1 to line_length initially, another little trick.
> 
> -----Original Message-----
> From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Arup Rakshit
> Sent: Thursday, April 04, 2019 2:33 PM
> To: Python
> Subject: I want understand how this word wrap program playing on input
> 
> I am reading a Python book, where the author used a simple word wrap program to explain another concept. But I am not understanding some parts of the program.
> 
>      def wrap(text, line_length):
>          """Wrap a string to a specified line length"""
>          words = text.split()
>          lines_of_words = []
>          current_line_length = line_length
>      
>          for word in words:
>              if current_line_length + len(word) > line_length:
>                  lines_of_words.append([]) # new line
>                  current_line_length = 0
>              lines_of_words[-1].append(word)
>              current_line_length += len(word)
>      
>          lines = [' '.join(line_of_words) for line_of_words in lines_of_words]
>          return '\n'.join(lines)
>      
>      wealth_of_nations = "The annual labour of every nation is the fund which or" \
>      "iginally supplies it with all the necessaries and conveniencies of life wh" \
>      "ich it annually consumes, and which consist always either in the immediate" \
>      " produce of that labour, or in what is purchased with that produce from ot" \
>      "her nations. According, therefore, as this produce, or what is purchased w" \
>      "ith it, bears a greater or smaller proportion to the number of those who a" \
>      "re to consume it, the nation will be better or worse supplied with all the" \
>      " necessaries and conveniencies for which it has occasion."
> 
> Now when I call it:
> 
>      python3 -i wrapper.py
>      >>> wrap(wealth_of_nations, 25)
>      'The annual labour of every\nnation is the fund which\noriginally supplies it with\nall the necessaries and\nconveniencies of life which\nit annually consumes, and\nwhich consist always either\nin the immediate produce of\nthat labour, or in what is\npurchased with that produce\nfrom other nations.\nAccording, therefore, as\nthis produce, or what is\npurchased with it, bears a\ngreater or smaller\nproportion to the number of\nthose who are to consume it,\nthe nation will be better or\nworse supplied with all the\nnecessaries and\nconveniencies for which it\nhas occasion.'
>      >>> print(_)
>      The annual labour of every
>      nation is the fund which
>      originally supplies it with
>      all the necessaries and
>      conveniencies of life which
>      it annually consumes, and
>      which consist always either
>      in the immediate produce of
>      that labour, or in what is
>      purchased with that produce
>      from other nations.
>      According, therefore, as
>      this produce, or what is
>      purchased with it, bears a
>      greater or smaller
>      proportion to the number of
>      those who are to consume it,
>      the nation will be better or
>      worse supplied with all the
>      necessaries and
>      conveniencies for which it
>      has occasion.
> 
> My questions are:
> 
> 1. why  `line_length` is set to `current_line_length` initially?
> 2. In the if clause why `current_line_length` is set back to 0 every time the condition is true.
> 3. Also why that `if` test is required there.
> 
> Can anyone help me to understand this program, I am totally confused.
> 
> Thanks,
> 
> Arup Rakshit
> ar at zeit.io
> 
> 
> 




More information about the Python-list mailing list