# (Python) Implementing incrementation

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.

### 3

-~i


Just good ol' unary - and bitwise-NOT.

Demo

>>> i = 3
>>> -~i
4
>>> i = 0
>>> -~i
1
>>> i = -3
>>> -~i
-2


### 14

I'm not a python guy, so here we go:

len(' '*i+' ')


According to the docs, I don't use addition, only string concatenation and repetition.

• yeah nevermind, i figured it out. im not 100% perfect with python, so i wouldnt think of using string concatenation. pretty good! – David Hewett Jul 26 '13 at 23:46
• Writing a good code golf challenge is hard. It is part of the code-golf game to stretch the rules to the limit if it makes your scoring better. – Johannes Kuhn Jul 26 '13 at 23:48

# 17 characters

Not shortest, but another way. This time no maths or binary operators involved at all. But it only works on numbers greater than -1.

len(range(-1,x))


Proof:

>>> 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)[1]


Proof:

>>> inc = lambda x:len(range(-1,x)) or range(x,1)[1]
>>> for i in range(-2,2): print i, inc(i)
...
-2 -1
-1 0
0 1
1 2

• -1 on 33-5: len(range(-1,x))or range(x,1)[1]. The score is 27=32-5 then. – Erik the Outgolfer Jul 14 '16 at 16:22

### 10 - 5 = 5

sum((i,1))


Demo

>>> i = -3
>>> sum((i,1))
-2
>>> i = 0
>>> sum((i,1))
1
>>> i = 3
>>> sum((i,1))
4

• isnt this pretty much addition? – David Hewett Jul 27 '13 at 19:55
• @DavidHewett At it's core, it is addition. But this does not explicitly use +. The challenge description as-is seems to permit this; correct me if I'm wrong. – arshajii Jul 27 '13 at 20:24
• the description states addition, and a sum of two numbers is the same thing as addition. – David Hewett Jul 28 '13 at 7:44
• @DavidHewett So we can't use any function that, somewhere in its C implementation, uses addition? Also note that sum returns the sum of an iterable, in this case the tuple (i,1). – arshajii Jul 28 '13 at 12:24
• There's no clear distinction between a function that uses addition and one that does addition. E.g., sum() can also be used to concatenate tuples: sum([(2,3), (4,5)], ()) returns (2, 3, 4, 5). – flornquake Jul 28 '13 at 20:15

27-5=22

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


Demo:

>>> 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...)