gpt4 book ai didi

ruby - 将单词与前一个字符切片

转载 作者:数据小太阳 更新时间:2023-10-29 08:14:45 25 4
gpt4 key购买 nike

有没有更好的方法在 ruby​​ 中分割单词?我的解决方案:

str="TAMILNADU"
pairs=[]
numPairs= str.length-1
i=0
while i<numPairs do
pairs[i] = str.slice(i,2)
i+=1
end

结果

["TA", "AM", "MI", "IL", "LN", "NA", "AD", "DU"] 

最佳答案

是的,有:

irb(main):001:0> "TAMILNADU".chars.each_cons(2).map(&:join)
=> ["TA", "AM", "MI", "IL", "LN", "NA", "AD", "DU"]

参见 the documentation of Enumerable#each_cons .

关于ruby - 将单词与前一个字符切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28941468/

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