for something using recursivity (no loops) to go through every entries of my table. Can you help me?

(The code isn't complete, I know I have to use if charAt(i) == "x" or if charAt(i) == "+")

Thanks!

12-12-2012, 04:53 PM

devnull69

So you will have to write a mathematical parser essentially? Should it be able to cope with operator priority? This would really be a big deal ...

Or maybe I didn't understand your requirement? Do you really have lots of table cells with mathematical expressions that you need to parse/evaluate into a single integer result per cell?

12-12-2012, 07:35 PM

felgall

That is definitely a task better handled by two nested loops - one for the table rows and one inside it for the cells in the row.

There is no recursive solution that would map in any meaningful way to the structure of the data so while it would be possible to write a less efficient recursive version of the code it would not only use a lot more memory and run slower - it would also be much harder to maintain.

You should only use recursion where it maps to the task in a meaningful way.

I can't use neither eval or dynamic programming... The key of this exercise is to use recursion.

12-13-2012, 12:04 AM

devnull69

As this is supposed to be your exercise, I can only give you more hints

1 - Let's hope that you don't have to take care of operator priority (so you can just go right to left)
2 - call your function with the current string
3 - Start from the right of the current string, extract the number and the operator and use it for the expression like "number operator yourfunction(restofstring)". If there is no operator you end the recursion and return the number

Thanks Devnull69, I almost got it! :) I have a tiny problem left, how can I extract the 1? When I try with parseInt(s) it automatically takes 14...
I tried to extract it with s[i] where i = s.length-1 but it won't work if the number has more than 1 number (like 10)...and I don't even know if it's supposed to work with a string... Thanks for your help again!