That function does not give the correct output for larger arrays. The

level functions can be generated with eval, see the code below. However

there must be a better Ruby way.

def calc level

tmp = [“def calcd nums\ntmp=[]\n”]

str = “n1”

num = 1

level.times {

tmp.push “nums.each{ |n#{num}| tmp.push #{str}\n”

str = str+" + n#{num+=1}"

}

level.times { tmp.push “}”}

tmp.push “\ntmp\n”

tmp.push “end”

tmp.to_s

end

array, times = (‘1’…‘10’).to_a, 3

eval(calc(times))

p calcd(array)

Harry K. wrote:

On Sun, Oct 18, 2009 at 11:43 AM, Toi T. [email protected] wrote:

```
}}}
```

tmp

end

## puts calc([“0”, “1”], 3)

Does this give you the right output?

No recursion, but…

def calc(n, v)

h,t = Hash[*(0…n.size).zip(n).flatten],[]

(1…v).each{|x| (0…n.size**x).each{|y| t <<

y.to_s(n.size).rjust(x,“0”)}}

t.map{|a| a.split(//).map{|b| h[b.to_i]}.join}.sort

end

p calc([“0”,“1”], 3)

Harry