A couple of years ago, my friend wanted to learn programming, so I was giving her a hand with resources and reviewing her code. She got to the part on adding code comments, and wrote the now-infamous line,
i = i + 1 #this increments i
We’ve all written superflouous comments, especially as beginners. And it’s not even really funny, but for whatever reason, somehow we both remember this specific line years later and laugh at it together.
Years later (this week), to poke fun, I started writing sillier and sillier ways to increment i
:
Beginner level:
# this increments i:
x = i
x = x + int(True)
i = x
Beginner++ level:
# this increments i:
def increment(val):
for i in range(val+1):
output = i + 1
return output
Intermediate level:
# this increments i:
class NumIncrementor:
def __init__(self, initial_num):
self.internal_num = initial_num
def increment_number(self):
incremented_number = 0
# we add 1 each iteration for indexing reasons
for i in list(range(self.internal_num)) + [len(range(self.internal_num))]:
incremented_number = i + 1 # fix obo error by incrementing i. I won't use recursion, I won't use recursion, I won't use recursion
self.internal_num = incremented_number
def get_incremented_number(self):
return self.internal_num
i = input("Enter a number:")
incrementor = NumIncrementor(i)
incrementor.increment_number()
i = incrementor.get_incremented_number()
print(i)
Since I’m obviously very bored, I thought I’d hear your take on the “best” way to increment an int in your language of choice - I don’t think my code is quite expert-level enough. Consider it a sort of advent of code challenge? Any code which does not contain the comment “this increments i:” will produce a compile error and fail to run.
No AI code pls. That’s no fun.
First, imagine a number in JavaScript. (Bit of a nail biter here, huh?)
let i = 5
Then, we will construct an incrementor. This is really simple: here is the method.
- Make a bracket-string-centric version of
eval()
.
[]["filter"]["constructor"]("return i+1")()
- Reconstruct stringy
eval()
by using+[]
as 0,+!+[]
as 1, and implicit conversions as ways to create strings. For example, ‘false’ is(![]+[])
, so ‘f’ is(![]+[])[+[]]
.
[][ (![] + [])[+[]] + // f ([![]] + [][[]])[+!+[] + [+[]]] + // i (![] + [])[!+[] + !+[]] + // l (!![] + [])[+[]] + // t (!![] + [])[!+[] + !+[] + !+[]] + // e (!![] + [])[+!+[]] // r ][ ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o ([][[]]+[])[+!+[]]+ // n (![]+[])[!+[]+!+[]+!+[]]+ // s (!![]+[])[+[]]+ // t (!![]+[])[+!+[]]+ // r ([][[]]+[])[+[]]+ // u ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c (!![]+[])[+[]]+ // t (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o (!![]+[])[+!+[]] // r ]("return i+1")()
- Draw the rest of the fucking owl. Final code:
let i = 5; // haha yay [][ (![] + [])[+[]] + // f ([![]] + [][[]])[+!+[] + [+[]]] + // i (![] + [])[!+[] + !+[]] + // l (!![] + [])[+[]] + // t (!![] + [])[!+[] + !+[] + !+[]] + // e (!![] + [])[+!+[]] // r ][ ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o ([][[]]+[])[+!+[]]+ // n (![]+[])[!+[]+!+[]+!+[]]+ // s (!![]+[])[+[]]+ // t (!![]+[])[+!+[]]+ // r ([][[]]+[])[+[]]+ // u ([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+ // c (!![]+[])[+[]]+ // t (!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+ // o (!![]+[])[+!+[]] // r ]( (!![]+[])[+!+[]]+ // r (!![]+[])[!+[]+!+[]+!+[]]+ // e (!![]+[])[+[]]+ // t ([][[]]+[])[+[]]+ // u (!![]+[])[+!+[]]+ // r ([][[]]+[])[+!+[]]+ // n (+[![]]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+ // ' ' ([![]]+[][[]])[+!+[]+[+[]]]+ // i (+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]])+[])[!+[]+!+[]]+ // + +!+[] // 1 )() // no virus i swear. execute arbitrary code in your browser console.
Anyway, that’s just everyday JS work. It’s like step 5 after resizing the button, but a bit before centering the div.
based on this. some translation methods done differently.
- Make a bracket-string-centric version of
Conditional adder:
if x==1: return 2 else if x==2: return 3 ...
Can’t increment negative numbers or zero smh.
Fix:
if x==0: return 1 else x==1: return 2 else if x==-1: return 0 else if x==2 return 3 else if x==-2 return -1 ...
I like to shake the bytes around a little
i = ( i << 1 + 2 ) >> 1
Wait, why does it multiply by 4? (apparently addition takes precedence over bitwise operations)
My favourite one is:
i -=- 1
This is actually the correct way to do it in JavaScript, especially if the right hand side is more than
1
.If JavaScript thinks
i
contains a string, and let’s say its value is27
,i += 1
will result ini
containing271
.Subtraction doesn’t have any weird string-versus-number semantics and neither does unary minus, so
i -=- 1
guarantees28
in this case.For the increment case,
++
works properly whether JavaScript thinksi
is a string or not, but since the joke is to avoid it, here we are.
Typing on mobile please excuse.
i = 0 while i != 1: pass # i is now 1
Ah yes, the wait for a random bit flip to magically increment your counter method. Takes a very long time
The time it takes for the counter to increment due to cosmic rays or background radiation is approximately constant, therefore same order as adding one. Same time complexity.
Constant time solution. Highly efficient.
If you do it on a quantum computer, it goes faster because the random errors pile up quicker.
Finally, a useful real world application for quantum computing!