Challenge: Implement incrementation without addition, multiplication, exponentiation, and any higher binary operators (and their opposites) in Python. Winner is the person who can do it in the least amount of code. You score 5 characters less if you can do it for negative numbers and without unary operators.
Not shortest, but another way. This time no maths or binary operators involved at all. But it only works on numbers greater than -1.
>>> inc = lambda x: len(range(-1,x)) >>> inc(7) 8 >>> inc(100) 101
28 = 33-5 characters
Definitely out of contention but this does it for all positive and negative integers, again no math, no unary tricks.
len(range(-1,x)) or range(x,1)
>>> inc = lambda x:len(range(-1,x)) or range(x,1) >>> for i in range(-2,2): print i, inc(i) ... -2 -1 -1 0 0 1 1 2
It's not really very short, but it doesn't use len(), range(), sum() and other stuff which definitely has a lot of increments and additions in it. Works with negative numbers, and no unary operations. Only pure, natural, raw, organic bitwise magic :)
j=i k=1 while i<=j:i^=k;k<<=1
>>> def inc(i): ... j=i ... k=1 ... while i<=i:i^=k;k<<=1 ... return i ... >>> inc(1) 2 >>> inc(2) 3 >>> inc(20) 21 >>> inc(-24) -23 >>>
(UPD: Sorry, I'm not sure whether newlines are counted as chars, if yes, it should be 29 not 27...)