[Chicago] constant length string manipulation

Carl Karsten carl at personnelware.com
Sun Dec 2 16:42:36 CET 2007


def s5(s):
   if len(s)<>5:
     s=s5((s+' ')[:5])
   return s

print s5('Hello Worlds').__repr__()
print s5('He').__repr__()

The :5 blows the elegance.

I thought I could +1 and -1 at the same time and have it converge on 5, but that 
didn't work.

Carl K



Jeff Hinrichs - DM&T wrote:
> Actually, this kind of problem just screams recursion
> 
> def s5(s):
> 	if len(s) < 5: s=s5(s+' ')
> 	if len(s) > 5: s=s5(s[:-1])
> 	return s
> 
> print s5('Hello Worlds')
> 
> 
> On Dec 2, 2007 12:26 AM, Massimo Di Pierro <mdipierro at cs.depaul.edu> wrote:
>> f=lambda a,n: (a[:n]+' '*n)[:n] # theta(n) for every a
>> f('hello world',5)
>>
>>
>> On Dec 2, 2007, at 12:14 AM, Cosmin Stejerean wrote:
>>
>>> It's cheating. What if he wanted 10 instead of 5? Hardcoding spaces is
>>> a bad idea.
>>>
>>> Cosmin Stejerean (m)
>>>
>>>
>>> On Dec 1, 2007, at 9:29 PM, Carl Karsten <carl at personnelware.com>
>>> wrote:
>>>
>>>> string concatenation is icky.
>>>>
>>>> you have too many 5s.
>>>>
>>>> the line is too long.
>>>>
>>>> your variables are too short.
>>>>
>>>> there are no comments.
>>>>
>>>> and it isn't OOP.
>>>>
>>>> Carl K
>>>>
>>>> Massimo Di Pierro wrote:
>>>>> what's wrong with
>>>>>
>>>>> b=(a[:5]+'     ')[:5]
>>>>>
>>>>> Massimo
>>>>>
>>>>> On Nov 30, 2007, at 9:25 AM, Lukasz Szybalski wrote:
>>>>>
>>>>>> On Nov 29, 2007 3:29 PM, Cosmin Stejerean <cstejerean at gmail.com>
>>>>>> wrote:
>>>>>>> b.ljust(5)[:5]
>>>>>> a='1234567890' or a='123'
>>>>>> b=a.ljust(5)[:5]
>>>>>>
>>>>>> so I guess ljust will do.
>>>>>> Thanks.
>>>>>> Lucas
>>>>>> _______________________________________________
>>>>>> Chicago mailing list
>>>>>> Chicago at python.org
>>>>>> http://mail.python.org/mailman/listinfo/chicago
>>>>> _______________________________________________
>>>>> Chicago mailing list
>>>>> Chicago at python.org
>>>>> http://mail.python.org/mailman/listinfo/chicago
>>>>>
>>>>>
>>>> _______________________________________________
>>>> Chicago mailing list
>>>> Chicago at python.org
>>>> http://mail.python.org/mailman/listinfo/chicago
>>> _______________________________________________
>>> Chicago mailing list
>>> Chicago at python.org
>>> http://mail.python.org/mailman/listinfo/chicago
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>>
> 
> 
> 


More information about the Chicago mailing list