def greedyWrap(inString, width=80):
 outLines = []
 if (inString.find('\n\n') >= 0):
  paragraphs = string.split(inString, '\n\n')
  for para in paragraphs:
   outLines.extend((greedyWrap(para) + ['']))
  return outLines
 inWords = string.split(inString)
 column = 0
 ptr_line = 0
 while inWords:
  column = (column + len(inWords[ptr_line]))
  ptr_line = (ptr_line + 1)
  if (column > width):
   if (ptr_line == 1):
    pass
   else:
    ptr_line = (ptr_line - 1)
   (l, inWords) = (inWords[0:ptr_line], inWords[ptr_line:])
   outLines.append(string.join(l, ' '))
   ptr_line = 0
   column = 0
  elif (not (len(inWords) > ptr_line)):
   outLines.append(string.join(inWords, ' '))
   del inWords[:]
  else:
   column = (column + 1)
 return outLines

def greedyWrap ( inString , width = 80 ) : outLines = [ ] if ( inString . find ( '\n\n' ) >= 0 ) : paragraphs = string . split ( inString , '\n\n' ) for para in paragraphs : outLines . extend ( ( greedyWrap ( para ) + [ '' ] ) ) return outLines inWords = string . split ( inString ) column = 0 ptr_line = 0 while inWords : column = ( column + len ( inWords [ ptr_line ] ) ) ptr_line = ( ptr_line + 1 ) if ( column > width ) : if ( ptr_line == 1 ) : pass else : ptr_line = ( ptr_line - 1 ) ( l , inWords ) = ( inWords [ 0 : ptr_line ] , inWords [ ptr_line : ] ) outLines . append ( string . join ( l , ' ' ) ) ptr_line = 0 column = 0 elif ( not ( len ( inWords ) > ptr_line ) ) : outLines . append ( string . join ( inWords , ' ' ) ) del inWords [ : ] else : column = ( column + 1 ) return outLines
given a string and a column width .