gpt4 book ai didi

ruby - 。从...开始?方法无法识别子串 'a'

转载 作者:数据小太阳 更新时间:2023-10-29 07:54:56 24 4
gpt4 key购买 nike

我想将一个字符串翻译成 pig latin。规则如下:

  1. 有效单词的长度为两个或更多个字母。
  2. 如果单词以辅音开头('a''e''i''o''u'),然后第一个字母移到单词的末尾。
  3. 然后添加'ay'

我设法想出了方法:

 def translate(word)
if word.size <= 2
word
elsif
word.size > 2
!word.start_with?('a', 'e', 'i', 'o', 'u')
x = word.reverse.chop.reverse
x.insert(-1, word[0])
x << "ay"
else
word << "ay"
end
end

但是,我的测试没有通过某些字符串,

Test Passed: Value == "c"
Test Passed: Value == "pklqfay"
Test Passed: Value == "yykay"
Test Passed: Value == "fqhzcbjay"
Test Passed: Value == "ndnrzzrhgtay"
Test Passed: Value == "dsvjray"
Test Passed: Value == "qnrgdfay"
Test Passed: Value == "npfay"
Test Passed: Value == "ldyuqpewypay"
Test Passed: Value == "arqokudmuxay"
Test Passed: Value == "spvhxay"
Test Passed: Value == "firvmanxay"
Expected: 'aeijezpbay' - Expected: "aeijezpbay", instead got: "eijezpbaay"
Expected: 'etafhuay' - Expected: "etafhuay", instead got: "tafhueay"

这些测试通过:

Test.assert_equals(translate("billy"),"illybay","Expected: 'illybay'")
Test.assert_equals(translate("emily"),"emilyay","Expected: 'emilyay'")

我不知道为什么。

最佳答案

如果 word 的长度大于或等于 2,则返回 word,如果不是,则执行 start_with 步骤,但是您的 else 声明工作?

尝试将长度验证修改为小于 2,然后检查单词“start_with”是否为元音,并仅返回单词加上 ay,如果不是,则执行第一个字符旋转步骤然后添加 ay 部分,例如:

def translate(word)
if word.size < 2
word
elsif word.start_with?('a', 'e', 'i', 'o', 'u')
word << "ay"
else
x = word.reverse.chop.reverse
x.insert(-1, word[0])
x << "ay"
end
end

关于ruby - 。从...开始?方法无法识别子串 'a',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196048/

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