【发布时间】:2012-01-27 12:38:21
【问题描述】:
我有一个如下所示的数据结构:
[
{:choices=>["Hello", "Hi"]},
" ",
{:choices=>["wor", {:choices=>["ld", "d"]}, "there"]},
", says ",
"your ",
{:choices=>["friend", "amigo"]}
]
在这种结构中,选择节点表示一组可能的值,并且可以组合。
我需要一个(可能是递归的)Ruby 方法,它会输出一个包含所有可能输出的数组。即,对于这个例子:
[
"Hello word, says your friend",
"Hello world, says your friend",
"Hello there, says your friend",
"Hi word, says your friend",
"Hi world, says your friend",
"Hi there, says your friend",
"Hello word, says your amigo",
"Hello world, says your amigo",
"Hello there, says your amigo",
"Hi word, says your amigo",
"Hi world, says your amigo",
"Hi there, says your amigo"
]
我希望这是递归的,但我已经研究了一个小时,我想要另一双眼睛。我在这里错过了什么?
【问题讨论】:
-
你的数据结构错误。
"wor"、{:choices=>["ld", "d"]}、"there"这三个元素将被解释为替代,因此不会给出您想要的结果。
标签: ruby algorithm data-structures recursion