Created
January 29, 2011 16:19
-
-
Save pedrovanzella/801960 to your computer and use it in GitHub Desktop.
Generates all possible combinations of a set, including doubles, to fill given spaces on a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################## | |
# genpass.rb # | |
# Generates a table of possible # | |
# passwords according to a fixed pattern # | |
# # | |
# Ugly, but works # | |
############################################## | |
def generate_combinations(array, r) | |
n = array.length | |
indices = (0...r).to_a | |
final = (n - r...n).to_a | |
while indices != final | |
yield indices.map {|k| array[k]} | |
i = r - 1 | |
while indices[i] == n - r + i | |
i -= 1 | |
end | |
indices[i] += 1 | |
(i + 1...r).each do |j| | |
indices[j] = indices[i] + j - i | |
end | |
end | |
yield indices.map {|k| array[k]} | |
end | |
# Our character space, add or remove as you need | |
all = ['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', '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'] | |
(1..2).each do |i| # Change this interval | |
(1..2).each do |j| # And this | |
generate_combinations(all * i, i) do |g| | |
generate_combinations(all * j, j) do |h| | |
# Change this string according to your pattern | |
puts "(#{g.pack("a"*i)})(#{h.pack("a"*j)})" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment