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

后端 未结 12 1774
一生所求
一生所求 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
    
    0 讨论(0)
  • 2021-02-04 18:10

    Just a coment to Garry Shutler, but I want code coloring:

    You really don't need to make it IQuaryable, neither the sort, so you can remove the second method. One step forwad is to use Aggregate for the cross product, it end up like this:

    IEnumerable<string> letters = new[]{
                    "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"};
    
    var result = Enumerable.Range(0, 4)
                    .Aggregate(letters, (curr, i) => curr.SelectMany(s => letters, (s, c) => s + c));
    
    foreach (var val in result)
         Console.WriteLine(val);
    

    Anders should get a Nobel prize for the Linq thing!

    0 讨论(0)
  • 2021-02-04 18:10

    javascript!

    var chars = 4, abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", top = 1, fact = [];
    for (i = 0; i < chars; i++) { fact.unshift(top); top *= abc.length; }
    for (i = 0; i < top; i++)
    {
        for (j = 0; j < chars; j++) 
            document.write(abc[Math.floor(i/fact[j]) % abc.length]);
        document.write("<br \>\n");
    }
    
    0 讨论(0)
  • 2021-02-04 18:14

    Inspired by Garry Shutler's answer, I decided to recode his answer in T-SQL.

    Say "Letters" is a table with only one field, MyChar, a CHAR(1). It has 26 rows, each an alphabet's letter. So we'd have (you can copy-paste this code on SQL Server and run as-is to see it in action):

    DECLARE @Letters TABLE (
        MyChar CHAR(1) PRIMARY KEY
    )
    DECLARE @N INT
    SET @N=0
    WHILE @N<26 BEGIN
        INSERT @Letters (MyChar) VALUES ( CHAR( @N + 65) )
        SET @N = @N + 1
    END
    -- SELECT * FROM @Letters ORDER BY 1
    
    SELECT A.MyChar, B.MyChar, C.MyChar, D.MyChar
    FROM @Letters A, Letters B, Letters C, Letters D
    ORDER BY 1,2,3,4
    

    The advantages are: It's easily extensible into using capital/lowercase, or using non-English Latin characters (think "Ñ" or cedille, eszets and the like) and you'd still be getting an ordered set, only need to add a collation. Plus SQL Server will execute this slightly faster than LINQ on a single core machine, on multicore (or multiprocessors) the execution can be in parallel, getting even more boost.

    Unfortunately, it's stuck for the 4 letters specific case. lassevk's recursive solution is more general, trying to do a general solution in T-SQL would necessarily imply dynamic SQL with all its dangers.

    0 讨论(0)
  • 2021-02-04 18:15

    GNU Bash!

    {a..z}{a..z}{a..z}{a..z}
    
    0 讨论(0)
  • 2021-02-04 18:15

    Use something which automatically Googles for every single letter combination, then see if there are more ".sz" or ".af" hits then ".com" hits at the five first results ... ;)

    Seriously, what you're looking for might be Tries (data structure) though you still need to populate the thing which probably is far harder...

    0 讨论(0)
提交回复
热议问题