# Ruby, <s>113</s> 112 bytes <!--language-all:lang-ruby--> I sort of double-posted my Python answer (somehow), so here's a Ruby answer. a=->n,f,t,d{z=0;s='';n.split(s).map{|i|z=(z+d.index(i))*f};z/=f;(z,m=z.divmod(t);s=d[m]+s)while z>0;s[0]?s:d[0]} Ungolfed: def a(n,f,t,d) z=0 s='' n.split(s).each do |i| # viable because s == '' at this stage z = (z + d.index(i)) * f end z/=f while z>0 z,m = z.divmod(t) s = d[m] + s end if s[0] # if n not zero return s else return d[0] end end