我目前正在研究一個Ruby問題,其中我基本上是在創建自己的語言。下面(名詞,動詞,文章)可以被認爲是單詞。然而,爲了創造一個有效的句子,我必須有一個動詞,一個名詞或者至少兩篇文章。紅寶石在維持秩序的同時用子串創建句子
名詞: 「ABCD」, 「C」, 「DEF」, 「H」,「IJ, 「CDE」
動詞: 「BC」, 「FG」, 「G」, 「HIJ」, 「BCD」
文章:「一」,「交流」,「E」
所以我試圖做的基本上是寫將一個字符串返回所有可能的有效句子(同時保持字符的方法以相同的順序在詞語之間插入空格)
例如輸入= "abcdefg"
返回列表
[ "a bc def g", "a bcd e fg", "abcd e fg"]
所以,我想打破這個問題,這是我迄今爲止
alpha = "abcdefg"
nouns = ["abcd", "c", "def", "h", "ij", "cde"]
verbs = ["bc", "fg", "g", "hij", "bcd"]
articles = ["a", "ac", "e"]
verbArray = [];
nounArray = [];
articleArray = [];
nouns.each do |item|
if alpha.include?(item)
nounArray << item
end
end
verbs.each do |item|
if alpha.include?(item)
verbArray << item
end
end
articles.each do |item|
if alpha.include?(item)
articleArray << item
end
end
puts nounArray.inspect => ["abcd", "c", "def", "cde"]
puts verbArray.inspect => ["bc", "fg", "g", "bcd"]
puts articleArray.inspect => ["a", "e"]
我的思維過程是我第一次希望得到所有可能的組合爲每個單詞(名詞,動詞,文章)。我不確定這是否是解決這個問題最有效的方法,但除了這一步之外,我還沒有成功地嘗試形成有序的句子。
我一直在搜索堆棧和其他網站的組合/排序技術的類型,再加上我正試圖避免使用正則表達式。我誠摯地感謝任何方向/反饋,以便如何繼續我的旅程來解決這個問題。感謝您的時間!
是否有'動詞/名詞/ article'爲了進制限制? –
另外,'我必須有一個動詞,一個名詞,或至少兩篇文章。 '不明確。 –
示例值看起來很神祕。是否有充分的理由不使用實際的(或可能是假的但可發音的)名詞,動詞和物品? – Stefan