3 ms·
Here's an anagram-sorter. (I'm no Python ninja, but I quite liked my idea.) def sort(s): listt = list(s) listt.sort(cmp=lambda a,b: cmp( ord(b)%len(s
by San 17y ago
Here's an anagram-sorter. (I'm no Python ninja, but I quite liked my idea.)
def sort(s):
listt = list(s)
listt.sort(cmp=lambda a,b: cmp( ord(b)%len(s) , ord(a)%len(s) ))
print ''.join(listt),
sort('Oh,well')
sort('lord')
And this is the rather succinct Ruby version:
def sort s
print s.split(//).sort_by.with_index{|a,i|[-(a.ord%s.length),i]}.join
end
sort 'Oh,well'
sort 'lord'