[Chicago] constant length string manipulation

Jeff Hinrichs - DM&T jeffh at dundeemt.com
Sun Dec 2 15:29:53 CET 2007


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
>



-- 
Jeff Hinrichs
Dundee Media & Technology, Inc
jeffh at dundeemt.com
402.218.1473


More information about the Chicago mailing list