• Barley_Man@sopuli.xyz
    link
    fedilink
    English
    arrow-up
    24
    ·
    2 days ago

    oooooooooooooooooooooooooooooooo

    00000000000000000000000000000000

    88888888888888888888888888888888

    oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

    • Arthur Besse@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      21
      ·
      edit-2
      1 day ago

      python -c 'import time as t; a="o"; all( (print(a), a:=a.replace(*["o","O","8","oo"][i%3:i%3+2]), t.sleep(max(.3,1-i/50))) for i in range(60))'

      edited to make it stop after 1MB; notes here:

      someone asked in a reply:

      Does that loop infinitely

      The first version I posted would loop infinitely… if you have infinite RAM, that is 🫠 (the length of the string will reach 1KB after 30 iterations, and 1MB after 60, 2MB after 63, and so on). Also, to keep the loop in a single line I had foolishly used a list comprehension which meant each previous iteration was also being retained.

      Fortunately the rate of memory consumption is not too fast because python string replacement is very slow, but, thanks to your question making me think about it, to avoid eventually crashing someone’s computer if they don’t know to hit ctrl-c to kill it, i’ve now edited it so that it will stop after 60 iterations. I also made it use all() to consume a generator comprehension instead of a list comprehension, to avoid retaining the state of previous iterations.

      here is my very inefficient list-comprehension-using original version which will run until it runs out of memory:

      python -c 'import itertools as I,time as t;a="o";[(print(a),a:=a.replace(*["o","O","8","oo"][i%3:i%3+2]),t.sleep(max(.3,1-(i/50))))for i in I.count()]'

      if you leave this version running long enough, you will be at the mercy of your operating system’s out-of-memory-killer: if it decides to kill other things before it kills this python process you might have a bad time.

      here is another version which will actually loop infinitely without consuming more RAM:

      python -c 'import itertools as I,time as T; all((any(print(["o","O","8"][i%3],end="")for _ in range(2**(i//3))),print(),T.sleep(max(.3,1-i/50)))for i in I.count())'

      this is technically not completely constant-space because i and 2**(i//3) are still growing… but it will run for a very very very long time before it needs to allocate a small amount more.

      I’m leaving the space-inefficient now-not-infinite one at the top of this comment because using replace() is easier to read than this nested loop version.