Generate ordered combinations with repetition
Python 2, 56 bytes
n
is the maximum length and s
is expected to be a list of characters. It is not clear to me whether n = 0 or an empty character list are valid inputs, but this function also handles them correctly.
f=lambda s,n:n*s and s+[x+c for x in f(s,n-1)for c in s]
J, 41 char
f=.}:@;@({@(,&(<',')@(]#<@[))"1 0>:@i.@])
'ab' f 3
a,b,aa,ab,ba,bb,aaa,aab,aba,abb,baa,bab,bba,bbb
APL (Dyalog Unicode), 13 bytesSBCS
⊃,/,¨∘.,\⎕⍴⊂⍞
Try it online!
never miss an opportunity to use a scan :)
prompts for a string of "digits" and then for n
thanks @Adám for telling me how to enable ]box
on TIO