Seven's Cycle Sum Sequence

Python 2, 69 bytes

lambda n:[''.join('02'[x>'0']for x in`n`)+'0',n][set(`n`)<=set('05')]

The function is simple to describe:

  • If n consists of only 0's and 5's, output it unchanged.
  • Otherwise, replace each digit of n with 2, except 0 stays 0, and tack on a 0 to the end.

The golfing can be improved, I'm mostly posting to share the method. A language with native regex should allow a short solution.

An alternative statement of the function is

  • In n, replace each digit with a 5, except 0 stays as 0
  • If this changed n (it had a digit other than 0 or 5), multiply the result by 4

Python 2, 63 bytes

lambda s:s.strip('05')and''.join(`(c>'0')*2`for c in s)+'0'or s

Input argument is expected to be a string.


CJam, 16 bytes

Using the same algorithm as everyone else:

r_50s-{:~2fe&0}&

Test suite. (Generates all results from 1 to the input.)

Explanation

r_      e# Read input and duplicate
50s     e# Push the string "50".
-       e# Remove all '5' and '0' characters from the input.
{       e# If any characters remained in the input...
  :~    e#   Evaluate each digit character to turn it into an integer.
  2fe&  e#   Map (&& 2) over the list. Due to short-circuiting, zeros remain zeros and
        e#   everything else becomes 2.
  0     e#   Push a trailing zero.
}&