4
\$\begingroup\$

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.

\$\endgroup\$
16
\$\begingroup\$

3

-~i

Just good ol' unary - and bitwise-NOT.

Demo

>>> i = 3
>>> -~i
4
>>> i = 0
>>> -~i
1
>>> i = -3
>>> -~i
-2
\$\endgroup\$
10
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ yeah nevermind, i figured it out. im not 100% perfect with python, so i wouldnt think of using string concatenation. pretty good! \$\endgroup\$ – David Hewett Jul 26 '13 at 23:46
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Johannes Kuhn Jul 26 '13 at 23:48
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ -1 on 33-5: len(range(-1,x))or range(x,1)[1]. The score is 27=32-5 then. \$\endgroup\$ – Erik the Outgolfer Jul 14 '16 at 16:22
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ isnt this pretty much addition? \$\endgroup\$ – David Hewett Jul 27 '13 at 19:55
  • \$\begingroup\$ @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. \$\endgroup\$ – arshajii Jul 27 '13 at 20:24
  • \$\begingroup\$ the description states addition, and a sum of two numbers is the same thing as addition. \$\endgroup\$ – David Hewett Jul 28 '13 at 7:44
  • \$\begingroup\$ @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). \$\endgroup\$ – arshajii Jul 28 '13 at 12:24
  • 3
    \$\begingroup\$ 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). \$\endgroup\$ – flornquake Jul 28 '13 at 20:15
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.