[pyar] Problema interesante: el anti-string
Pablo Ziliani
pablo en kultroom.com
Mie Ago 4 16:41:27 ART 2010
Pablo Ziliani wrote:
> Roberto Alsina wrote:
>> Dado un string S, crear una funcion f tal que S1>S2 => f(S1) < f(S2)
>
>
> o sea, algo así pero donde a[1] sea un caracter razonable, no?
>
> >>> a = ['papa', 'foo', 'pepe', 'bar', 'python'] >>> m =
> ord(max(''.join(a)))
> >>> b = [(i, ''.join(chr(m - ord(j)) for j in i)) for i in a]
> >>> b
> [('papa', '\t\x18\t\x18'), ('foo', '\x13\n\n'), ('pepe',
> '\t\x14\t\x14'), ('bar', '\x17\x18\x07'), ('python',
> '\t\x00\x05\x11\n\x0b')]
> >>> sorted(b, key=lambda x:x[0])
> [('bar', '\x17\x18\x07'), ('foo', '\x13\n\n'), ('papa',
> '\t\x18\t\x18'), ('pepe', '\t\x14\t\x14'), ('python',
> '\t\x00\x05\x11\n\x0b')]
> >>> sorted(b, key=lambda x:x[1])
> [('python', '\t\x00\x05\x11\n\x0b'), ('pepe', '\t\x14\t\x14'),
> ('papa', '\t\x18\t\x18'), ('foo', '\x13\n\n'), ('bar', '\x17\x18\x07')]
>
>>> S1 = [u'papa', u'foo', u'pepe', u'bar', u'python']
>>> chars = sorted(set(''.join(S1)))
>>> antichars = dict(zip(chars, reversed(chars)))
>>> S = [(word, u''.join(antichars[char] for char in word)) for word in S1]
>>> sorted(S, key=lambda x:x[0])
[(u'bar', u'tye'), (u'foo', u'phh'), (u'papa', u'fyfy'), (u'pepe',
u'frfr'), (u'python', u'fabohn')]
>>> sorted(S, key=lambda x:x[1])
[(u'python', u'fabohn'), (u'pepe', u'frfr'), (u'papa', u'fyfy'),
(u'foo', u'phh'), (u'bar', u'tye')]
More information about the pyar
mailing list