Help with developing algorithm

Hello all!

I am currently studying java in one of my engineering classes and received this project on an assignment. I was wondering if anyone would be able to help me out. Any help would be useful.

Thanks a lot!

Question:

Write a program that takes as input a numeric string (i.e. a string that consists only of the numeric digits 0 through 9) and uses recursion to answer the following problem: is there any way to add mathematical operators into the string such that the result is a valid mathematical equation?

The following strings have valid solutions:

"2317" (solution: "2*3+1=7")

"05555" (solution: "0*5*5+5=5")

"2284" (solution: "2*2=8-4")

"11112" (solution "1+11=12")

"123123" (solution: "123=123")

The following operators are valid (and have standard meaning): "+", "*", "/", "-", "%", "=". Integer division should be used for the "/" operator. Negative numbers are not allowed (so given the string "143", "-1+4=3" is not a valid solution). The "=" can only appear once in a valid solution (note that both "1*1=1" and "1=1*1" are valid solutions to the string "111"). Lastly, you may assume "left-to-right" arithmetic operations (i.e. 1+4*2-7 equals 3, since proceeding from left to right we first perform the addition, then the multiplication, then the subtraction).

Your program should take the user's input, print out whether or not the string has any valid solutions, and print out (any) valid solution if one exists.

[1521 byte] By [mtomkinsona] at [2007-9-29 13:45:21]
# 1
Your cheatin' heart... is gonna tell on you....
warnerjaa at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 2
> I was wondering if anyone would be able to help me out.Just as soon as you show us what you've got so far.
rkconnera at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 3

I might start by deciding the location of the equal sign. A string of length n has n-1 spots the equal sign can be inserted.

After that, you have two string, left and right. For each half you need to find an algorithm to place a generics operation marker.

Eg. 2111932

2111=932

L.S. = 2111

R.S. = 932

2111 -> 2o11o1

932 -> 93o2

where "o" is the generic character that represents a mathematical operation.

Then the next step is to replace each "o" with one of of your mathematical operators.

One a replacement has been done, evaluate. If true, return if false, do the next replacement iteration.

rkippena at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 4

Taking this one step further to what rkippen wrote, define a 'glue' operator, say '#'. The definition of this operator is as follows:

let a and be be two integers, then 'a#b' is the number 'ab', i.e the digits of a are simply prepended to the digits of b. This reduces your problem, given 'n' input digits d_1, d_2 ... d_n as follows:

let d_1 o_1 d_2 o_2 ... d_n-1 o_n-1 d_n be an expression where exactly one of the o_i represents the '=' operator. All other o_j != o_i represent one of +, -, *, /, %, #. The d_i represent the input digits.

Iterate over all possibilities of the o_i, and check which of them represent a valid equation.

kind regards,

Jos

JosHorsmeiera at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 5
Cross-post: http://forum.java.sun.com/thread.jsp?forum=4&thread=454322&tstart=0&trange=15
duffymoa at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 6
How much do you know about the Catalan numbers? You may find it helpful to read up on generating functions and convolutions.
YATArchivista at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...
# 7
What a sad reflection on the state of engineering education. How did I ever get any homework done without the Internet and forums? - MOD
duffymoa at 2007-7-15 4:10:14 > top of Java-index,Other Topics,Algorithms...