小小的挑战。
目标(nodeJS,循环是一个* interator函数,使用" yield"策略)
var str;
var minChars = 1;
var maxChars = 10;
for (str of loop('abcdefghijklmnopqrstuvwxyz',minChars,maxChars)) {
console.log(str);
}
约束:
示例输出(顺序很重要):
a
b
c
[...]
z
aa
ab
ac
[...]
aaa
aab
aac
[...]
aba
abb
abc
[...]
bza
bzb
bzc
[...]
zzzzzzzzzz
答案 0 :(得分:3)
琐碎的解决方案:
function* loop(alphabet, min, max) {
if (min > max) throw new RangeError("max needs to be greater than min");
if (min <= 0)
yield "";
if (max > 0)
for (const rest of loop(alphabet, min-1, max-1))
for (const a of alphabet)
yield rest+a;
}