0

I'm excited to ask my first question here. I've run into some problems with recursion. I am coding with Python.

I am using a dictionary to store previously solved numbers in a mathematical sequence. I then check this dictionary when solving later sequences to make the program run faster. Each key represents solved 'starting numbers,' the value represents how long it took the sequence to take the starting number and take it down to 1 using a particular formula.

I'm solving the problem using recursion, which itself is very easy but there's more to it for me.

Each recursive step gives me the opportunity to update a different key in the library, but I am unable to find a way to do this.

So where a sequence might look like 13 -> x -> y -> z ... -> 1, what would you do to not only update the 13 key with value, but also the x, y, x values in the dictionary during the same recursive path? Right now I'm only able to update one number per sequence.

cache = {1:1}

def solve(number):

    if cache.has_key(number):
        return cache[number]
    else:           
      if condition one.. 
            return 1 + solve(number * formula 1)
      else condition two...
            return 1 + solve(number * formula 2)

for x in xrange(1,1000):

cache[x] = solve(x)   <-- right now only cache[x] is being updated. 

Thank you!

Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
Jake Way
  • 5
  • 3

3 Answers3

1

Couldn't you just put it in cache before returning it for each recursive call?

instead of:

return 1 + solve(number * formula 1)

you'd do:

result = 1 + solve(number * formula 1)
cache[number] = result
return result

Or are you after something different?

Mattias Nilsson
  • 3,426
  • 1
  • 20
  • 27
0

In principle the dictionary.update() method is able to update several values of a dictionary in one shot. But I assume your problem lies in the fact, that you update cache[x] only in the caller, i. e. outside of the recursion.

guidot
  • 4,534
  • 2
  • 20
  • 35
0

You can update another parameter of solve to contain all of the numbers in the sequence, like

def solve(number, valueList):
...
...
if condition one:
        valueList.append(number)
        return 1 + solve(number * formula 1, valueList)

etc. I'm assuming that what you mean by "the numbers in the sequence" are the ones that meet condition 1 or 2 before being accepted.

If you don't have a problem with Mattias' answer, his is more straightforward. I'm assuming that for whatever reason, you don't want to add to the cache until the recursion finishes.

Anyway, when the recursion finishes, you can iterate through valueList and add it to cache.

vroomfondel
  • 2,886
  • 1
  • 18
  • 31
  • Thank you so much, I just posted this yesterday and already have the answer to my question. Mattias answer was what I was looking for, I didn't realize I could use return/result in this way. – Jake Way Jul 05 '13 at 05:39
  • @JakeWay you should accept his answer then! Glad to hear you figured it out. – vroomfondel Jul 05 '13 at 07:02