Re: Recursion with Sequences

Posted 16 May 2014 - 10:56 AM

I just got it

I knew how to restore it. The problem was with my logic within the recursion. The returning sequence contained two elements, but you can see I only make one call to smooth(s1).remove(0), where the Sequence returned by smooth actually had 2 values.

Re: Recursion with Sequences

I noticed that too, PreDominancewants to solve this using recursion, but his last solution appears iterative.

Well, I know that this is the Java forum, but python can be excellent for pseudocode. I have written 4 separate methods that do the same thing. I have not run them thought performance tests (timeit), but I will in the near future. You can try out the python demo here is you like.

Of course, you cannot nest functions, but you can create a public (helper) method which will call a private method which will handle the recursion.

I find that it helps. if you are interested in python, to template and prototype all your logic in a typeless language, then port it back into Java.

''' baavgai's solution. '''
def interpolate4(l):
if l is not list: l = list(l)
if (len(l) < 2):
return l[:-1] # this is the only return instance we'll ever create
else:
n = l.pop(0) # pop
n2 = l.pop(0) # pop
l.insert(0, n2) # push the last pop back
r = interpolate4(l) # all self, now one number taken from top
r.insert(0, (n+n2)/2) # do the math
l.insert(0, n) # push the other number back, so we balance
return r # return the result

In the context of the OP, they have a Java collection class with limited available methods and THAT is the problem domain. Solving the program in languages more suited to it, using more flexible structures, while entertaining, doesn't seem overly helpful.