I have a function that performs perfectly iteratively (non-recursively, with a loop). What it does, is it takes the average of adjacent elements in a Sequence, and returns them in a new Sequence. In other words..

Seq = {1, 3, 5}

return = {(1+3)/2, (3+5)/2}

return = {2, 4}

Here's the implementation iteratively.

private static Sequence<Integer> smooth(Sequence<Integer> s1) { Sequence<Integer> retSeq = s1.newInstance(); Integer copy = null; for (int i : s1) { if (copy == null) { copy = i; continue; } retSeq.add(retSeq.length(), (copy + i) / 2); copy = i; } return retSeq; }

Here're the methods available.

I have two separate attempts at it, neither of them work. Posting the results.

private static Sequence<Integer> smooth(Sequence<Integer> s1) { Sequence<Integer> retSeq = s1.newInstance(); Sequence<Integer> copy = s1.newInstance(); for (int i : s1) { copy.add(copy.length(), i); } if (s1.length() == 2) { int firstElement = copy.remove(0); int secondElement = copy.remove(0); p("2 elements left, removing: " + firstElement + " and " + secondElement + "."); retSeq.add(retSeq.length(), (firstElement + secondElement) / 2); } else { int firstElement = s1.remove(0); int secondElement = copy.remove(1); p("Removing: " + firstElement + " and " + secondElement + "."); int firstSmoothed = (firstElement + secondElement) / 2; int smoothedNumber = smooth(s1).remove(0); p("Smoothed Number: " + smoothedNumber + "."); s1.add(0, firstElement); retSeq.add(0, firstSmoothed); retSeq.add(retSeq.length(), smoothedNumber); } p("Returning: " + retSeq.toString()); return retSeq; }

private static Sequence<Integer> smooth(Sequence<Integer> s1) { Sequence<Integer> retSeq = s1.newInstance(); int tmp = 0; int tmp2 = 0; if (s1.length() == 2) { tmp = s1.remove(0); tmp2 = s1.remove(0); retSeq.add(retSeq.length(), (tmp + tmp2) / 2); } else { int lastDigit = s1.remove(s1.length() - 1); int nextDigit = s1.remove(s1.length() - 1); retSeq.add(retSeq.length(), (lastDigit + nextDigit) / 2); s1.add(s1.length(), nextDigit); int test = smooth(s1).remove(0); retSeq.add(retSeq.length(), test); s1.add(s1.length(), lastDigit); } return retSeq; }

Any help?

This forum doesn't allow images? Weird.

I also can't seem to edit my post. Sorry guys .~.