Welcome, guest | Sign In | My Account | Store | Cart
#oneliner to reverse,sort,extend a string /tuple/list
#can also pass user defined func in sort
#string or tuple or list can be extended to list,tuple
#for None returns 0

op=lambda o,f,p='',t=type:t(o) in [t(()),t(''),t([])] and eval("lambda o,l,t,p:[l.extend(o),l.%s(%s),%s(l)][2]"%(f,['p',''][f=='reverse' or not p],[["''.join","tuple"][t(o)==t(())],''][t(o)==t([])]))(o,[],t,p)

#if u want that faster versions
#then use this as it creates three lambdas and returns them
#so that they may be used later
reverse,extend,sort = map(lambda a:eval("lambda o,%sl=[[]],t=type:t(o) in [t(()),t(''),t([])] \
                           and [l.pop(0),l.append(list(o)),l[0].%s, t(o)==t('') and ''.join(l[0])\
                           or t(o)==t(()) and tuple(l[0]) or l[0]][3]"%(a[0],a[1])),

#if you are wondering whats happening above....
#the basic approach is to convert tuple/string to list
#do operation and reconvert it to tuple/string
#may be recipe
#will help u to understand that

#test code
print 'sort',obj,op(obj,'sort')
print 'extend',obj,op(obj,'extend',obj)
print 'reverse',obj,op(obj,'reverse')
print 'sort',obj,op(obj,'sort',lambda a,b:cmp(b,a))
print 'extend',obj,op(obj,'extend',' uniyal')
print 'reverse',obj,op(obj,'reverse')
print 'sort',obj,op(obj,'sort')
print 'extend',obj,op(obj,'extend','abc')
print 'reverse',obj,op(obj,'reverse')


  • revision 2 (21 years ago)
  • previous revisions are not available