Please look at the following link for the problem

http://geeksforgeeks.org/forum/topic/yahoo-interview-question-for-software-engineerdeveloper-fresher-about-algorithms-9

#!/usr/bin/env ruby

#The recursive function to find out all the expressions which evaluate to a given value

#The technique used is "backtracking"

def findExpr(s, expr, k)

if s.length != 0

expr += s[0].chr

val = eval(expr)

if val == k && s.length == 1

print expr, "\n"

elsif val < k

#The operations supported are '*', '+' and '.'

#where '.' is simple concatenation of the digits

findExpr(s[1..-1], expr + '*', k)

findExpr(s[1..-1], expr + '+', k)

findExpr(s[1..-1], expr, k)

end

end

end

findExpr("123456789", "", 2097)

### Like this:

Like Loading...

*Related*

Plz explain the logic in other coding formats like c, c++ or java.