Creating an Alphanumeric Sequence in Ruby

How will I generate a sequential alphanumeric string?

Each line should contain only 8 characters.

Symbols possible for each position:

["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"] 

Also, if possible, I would like to select the starting point of the sequence.

For instance:

 00000001 00000002 00000003 00000005 ... 0000L3FH 0000L3FJ 0000L3FK 0000L3FL 0000L3FM 0000L3FN 0000L3FP ... 0000L4FP 0000L4FQ 0000L4FR 0000L4FS ... 0000M000 0000M001 0000M002 
+6
source share
2 answers

This is a permutation with repetition. Arrays can do this out of the box.

 chars = %w(0 1 2 BC) sequencer = chars.repeated_permutation(chars.size) #OP:replace chars.size by 8 10.times{p sequencer.next} #["0", "0", "0", "0", "0"] #["0", "0", "0", "0", "1"] #["0", "0", "0", "0", "2"] #["0", "0", "0", "0", "B"] #["0", "0", "0", "0", "C"] #["0", "0", "0", "1", "0"] #["0", "0", "0", "1", "1"] #["0", "0", "0", "1", "2"] #["0", "0", "0", "1", "B"] #["0", "0", "0", "1", "C"] p sequencer.next #["0", "0", "0", "2", "0"] 
+13
source

This allows you to set the starting point:

 dial = %w(0 1 2 AB) start_position = %w(AB 0) #for instance. p clock = start_position.map{|char| dial.rotate(dial.index(char))} #[["A","B","0","1","2"], ["B","0","1","2","A"], ["0","1","2","A","B"]] # start ticking: clock.shift.product(*clock){|tick|p tick} #["A", "B", "0"] #["A", "B", "1"] #["A", "B", "2"] #["A", "B", "A"] #["A", "B", "B"] #["A", "0", "0"] #... 
0
source

All Articles