I am trying to find every possible combination of the letters of the
alphabet to create a file by a program to solve simple replacement
ciphers. There shouldn’t be any repeats of letters in the combination
(i.e. a can only occur once in a given combination).
I had thought of using the code below to get every possible combination
and, even though it contains duplicate letters in the combinations it
generates, I was going to prune those out. The problem is that it would
take a very considerable number of years before the program finishes.
Anyone know of an algorithm that I could use to generate the
possibilities in a more timely manner?
class Value
def initialize val=‘a’
@value = val
end
def == rhs
if rhs.class != self.class
return false
end
rhs.value == self.value
end
def next
if @value == ‘z’
self.reset
else
@value = @value.next
end
end
def reset
@value = ‘a’
end
def value
@value
end
end
class Set
def initialize
@buffer = []
26.times {@buffer << Value.new}
end
def value
@buffer
temp = []
@buffer.each{|a| temp << a.value}
temp
end
def exausted?
@buffer.each do |a|
if a.value != ‘z’
return false
end
end
return true
end
def next
cursor = 1
while true
if @buffer.last(cursor)[0].value == 'z'
@buffer.last(cursor)[0].next()
cursor += 1
@buffer.last(cursor)[0].next()
else
@buffer.last(cursor)[0].next()
break
end
end
self.value
end
end
#################################################################
#################################################################
s = Set.new
f = open “sets.out”,‘w’
f << s.value
while s.exausted? == false
f << s.next << “\n”
end