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

后端 未结 12 1779
一生所求
一生所求 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 17:57

    Simpler Python!

    def getWords(length=3):
        if length == 0: raise StopIteration
        for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
            if length == 1: yield letter
            else:
                for partialWord in getWords(length-1):
                    yield letter+partialWord
    

提交回复
热议问题