当你翻阅元素周期表的时候,看着各种元素符号在你眼前闪过,你曾否有什么惊人的发现?某些元素符号首尾相连,竟能连成许多有趣的单词,例如GeNIUS(铬 氮 碘 铀 硫),LuCK(镥 碳 钾),创意十足。在好奇心的驱使下,我决定找一找有多少这样的单词。幸好我们有Python,可以免去手动列举的麻烦,下面是我的代码。
注:因为元素符号所能组成的词较少,所以我在匹配时添加了两个字母的元素符号倒置的情况(如ScINeCe可匹配science),最终的元素符号拼写可能与单词不同。
import re single = [ 'h', 'b', 'c', 'n', 'o', 'f', 'p', 's', 'k', 'v', 'y', 'i', 'w', 'u' ] double = [ 'he', 'li', 'be', 'ne', 'na', 'mg', 'al', 'si', 'cl', 'ar', 'ca', 'sc', 'ti', 'cr', 'mn', 'fe', 'co', 'ni', 'cu', 'zn', 'ga', 'ge', 'as', 'se', 'br', 'kr', 'rb', 'sr', 'zr', 'nb', 'mo', 'tc', 'ru', 'rh', 'pd', 'ag', 'cd', 'in', 'sn', 'sb', 'te', 'xe', 'cs', 'ba', 'la', 'ce', 'pr', 'nd', 'pm', 'sm', 'eu', 'gd', 'tb', 'dy', 'ho', 'er', 'tm', 'yb', 'lu', 'hf', 'ta', 're', 'os', 'ir', 'pt', 'au', 'hg', 'tl', 'pb', 'bi', 'po', 'at', 'rn', 'fr', 'ra', 'ac', 'th', 'pa', 'np', 'pu', 'am', 'cm', 'bk', 'cf', 'es', 'fm', 'md', 'no', 'lr', 'rf', 'db', 'sg', 'bh', 'hs', 'mt', 'ds', 'rg', 'cn', 'nh', 'fl', 'mc', 'lv', 'ts', 'og' ] def in_double(str): return str in double def invrsd_double(str): return (str[1] + str[0]) in double def is_double(str): return str in double or (str[1] + str[0]) in double punct = '!,;:.?"\'、,;。' def rm_punct(str): str = re.sub(r'[{}]+'.format(punct),' ',str) return str.strip() def get(word): ln = len(word) idx = 0 res = [] while idx < ln: if (idx + 1) < ln and in_double(word[idx] + word[idx+1]): res.append(word[idx] + word[idx+1]) idx += 2 elif word[idx] in single: res.append(word[idx]) idx += 1 elif (idx + 1) < ln and invrsd_double(word[idx] + word[idx+1]): res.append('*' + word[idx] + word[idx+1]) idx += 2 elif idx >= 2 and in_double(word[idx-2] + word[idx-1]) and (word[idx-2] in single) and is_double(word[idx-1] + word[idx]): res.pop() res.append(word[idx-2]) if in_double(word[idx-1] + word[idx]): res.append(word[idx-1] + word[idx]) else: res.append('*' + word[idx-1] + word[idx]) idx += 1 else: res.append('?' + word[idx]) idx += 1 return res def spell(arr): procd = '' for str in arr: if len(str) == 1: procd += str.upper() elif str[0] == '?': if (str[1].isdigit()): procd += str[1] else: procd += '?' elif str[0] == '*': procd += (str[2].upper() + str[1]) else: procd += (str[0].upper() + str[1]) return procd with open('src.txt','r') as f: src = rm_punct(f.read()).split() txt = '' for s in src: txt += spell(get(s.lower())) + ' ' print(txt)
测试输入:
输出: