gpt4 book ai didi

ruby - 使用多个数组创建计数

转载 作者:数据小太阳 更新时间:2023-10-29 07:13:08 25 4
gpt4 key购买 nike

我正在尝试按 0-9 和 a-z 的顺序创建数字和字母列表。

我有一组值value_array = ['0','1','2','3','4','5','6','7','8','9','a','b' ,'光盘','e','f','g','h','i','j','k','l','m','n','o','p','q ','r','s','','u','v','w','x','y','z']

和一个组合列表的数组,按顺序,这些数字可以产生 x 个字符,比方说三个

list_array = []

和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]

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

如何获取值数组以计算当前组合数组,以便我可以创建类似“的数组”['0','0','1']['0','0','2']['0','0','3']['0','0','4']['0','0','5']['0','0','6']......['a','z','1']....最后到 ['z','z','z']?

到目前为止,这是我的代码。对不起,如果它真的很疯狂。我对此一窍不通:

    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

最佳答案

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"]]

正如 Mladen 指出的那样,使用 Ruby 1.9 更容易:

(("0".."9").to_a + ("a".."z").to_a).repeated_permutation(3)

关于ruby - 使用多个数组创建计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com