What's a good way for figuring out all possible words of a given length

后端 未结 12 1956
一生所求
一生所求 2021-02-04 17:27

I\'m trying to create an algorithm in C# which produces the following output strings:

AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ

What is the

12条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 18:06

    Python!

    (This is only a hack, dont' take me too seriously :-)

    # Convert a number to the base 26 using [A-Z] as the cyphers
    def itoa26(n): 
       array = [] 
       while n: 
          lowestDigit = n % 26
          array.append(chr(lowestDigit + ord('A'))) 
          n /= 26 
       array.reverse() 
       return ''.join(array)
    
    def generateSequences(nChars):
       for n in xrange(26**nChars):
          string = itoa26(n)
          yield 'A'*(nChars - len(string)) + string
    
    for string in generateSequences(3):
       print string
    

提交回复
热议问题