@rolf: I am assuming that f is a function from an integer to an integer in the mathematical/pure sense. That is to say that running f on an int on some disconnected computer, taking the integer output and passing it through another computer with the same algorithm should make absolutely no difference.
An easy solution is to map positive normal numbers into a series of ones (ie 5 to 11111). Then a series of ones to negative that number. A negative normal number to a a series of ones with a negative sign. And a series of ones with a negative sign to positive normal numbers.
def f(n):
if n == 0: return 0
if n > 0:
if str(n).replace("1", "") == "":
return -len(str(n))
else:
return int("1"*n)
else:
if str(-n).replace("1", "") == "":
return len(str(-n))
else:
return -int("1"*(-n))
for n in range(0,10):
print n, "\t\t", f(n), "\t\t", f(f(n)), "\t\t", f(f(f(n)))
Which outputs:
0 0 0 0
1 -1 1 -1
2 11 -2 -11
3 111 -3 -111
4 1111 -4 -1111
5 11111 -5 -11111
6 111111 -6 -111111
7 1111111 -7 -1111111
8 11111111 -8 -11111111
9 111111111 -9 -111111111
It works except for 1, because it's confused for a series of 1 of length one. Luckily, all other digits other than one work.
def f(n):
if n == 0: return 0
if n > 0:
if str(n).replace("2", "") == "":
return -len(str(n))
else:
return int("2"*n)
else:
if str(-n).replace("2", "") == "":
return len(str(-n))
else:
return -int("2"*(-n))
for n in range(0,10):
print n, "\t\t", f(n), "\t\t", f(f(n)), "\t\t", f(f(f(n)))
NOTHING IS IMPOSSIBLE :D