我如何计算 Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch 中的字母?
print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'))
Run Code Online (Sandbox Code Playgroud)
说 58
好吧,如果有那么容易,我就不会问你了,现在是吗?!
维基百科说(https://en.wikipedia.org/wiki/Llanfairpwllgwyngyll#Placename_and_toponymy)
名称的长格式是英国最长的地名之一,也是世界上最长的地名之一,有 58 个字符(51 个“字母”,因为“ch”和“ll”是二合字母,在威尔士语)。
所以我想数一数并得到答案 51。
对。
print(len(['Ll','a','n','f','a','i','r','p','w','ll','g','w','y','n','g','y','ll','g','o','g','e','r','y','ch','w','y','r','n','d','r','o','b','w','ll','ll','a','n','t','y','s','i','l','i','o','g','o','g','o','g','o','ch']))
51
Run Code Online (Sandbox Code Playgroud)
是的,但那是作弊,显然我想使用这个词作为输入,而不是列表。
维基百科也说威尔士语的有向图是ch, dd, ff, ng, ll, ph, rh, th
https://en.wikipedia.org/wiki/Welsh_orthography#Digraphs
所以我们走了。让我们把长度加起来,然后去掉重复计算。
word='Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'
count=len(word)
print('starting with count of',count)
for index in range(len(word)-1):
substring=word[index]+word[index+1]
if substring.lower() in ['ch','dd','ff','ng','ll','ph','rh','th']:
print('taking off double counting of',substring)
count=count-1
print(count)
Run Code Online (Sandbox Code Playgroud)
这让我走到这一步
starting with count of 58
taking off double counting of Ll
taking off double counting of ll
taking off double counting of ng
taking off …Run Code Online (Sandbox Code Playgroud)