作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何计算 Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch 中的字母?
print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'))
说 58
The long form of the name is the longest place name in the UnitedKingdom and one of the longest in the world at 58 characters (51"letters" since "ch" and "ll" are digraphs, and are treated as singleletters in the Welsh language).
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
是的,但那是作弊,显然我想使用这个词作为输入,而不是列表。
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)
这让我走到这一步
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 double counting of ll
taking off double counting of ch
taking off double counting of ll
taking off double counting of ll
taking off double counting of ll
taking off double counting of ch
49
看来我当时减去的太多了。我应该得到 51。现在一个问题是
llll
已找到 3
ll
s 并取下三个而不是两个。所以这将需要修复。 (不得重叠。)
ng
.维基百科没有说名称中有字母“ng”,但它被列为我上面引用的页面上的有向图之一。
|
来提供附加信息。在输入字符串中就像在字典中一样,只是为了让算法知道
ng
bit真的是两个字母。但显然我不想要
|
本身被算作一封信。
word='llong'
ANSWER NEEDS TO BE 3 (ll o ng)
word='llon|gyfarch'
ANSWER NEEDS TO BE 9 (ll o n g y f a r ch)
word='Llanfairpwllgwyn|gyllgogerychwyrndrobwllllantysiliogogogoch'
ANSWER NEEDS TO BE 51 (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)
还有这个有向图列表:
['ch','dd','ff','ng','ll','ph','rh','th']
规则将是:
llll
是 ll
+ ll
,不是 l
+ ll
+ l
|
算不上,但也不能完全无视,到此为止了ng
是一个有向图 |
作为一个字母(1 太高),然后它与
llll
一起起飞太多了(1 太低) - 错误取消
llong
对 (3)。
llon|gyfarch
错误 (10) - 计算
|
再次
最佳答案
像许多与字符串有关的问题一样,这可以通过正则表达式以简单的方式完成。
>>> word = 'Llanfairpwllgwyn|gyllgogerychwyrndrobwllllantysiliogogogoch'
>>> import re
>>> pattern = re.compile(r'ch|dd|ff|ng|ll|ph|rh|th|[^\W\d_]', flags=re.IGNORECASE)
>>> len(pattern.findall(word))
51
字符类
[^\W\d_]
(from
here ) 匹配不是数字或下划线的单词字符,即字母,包括那些带有变音符号的字符。
关于python - 计算威尔士语文本中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63528797/
我是一名优秀的程序员,十分优秀!