You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双元音计数器开发问题:重复双元音未识别及qu排除需求

双元音计数器问题解决方案

原代码存在三个核心问题:

  1. 错误使用strip()处理字符串,该方法仅移除首尾指定字符,无法保留中间的连续双元音结构;
  2. 仅判断双元音是否存在,未统计同一双元音的多次出现;
  3. 未实现排除qu的逻辑。

以下是修正后的代码:

word = input("Tell me the word you wish to check for diphthongs: ").lower()
total = 0
diphthongs = {'ue', 'ie', 'ae', 'ee', 'oe', 'ui', 'ii', 'ai', 'ei', 'oi', 'ua', 'ia', 'aa', 'ea', 'oa', 'uo', 'io',
              'ao', 'eo', 'oo', 'uu', 'iu', 'au', 'eu', 'ou'}

for i in range(len(word) - 1):
    current_pair = word[i:i+2]
    if current_pair in diphthongs and current_pair != 'qu':
        total += 1

print(f"There are {total} diphthongs in the string")

关键改进点:

  • 统一大小写:用lower()将输入转为小写,避免因大小写差异导致的匹配失败;
  • 逐个检查连续双字符:遍历字符串的每个位置,提取连续两个字符进行判断,确保同一双元音出现多次时能被重复计数(比如courthouse中的两个ou会被统计为2次);
  • 排除qu:直接在判断条件中过滤qu,符合需求;
  • 使用集合优化查询:将双元音列表改为集合,提升成员查询的效率(数据量较小时差异不明显,但更符合最佳实践)。

测试示例:

  • 输入courthouse,输出There are 2 diphthongs in the string;
  • 输入queen,输出There are 1 diphthongs in the string(qu被排除,仅统计ee)。

内容的提问来源于stack exchange,提问作者Tanner Fife

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:55:58