# Generate all words of a given size from a fixed alphabet

**URL:** <https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949>\
**Category:** General Usage\
**Created:** [February 18, 2019, 7:43pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949 "2019-02-18T19:43:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanhevg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tanhevg/32/12025_2.png) [@tanhevg](https://discourse.julialang.org/u/tanhevg)\
**Post date:** [February 18, 2019, 7:43pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/1 "2019-02-18T19:43:04Z")

</div>

Hello,

Say I have an alphabet of n symbols, and I want to generate all words of length k that can exist in that alphabet. What is the concise way to do that in Julia? I am expecting a list of n^k words. Writing k nested for loops looks a bit dull. This sounds something that should be a one-liner with `Combinatorics.jl`, but I can’t find a decent doc for it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 18, 2019, 8:06pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/2 "2019-02-18T20:06:41Z")

</div>

Here’s a simple way. No need for `Combinatorics` even, just plain `Iterators`.

```julia
julia> alphabet = 'a':'d';

julia> k = 2;

julia> join.(collect(Iterators.product(ntuple(_ -> alphabet, k)...))[:])
16-element Array{String,1}:
 "aa"
 "ba"
 "ca"
 "da"
 "ab"
 "bb"
 "cb"
 "db"
 "ac"
 "bc"
 "cc"
 "dc"
 "ad"
 "bd"
 "cd"
 "dd"

```

---

<div class="post-metadata">

**Author:** ![fnin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnin/32/23300_2.png) [@fnin](https://discourse.julialang.org/u/fnin)\
**Post date:** [February 18, 2019, 8:49pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/3 "2019-02-18T20:49:37Z")

</div>

This is a bit more verbose, but gives you a lazy iterator (similar to Combinatorics.jl types) for this problem.

```julia
struct WithReplacementOrderedCombinations{T}
    itr::T
    count::Int64
    itrsize::Int64
    function WithReplacementOrderedCombinations(itr::T,count::Int) where T
        new{T}(itr,Int64(count),length(itr))
    end
end

function Base.iterate(c::WithReplacementOrderedCombinations{T},state::Int64=0) where T
    if state>=length(c)
        return nothing
    end
    indices=digits(state,base=c.itrsize,pad=c.count)
    T([c.itr[i] for i in (indices .+1)]),state+1
end

function Base.length(c::WithReplacementOrderedCombinations)
    length(c.itr) ^ c.count
end

julia> alphabet = "abcd";

julia> k = 2;

julia> collect(WithReplacementOrderedCombinations(alphabet,k))
9-element Array{Any,1}:
 "aa"
 "ba"
 "ca"
 "ab"
 "bb"
 "cb"
 "ac"
 "bc"
 "cc"

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 18, 2019, 9:14pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/4 "2019-02-18T21:14:47Z")

</div>

`Iterators.product` is actually already lazy. I collected it above since it sounded like that’s what OP wanted, but we can just as well iterate over it lazily:

```julia
alphabet = 'a':'d' # or "abcd"
k = 2

itr = Iterators.product(ntuple(_ -> alphabet, k)...)

for word in Base.Generator(join, itr)
    println(word)
    word == "bb" && break
end

```

Output:

```julia
aa
ba
ca
da
ab
bb

```

---

<div class="post-metadata">

**Author:** ![fnin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnin/32/23300_2.png) [@fnin](https://discourse.julialang.org/u/fnin)\
**Post date:** [February 19, 2019, 11:39am UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/6 "2019-02-19T11:39:27Z")

</div>

Thats neat! I didn’t know that.

---

<div class="post-metadata">

**Author:** ![tanhevg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tanhevg/32/12025_2.png) [@tanhevg](https://discourse.julialang.org/u/tanhevg)\
**Post date:** [February 19, 2019, 2:46pm UTC](https://discourse.julialang.org/t/generate-all-words-of-a-given-size-from-a-fixed-alphabet/20949/7 "2019-02-19T14:46:58Z")

</div>

Thanks!
