code for Computer Language Shootout

Michael Spencer mahs at telcopartners.com
Wed Mar 16 13:16:18 EST 2005


F. Petitjean wrote:
> Le Tue, 15 Mar 2005 23:21:02 -0800, Michael Spencer a écrit :
> 

>>
>>def output(seq, linelength = 60):
>>     if seq:
>>         iterseq = iter(seq)
>>         while iterseq:
>>             print "".join(islice(iterseq,linelength))
> 
> I suppose you mean :
>            print "".join( str(item) for item in islice(iterseq, linelength) )
> 	   #  using python 2.4 genexp
> 
No, as written, given the seq is a sequence of single character strings already 
(changing the signature might clarify that):

def output(charseq, linelength = 60):
     if charseq:
         iterseq = iter(charseq)
         while iterseq:
             print "".join(islice(iterseq,linelength))

  >>> output("*" * 120, 40)
  ****************************************
  ****************************************
  ****************************************
  >>> output(['*'] * 120, 40)
  ****************************************
  ****************************************
  ****************************************
  >>>

Cheers
Michael




More information about the Python-list mailing list