5

I am trying to create a list of numbers and letters in order from 0-9 and a-z.

I have an array of values value_array = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d', 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w', 'x','y','z']

and an array for the list of combinations, in order, that these numbers can produce for x number of characters, let's say three

list_array = []

and an array for the current combination of letters and numbers (which I will turn into a string before pushing it to the list array,]

current_combo ['0','0', '0']

How do I get the value array to count up for the current combo array so that I can create arrays like" ['0','0','1'] ['0','0','2'] ['0','0','3'] ['0','0','4'] ['0','0','5'] ['0','0','6'] .. .. .. ['a','z','1'] .. .. and finally to ['z','z','z']?

Here is my code thus far. Sorry if it's really crazy. I'm a noob at this:

    exponent = test.count('?')


puts 36 ** exponent

possible_characters = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d',
'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w',
'x','y','z']

list = []

combo = []
end_combo = []

exponent.times do |e|
  combo << '0'
  end_combo << 'z'
end

puts combo.to_s

while combo != end_combo



end
1

2 Answers 2

7
xs = ("0".."9").to_a + ("a".."z").to_a
xs.product(xs, xs)
# [["0", "0", "0"], ["0", "0", "1"], ..., ["z", "z", "y"], ["z", "z", "z"]]

As Mladen noted, with Ruby 1.9 it's even easier:

(("0".."9").to_a + ("a".."z").to_a).repeated_permutation(3)
Sign up to request clarification or add additional context in comments.

1 Comment

Actually in 1.9.2 it's even easier to create the xs array: [*?0..?9, *?a..?z].
2
  value_array.repeated_permutation(n).to_a 

2 Comments

I think the OP wants a cartesian product, not a permutation.
1.9.2 has repeated_permutation which should do the trick: ruby-doc.org/core/classes/Array.html#M000289

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.