Ruby Recursion Difficulties
I'm writing a bit of code to find every combination of a group of
characters that is more than 2 characters long using a recursive function.
I'm not sure how or why, but when I run the code, it deletes the list of
letters that it's building the different combinations from.
def nuther letters, word, n, step
arr=[]
word[step]=letters.delete_at(n)
if word[step+1] != nil
letters.length.times do |t|
arr=arr+nuther(letters, word, t, (step+1))
end
else
arr<<"#{word.join}"
end
return arr
end
def everycombo letters
arr=[]
(letters.length-2).times do |t|
word=[]
(t+2).times do
word<<[]
end
letters.length.times do |n|
arr=arr+nuther(letters, word, n, 0)
end
end
return arr
end
puts everycombo ["a","b","c","d","e"]
What's going on here?
No comments:
Post a Comment