logoalt Hacker News

xg15last Friday at 11:30 AM5 repliesview on HN

> Now, this is a time-memory tradeoff, but my time on this earth is limited so I decided to meta-program the if statements using a programmer program in a different programming language.

  for i in range(2*8):
    if i % 2 == 0:
No comment...

Replies

_ache_last Friday at 11:33 AM

Yeah... I come here to talk about that. Should have been

  for i in range(0, 2**8, 2):
      print("    if (number == "+str(i)+")")
      print("        printf(\"even\\n\");")
      print("    if (number == "+str(i + 1)+")")
      print("        printf(\"odd\\n\");")
or

  for i in range(0, 2**8, 2):
      print(f"""    if (number == {i})
          puts("even");
      if (number == {i + 1})
          puts("odd");""")
show 1 reply
PurpleRamenlast Friday at 12:06 PM

I think we can improve this. Just make a microservice who generates the code on the fly and streams it to the compiler. Then you also just have to create the necessary code and don't waste the SSD with unused code-paths.

show 1 reply
cowsandmilklast Friday at 11:33 AM

How horridly inefficient, he should have just flipped a Boolean on each iteration.

show 2 replies
layer8last Friday at 2:12 PM

It should have used a flag that is being toggled.

catigulalast Friday at 4:23 PM

Claude's version:

  even, odd = "even", "odd"
  for i in range(2\*32):
      print(f'    if (number == {i}) puts("{even}");')
      even, odd = odd, even
As usual, a non-marginally superior mind to commentators.