You should all be familiar with the Conway sequence (a.k.a. 'look-and-say'-sequence) by now:
1 11 21 1211 111221 312211 etc You can also start by any arbitrary number as starting point. Let f(s) be the next element of the sequence. Now for every given swe can find f(s). The reverse is not as trivial: it is not for every y possible to find the predecessor s such that f(s) = y. E.g. for y = 1 we cannot find a predecessor. But if y has an even length you can divide it into pairs of digits which describe each one part of a predecessor:
513211 divides in 51,32,11 so: 51 comes from 11111 32 comes from 222 11 comes from 1 put together: 111112221 So this way there we can define an unique predecessor for every y of even length.
Note: The 'predecessor' s defined this way does generally NOT satisfy f(s) = y.
Goal
Write a function / program snippet that accepts a string of digits as input that
- calculates the next element of the Conway sequence if the length of the input string is odd
- calculates the predecessor of the input string as defined above if the input string length is even.
Shortest code in bytes wins.
Recent Questions based on the look-and-say sequences: