(Python) Implementing incrementation

4

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.

David Hewett

Posted 2013-07-26T23:14:08.100

Reputation: 57

Answers

16

3

-~i

Just good ol' unary - and bitwise-NOT.

Demo

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

arshajii

Posted 2013-07-26T23:14:08.100

Reputation: 2 142

10

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.

Johannes Kuhn

Posted 2013-07-26T23:14:08.100

Reputation: 7 122

yeah nevermind, i figured it out. im not 100% perfect with python, so i wouldnt think of using string concatenation. pretty good! – David Hewett – 2013-07-26T23:46:16.167

2Writing a good code golf challenge is hard. It is part of the [tag:code-golf] game to stretch the rules to the limit if it makes your scoring better. – Johannes Kuhn – 2013-07-26T23:48:19.333

1

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

user8777

Posted 2013-07-26T23:14:08.100

Reputation:

-1 on 33-5: len(range(-1,x))or range(x,1)[1]. The score is 27=32-5 then. – Erik the Outgolfer – 2016-07-14T16:22:33.373

0

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

arshajii

Posted 2013-07-26T23:14:08.100

Reputation: 2 142

isnt this pretty much addition? – David Hewett – 2013-07-27T19:55:01.740

@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 – 2013-07-27T20:24:12.597

the description states addition, and a sum of two numbers is the same thing as addition. – David Hewett – 2013-07-28T07:44:41.957

@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 – 2013-07-28T12:24:42.733

You can use a function that uses addition, but not a function that "does" addition, if that makes any sense. – David Hewett – 2013-07-28T19:20:30.993

3There'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 – 2013-07-28T20:15:05.257

@DavidHewett It's impossible to differentiate the two. – arshajii – 2013-07-28T22:05:12.840

0

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

Ivan Anishchuk

Posted 2013-07-26T23:14:08.100

Reputation: 111