Search code examples
kdb

kdb/q: generate all possible "strings" of length N


in KDB/Q, how do I generate all combinations given an alphabet universe (doesn't have to be string, could be like a list of numbers) for a given size n?

What I am looking for is similar to this in python, but in q. How to generate all possible strings in python?


Solution

  • Using cross would be the easiest method:

    q){y(x cross)/x}["ABC";1]
    "AA"
    "AB"
    "AC"
    "BA"
    "BB"
    "BC"
    "CA"
    "CB"
    "CC"
    
    q){y(x cross)/x}["ABC";2]
    "AAA"
    "AAB"
    "AAC"
    ...