# Numbers on a Chain

## Bash+coreutils, 197 bytes

for i in $(eval printf '%s\\n'${2//_/{0..9\}}|grep -vP '(\d).*\1');{
for((f=d=0;d<${#i}-$1;d++));{
((${i:d+1:1}==0||10#${i:d+1:$1}%${i:d:1}))&&f=
}
[ $f ]&&echo$i&&((c++))
}


### Output:

$./chain.sh 3 714_ 7140$ ./chain.sh 2 7141
$./chain.sh 2 14208 no answer$ ./chain.sh 3 14208
14208
$./chain.sh 2 1_208 no answer$ ./chain.sh 3 1_208
14208
$./chain.sh 2 6__2__4508 no answer$ ./chain.sh 3 6__2__4508
6132794508
$ ### Explanation • The parameter expansion ${2//_/{0..9\}} replaces all underscores with {0..9}.
• The resulting string is evaled to expand all these brace expressions.
• The grep weeds out all possibilities where there are any repeated digits.
• Then each remaining number is checked, digit-by-digit for conditions 1 and 2.

### Mathematica Ruby, 349224 229 bytes

n=$*[0].to_i r='no answer' (?0..?9).to_a.permutation($*[1].count'_'){|q|s=$*[1] q.map{|d|s=s.sub'_',d} c=s.chars (t=1 c.each_cons(n+1){|c|e=c.shift.to_i (t=!t break)if e<1||c[0]==?0||c.join.to_i%e>0} (r=s)if t)if c==c.uniq}$><<r


This is a very naive implementation. I count the number of underscores, and then simply create a list of all digit-permutations of that length, to brute force every possible combination. This will perform horribly for larger numbers of underscores, but this is code golf and not fastest-code. :)

Edit: Ported this from Mathematica. See the edit history for the original version.

## Python - 239 267

from itertools import*
T=raw_input()
n=int(T[0])
N=len(T)-2
J=''.join
for i in permutations('0123456789',N):
if all([S in[I,'_']for S,I in zip(T[2:],i)])*all([i[j]>'0'<i[j+1]and int(J(i[j+1:j+n+1]))%int(i[j])<1for j in range(N-n)]):print J(i);exit()