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.

  • 18107@aussie.zone
    link
    fedilink
    arrow-up
    6
    ·
    edit-2
    1 day ago

    I decided to use NAND instead of NOR, but it’s effectively the same thing.

    Scala:

    //main
    @main
    def main(): Unit =
      var i = 15 //Choose any number here
      i = add(i, 1) //this increments i
      println(i)
    
    //Adds 2 numbers in the most intuitive way
    def add(a: Int, b: Int): Int =
      val pairs = split(a).zip(split(b))
      val sumCarry = pairs.scanLeft(false, false)((last, current) => fullAdder(current._1, current._2, last._2))
      return join(sumCarry.map(_._1).tail.reverse)
    
    //Converts an integer to a list of booleans
    def join(list: Seq[Boolean]): Int = BigInt(list.map(if (_) '1' else '0').mkString, 2).toInt
    
    //Converts a list of booleans to an integer
    def split(num: Int): Seq[Boolean] = num.toBinaryString.reverse.padTo(32, '0').map(_ == '1')
    
    //Adds 2 booleans and a carry in, returns a sum and carry out
    def fullAdder (a: Boolean, b: Boolean, c: Boolean): (Boolean, Boolean) =
      (NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), c)), NAND(NAND(NAND(NAND(a, NAND(a, b)), NAND(NAND(a, b), b)), c), NAND(a, b)))
    
    //The basis for all operations
    def NAND(a: Boolean, b: Boolean): Boolean = !a || !b
    

    EDIT: replaced Integer.parseInt with BigInt(...).toInt to fix NumberFormatException with negative numbers.

    try it online here