zlacker

[parent] [thread] 4 comments
1. kristo+(OP)[view] [source] 2024-06-06 06:59:11
The statements will duck type to numbers so you could just be really silly and do something like:

   return 1 + (n >= 10) + (n >= 100) + (n >= 1e3) + (n >= 1e4) + (n >= 1e5) + (n >= 1e6) + (n >= 1e7) + (n >= 1e8) + (n >= 1e9) ...
replies(1): >>schoen+Cc
2. schoen+Cc[view] [source] 2024-06-06 09:01:23
>>kristo+(OP)
Heh, that gives this nice expression

  sum(n>=10**i for i in range(100))
for (for example) inputs up to a googol. Or with your fix for the base case,

  1 + sum(n>=10**i for i in range(1, 100))
Another cute way that hides the loop

  from itertools import count, takewhile
  1 + len(list(takewhile(lambda x: 10**x <= n, count(1))))
replies(2): >>psycho+4j >>kristo+Kh2
◧◩
3. psycho+4j[view] [source] [discussion] 2024-06-06 09:54:11
>>schoen+Cc
How is that hiding the loop? I would expect takewhile to be a loop although I never used this Python facility.
replies(1): >>schoen+nn2
◧◩
4. kristo+Kh2[view] [source] [discussion] 2024-06-06 22:34:24
>>schoen+Cc
a way that follows the rules and also does what you're trying to do:

    def noloops(n):

      x = lambda n, digits: (n / 1e9, digits + (n >= 10) + (n >= 100) + (n >= 1e3) + (n >= 1e4) + (n >= 1e5) + (n >= 1e6) + (n >= 1e7) + (n >= 1e8) + (n >= 1e9))

      y = lambda n, digits: x(*x(*x(*x(*x(*x(n, digits))))))

      return y(*y(*y(*y(*y(*y(n, 1))))))[1]
Now we have it up to 324 digits without any loops.
◧◩◪
5. schoen+nn2[view] [source] [discussion] 2024-06-06 23:11:49
>>psycho+4j
All of the itertools functions are implemented using loops, but they heavily abstract over them so that users can think in terms of "streams" (or officially "iterators") without writing loop-oriented code themselves.
[go to top]