根据模式生成数字

时间:2013-06-05 03:46:10

标签: ruby

我想生成四位数字(从00009999),看起来像ABCDAABC。 例如,模式ABCD将具有类似

的数字

0123012401250126012701280129等等。

模式AABC如:

00120013001400150016等。

1 个答案:

答案 0 :(得分:2)

(0..9).to_a.permutation(4).map{|a, b, c, d| [a, b, c, d].join}

(0..9).to_a.permutation(3).map{|a, b, c| [a, a, b, c].join}

或者

(0..9).to_a.permutation(4).map{|a, b, c, d| "#{a}#{b}#{c}#{d}"}

(0..9).to_a.permutation(3).map{|a, b, c| "#{a}#{a}#{b}#{c}"}
相关问题