Code golf: find all anagrams

后端 未结 8 2024
再見小時候
再見小時候 2021-02-04 06:00

A word is an anagram if the letters in that word can be re-arranged to form a different word.

Task:

  • The shortest source code by character count to find

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 06:10

    Python, 167 characters, includes I/O

    import sys
    d={}
    for l in sys.stdin.readlines():
     l=l[:-1]
     k=''.join(sorted(l)).lower()
     d[k]=d.pop(k,[])+[l]
    for k in d:
     if len(d[k])>1: print(' '.join(d[k]))
    

    Without the input code (i.e. if we assume the wordlist already in a list w), it's only 134 characters:

    d={}
    for l in w:
     l=l[:-1]
     k=''.join(lower(sorted(l)))
     d[k]=d.pop(k,[])+[l]
    for k in d:
     if len(d[k])>1: print(' '.join(d[k]))
    

提交回复
热议问题