Python刷OJ———UVa:156 Ananagrams

题干:

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different
orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this
attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think
that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
domain would be the entire English language, but this could lead to some problems. One could restrict
the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative
ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
“rearranged” at all. The dictionary will contain no more than 1000 words.

Input

Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
across lines. Spaces may appear freely around words, and at least one space separates multiple words
on the same line. Note that words that contain the same letters but of differing case are considered to
be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
consisting of a single ‘#’.

Output

Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
be at least one relative ananagram.
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries

Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon
————————————————————————————————————————————
简述:

众所周知任何英文单词都是由26个字母组成的,现在给定一个单词集,不区分大小写,找出其中只出现一次的字母组。例如一个单词集{post, stop, soon},其中stop和post的字母组是一样的,而soon字母组是独一的,于是我们输出soon。
题目要求输出的单词要按字母表排序,并且大写优先。

思路

(1)输入的单词由空格隔开,但是空格数量不确定,可以用split切分。题目中说,以一行#结束。那么就一直while 输入,用if来检测#并break。

(2)把单词存入列表后,我们对每一个单词先全部变小写,然后每个单词的字母按照字母表顺序排序(内置的sorted函数就可以轻易实现), 然后统计单词组出现的数目,找到数目为1的即可。统计数目我们可以用collections模块中的Counter函数,将会以字典的形式返回计数结果。

(3)找其中出现次数为1的单词组,并利用它的索引,直到原单词集中所对应的单词,但是注意,对于出现次数不唯一的单词组,将会返回None,利用过滤器过滤掉。

上代码(accept过了)

from collections import Counter


list_raw = []
while True:
    _str = input()
    if _str == "#":
        break
    else:
        list_raw.extend(list(_str.split(' ')))


def Lower(s_1):
    return s_1.lower()


def Sort(s_2):
    return ''.join(sorted(s_2))


def Find(s_3):
    if s_3[1] == 1:  # 检查字幕组是不是唯一的,唯一的则根据排序后字母组索引,返回最初列表中的单词
        return list_raw[list_sort.index(s_3[0])]


list_low = map(Lower, list_raw)
list_sort = list(map(Sort, list_low))
number = Counter(list_sort).items()
ana = list(filter(None, map(Find, number)))  # 过滤器过滤掉其中的None,因为单词组不唯一的单词组函数无返回值
for i in sorted(ana):
    print(i)

这道题的评判感觉还是比较松的,只要想到对每个单词的字母组排序,其他的都好说了

发布了28 篇原创文章 · 获赞 74 · 访问量 1632

猜你喜欢

转载自blog.csdn.net/CxsGhost/article/details/104083374
今日推荐