gpt4 book ai didi

python - 在辅音之间找到每两个(不重叠的)元音

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:02 24 4
gpt4 key购买 nike

Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your task is to find all the substrings of the origina string that contain two or more vowels. Also, these substrings must lie in between consonants and should contain vowels only.

Input Format: a single line of input containing string .

Output Format: print the matched substrings in their order of occurrence on separate lines. If no match is found, print -1.

Sample Input: rabcdeefgyYhFjkIoomnpOeorteeeeet

Sample Output:

ee
Ioo
Oeo
eeeee


上述挑战取自 https://www.hackerrank.com/challenges/re-findall-re-finditer

以下代码通过了所有测试用例:

import re

sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])", input())

if sol:
for s in sol:
print(s)
else:
print(-1)

以下不是。

import re

sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})[^aiueo]", input())

if sol:
for s in sol:
print(s)
else:
print(-1)

它们之间的唯一区别是正则表达式的最后一位。我不明白为什么第二个代码会失败。我会争辩说 ?= 是无用的,因为通过分组 [aiueoAIUEO]{2,} 我已经将它排除在捕获之外,但显然我错了,我可以'告诉为什么。

有什么帮助吗?

最佳答案

先行方法允许结束一个元音序列的辅音开始下一个序列,而非先行方法要求这些序列之间至少有两个辅音(一个结束一个序列,另一个开始下一个序列,因为两者匹配)。

import re
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])', 'moomoom'))
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})[^aiueo]', 'moomoom'))

哪个会输出

['oo', 'oo']
['oo']

https://ideone.com/2Wn1TS

有点挑剔,关于您的问题描述,这两种尝试都不正确。它们允许大写元音、空格和符号作为分隔符。您可能想使用 [b-df-hj-np-tv-z] 而不是 [^aeiou] 并使用 flags=re.I

关于python - 在辅音之间找到每两个(不重叠的)元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com