#esoteric-python
1 messages ยท Page 83 of 1
on topic enough imo
Aha!py [*map(a.__rshift__, range(len(bin(a)) - 2))]
Making it more exotic
and shorter
is it shorter lol
you can initialize prev with a dummy loop
how do I execute an ast tree after I've modified it?
compile() it and then exec()/eval() it
is it possible to use ast to replace print@"Hello" with print("Hello")?
my current attempt is:
import ast
tree = ast.parse(input())
class test(ast.NodeTransformer):
def visit_BinOp(self,node):
if node==ast.MatMult:
return ast.call(func=node.left, args=[node.right])
exec(compile(ast.fix_missing_locations(test().visit(tree)), filename="<ast>", mode="exec"))
yes
In [46]: curse(type(print), '__matmul__', lambda s,e: s(e))
In [47]: print@"Hello"
Hello
``` you can use forbiddenfruit.curse (though you have to do the same for `type(lambda:0)`
that's different, you're messing with the objects at runtime
not a nerd is using the ast
yeah I was trying to use ast
I changed it to if type(node.op)==ast.MatMult: and it still didn't work
nvm I got it working
import ast
tree = ast.parse(input())
class test(ast.NodeTransformer):
def visit_BinOp(self,node):
if type(node.op)==ast.MatMult:
return ast.Call(func=node.left, args=[node.right], keywords=[])
exec(compile(ast.fix_missing_locations(test().visit(tree)), filename="<ast>", mode="exec"))
is it possible to make a@b@c equivelent to a(b,c) using ast?
maybe you could do partial on function if it takes more args than one, until it only takes one. Then you run into trouble with variadic functions though
actually it'd probably be better to turn it into a(b(c))
Is it possible to make a class that when called given N, returns either an instance of the class or a tuple with N distinct instances of the class?
What I mean is:
a = Thing(1) # a is an instance of Thing
a, b = Thing(2) # a and b are distinct instances of Thing
I think you can mess with __new__ and metaclasses to do that but a better way would be using classmethods. Like Thing.from_multiple(3) etc. and then initializing 3 different copies of Thing
Alternatively you can do that via __mul__
!e ```py
class Thing:
def mul(self, multiplier):
return tuple(Thing() for _ in range(multiplier))
a = Thing()
print(a)
b, c = Thing() * 2
print(b, c)
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
001 | <__main__.Thing object at 0x7f8832e729d0>
002 | <__main__.Thing object at 0x7f8832e09610> <__main__.Thing object at 0x7f8832e11e50>
I second isidential's idea of having a classmethods
I went with a classmethod, except I overwrote __class_getitem__() for maximum jank.
Ergo,
a = Thing[1]
a, b = Thing[2]
Since this is not a very serious use case, I can afford to mess with syntax a little.
And that's about it! My project is complete:
And here's this as text:
>>> from no_strings import BFInterpreter
>>> _,__,___ = BFInterpreter[3]
>>> _.interpret(+_[+__[___<<___<+___>>___>>___]+__<-__<-__<<__<+__<++__]<<_._<++_._<++_._._+++_._<<++_._<---_._>>_._>_._+++_._------_._>-_._>>--_._)
Source code: +[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--.
Output: Hello, World!
I think I made a thing like that in the past ๐ค
But mine uses << and >> instead of < and >, so yours is superior.
Why are you using 3 different objects, though?
l1, indices = [a >> i for i in range(len(bin(a)) - 2)], [i for i, s in enumerate(bin(a)[2:]) if s == '1']```
@crystal mica i know its yesterday's topic but the indices list u built is wrong, may be rather something like this python from math import ceil, log a = 7 print([a >> i for i in range(ceil(log(a, 2)))], [i for i in range(ceil(log(a, 2))) if (a >> i) % 2 == 0])
out : [7, 3, 1] []
your code would out : ([7, 3, 1], [0, 1, 2])
@formal sandal There's a cornercase where _>_>_[_>_] is parsed as >[>>] due to the method I used to fix comparison chains breaking things
The fix for the cornercase is to have a separate object for each layer of loops
And here's this as text:
@earnest wing
could you explain how that worked? Like how did those symbols even output smth, and why did they output Hello, World!
It's a brainfuck interpreter, but instead of taking source code as a string ("+-<>[],."), it takes source code as valid python expressions.
well how does that expression translate to Hello World
+_[+__[___<<___<+___>>___>>___]+__<-__<-__<<__<+__<++__]<<_._<++_._<++_._._+++_._<<++_._<---_._>>_._>_._+++_._------_._>-_._>>--_._
=>
"+[+[<<<+>>>>]+<-<-<<<+<++]<<.<++.<++..+++.<<++.<---.>>.>.+++.------.>-.>>--."
umm
It's brainfuck code.
^
i know @snow beacon but I'd like to understand it
But how do those symbols translate to letters
AFAIK the first parts in the brackets set up a bunch of cells to a certain amount close to the alphabet, then the rest offsets and outputs the values.
Every . is outputting one character.
Is there some reference for that? like how'd one even discover that?
I'm guessing by readig about the BFInterpreter?
https://esolangs.org/wiki/Brainfuck Here's how the language itself works.
I'm like curious, the guy who wrote that code originally, how did he actually do it
It might be on Rosettacode or something.
ah
Rosettacode?
oh it's a website I just checked
ah that's a very good article
The source of the program, I believe: https://codegolf.stackexchange.com/questions/55422/hello-world/68494#68494
explains the Hello World code too
Since all commands for BF are valid characters in Python source code, it's possible to make an implementation like this by using a class that overrides many of the builtin methods (x.__add__(y) for x + y, etc.)
@low acorn aha, that's a good catch!
Why are you all doing this brainfuck! # coding trick exists!
@marsh void # coding tricks?
When you change the encoding of a file to a custom python decoder so that you can use whatever syntax you want.
You know how you can do #coding=utf8?
Or something similar, I don't know exactly.
You can define your own encoding.
# coding: omg_braces
def main() {
print('What a twist!');
}
main();``` you can make this work
screaming
depends on what y is I guess
print(...)
do you want to print something if a condition is true?
print(a*x,end='') (if x is a boolean, and a is the thing you want to print)
I swear that's longer
๐
it's longer by 1 char I'm, pretty sure
x and print(a) is probably the best solution
yeah
although there might be a better one in specific edge cases
e.g.?
if you were already printing without a newline so you already has ,end=''
for an example
ah
probably been done before but still:
class PrintDict(dict):
def __getitem__(self, item):
print(f"get {repr(item)}")
return super().__getitem__(item)
def __setitem__(self, item, value):
print(f"set {repr(item)} = {repr(value)}")
return super().__setitem__(item, value)
class PrintMeta(type):
@classmethod
def __prepare__(cls, name, bases, **kwds):
return PrintDict()
class SomeClass(metaclass = PrintMeta):
foo = "bar" # yup, this prints
my_locals = locals() # this is actually a live copy of the namespace, observe:
my_locals["foo"] = "baz"
assert foo == "baz" # runs just fine
prints
get '__name__'
set '__module__' = '__main__'
set '__qualname__' = 'SomeClass'
set 'foo' = 'bar'
get 'locals'
set 'my_locals' = {'__module__': '__main__', '__qualname__': 'SomeClass', 'foo': 'bar'}
get 'my_locals'
set 'foo' = 'baz'
get 'foo'
for those too lazy to copy / paste
That's pretty cool
prints a blank line tho
oh yeah nvm
also my x is actually a set
not a bool
is there any shorthand for x.lower()?
cause it's longg
Nope
not really
ooof
I've seen worst tbh
although if you use it multiple times you could put it in a variable (str.lower)
nope
only once
but it's a substantial chunk of my 78 chars
import sys is quite a few actually
could you send your code?
import sys
for i in sys.argv:{*map(chr,range(97,123))}-{*i.lower()}or print(i)
prints each arg if it contains every letter in the alphabet
could you just use regular input() instead?
import sys
[print(i)for i in sys.argv if {*map(chr,range(97,123))}-{*i.lower()}]
nvm that's longer
u can save 1 char with no whitespace after if
isn't string.ascii_letters in both cases?
?
the set will always have uppercase letters in it
so it'll either never print or always print
no cause ASCII 97-122 is just lowercase
oh yeah nvm
couldn't you change it to map(chr,range(65,91)) then use .upper() instead?
1 char shorter
you could try the inverse:
not 2847-sum(map(ord,{*i.lower()})) will only be true if it uses all letters
sadly that won't work if it has any characters like . or
which it does
it works if you filter with isalpha
but that's substantially longer
anyways I think I'd rather try to shorten it by myself now
thanks for the help though!
shortest way to reverse digits of an int?
str(i)[::-1] is pretty short
trouble is I need it back as an int
hmm
which is longg
int(str(i)[::-1])?
slightly long tho
i can't really think of any way to do it shorter
from math import ceil,log
def reverse(n):
return sum(n//10**j%10 * 10**k for j,k in enumerate(range(ceil(log(n, 10)) - 1, -1, -1)))
who needs strings
people who want it short
didn't need the dummy first loop
now it's short
even shorter
can't do better than that
reverse=lambda n:sum(n//10**(d-1-j)%10*10**j for j in range(d))if(d:=ceil(log(n,10)))else 0
challenge: golf a sorting algorithm (without using the builtin)
(function/program that takes an array of numbers and sorts it)
def g(l):
p=0
while p<len(l):
if p==0 or l[p]>=l[p-1]:
p+=1
else:
l[p],l[p-1]=l[p-1],l[p]
p-=1
return l
here's a terrible and very golfable gnome sort for example
sure
wait a little bit
f=lambda a:(lambda b:f(a)if b!=sorted(b)else b)(__import__("random").sample(a,len(a)))
this is it
bogosort
that uses the builtin though
'(without using the builtin)'
also bogosort isn't allowed because it's kinda cheating
how about: your algorithm has to be able to work with an array of 20 elements in under 10 seconds
your solution still uses sorted(b) so it's invalid
from random import shuffle
sort = lambda array: [array.insert(i,array.pop(array.index(min(array[i:]))))for i in range(len(array))]
arr = [*range(100)]
shuffle(arr)
sort(arr)
print(arr)
Don't like all the methods but fairly short
def srt(a):
o=[]
while a:o+=[min(a)];a.remove(min(a))
return o
```by no means the shortest, but quite short
why didnt you do it oneliner
Because it would be longer
You might be able to shorten it using .pop()
Need [], otherwise the += will not work
srt = lambda a:[a.pop(a.index(min(a)))for _ in a[:]]
```turns out, oneline was the way
Ah, clever
unfortunately remove is too many letters:
srt = lambda a:[b for _ in a[:]if a.remove(b:=min(a))or 1]
srt = lambda a:[a.remove(b:=min(a))or b for _ in a[:]]
```still longer, but not as much
is there any way to write numbers more concisely?
compared to?
writing them like 1800
so you want 1800 in a more concise way?
at least for relatively large numbers it would be shorter to write them as mathematical things
maybe not as small as 1800
you have the mathematical notation for floats
which is?
>>> 1.8e3
1800.0
you can write numbers greater than 999999 in hex
I mean that works
could go hex or octal for something shorter
https://docs.python.org/3/reference/lexical_analysis.html#integer-literals your options with literals
ta
j has the coolest syntax I've seen
420blazeit -> 274765352357189
base in decimal, "b", the digits 0-9a-z
should be asymptotically short af
you can do int(string,b) to convert a string from base b to decimal
oh yeah, same thing doesn't go past 36 though
also implementing that J base thing in ast could be cool
you got me very confused with the j thought you meant python complex at first
haha
wondering how on earth that works
I didn't even know python had complex numbers until I bruteforced all valid syntax 3 char strings
is there any golfing use for complex numbers?
makes sense to include it considering python's use with data analysis
if you ever have a challenge that needs 2-d vectors
using complex numbers is often easier
It probably does -(0+1j) -> (-0-1j)
In [64]: -0j.real
Out[64]: -0.0
In [65]: -0-0j.real
Out[65]: 0.0
the floating point standard brings that in
this line looks mildly esoteric
>>> -0.*-0.
0.0
The floating point standard peaked at Float3. It allows for:
NaN
-NaN
Inf
-Inf
0.0
-0.0
1.0
-1.0
!
I presume you've seen NaN Gates and Flip-FLOPS?
I've got to be honest, I hate floats.
I was pretty interested in posits for representing numbers a while back; the standard allows a 2-bit posit that can represent {0, 1, -1, complex infinity}.
To be honest I can't think of any other numbers anyone could want.
@marsh void #coding is not very modular for the (very common) scenario in which one wishes to interpret stringless bf in the middle of an otherwise normal script
n = int(input())
wave = list(map(int, input().split()))
up = True
down = False
for i in range(n - 1):
if up and wave[i] > wave[i+1]:
wave[i], wave[i+1] = wave[i+1], wave[i]
elif down and wave[i] < wave[i+1]:
wave[i], wave[i+1] = wave[i+1], wave[i]
up, down = down, up
print(wave)
How would you reduce the code to baar? I know it's essentially
(lambda n, wave, up, down : Code here) (int(input()), list(map(int, input().split())), True, False)
But like how do I get the whole for loop with the 2 conditions in baar
you can remove the variables up and down and use i%2 instead
n = int(input())
wave = list(map(int, input().split()))
for i in range(n - 1):
if i%2==0 and wave[i] > wave[i+1]:
wave[i], wave[i+1] = wave[i+1], wave[i]
elif i%2==1 and wave[i] < wave[i+1]:
wave[i], wave[i+1] = wave[i+1], wave[i]
print(wave)```
since wave[i], wave[i+1] = wave[i+1], wave[i] and wave[i], wave[i+1] = wave[i+1], wave[i] are the same you can turn that if/elif into just one condition
n = int(input())
wave = list(map(int, input().split()))
for i in range(n - 1):
if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1]):
wave[i], wave[i+1] = wave[i+1], wave[i]
print(wave)```
now to 1-line it...
actually let's 1-line wave[i], wave[i+1] = wave[i+1], wave[i] first since that's the hard part
oh wow, yeah
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i])
and now...
(lambda n,wave:[(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])])``` i think this should work
wait no whoops
(lambda n,wave:[(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])])(int(input()),list(map(int,input().split())))```
wait can you explain how that setitem thing worked please?
a[b]=c is the same as a.__setitem__(b,c)
__setitem__ is the dunder method that gets called when you assign an item in a list
technically it's not the same if you override __getattribute__ but most sane classes, including list, don't do that so yeah
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i])
so then, (lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) translates to
((lambda x,y: (wave[i] = x, wave[i+1] = y))(wave[i+1], wave[i]) yes?
yes
which is (wave[i] = wave[i+1], wave[i+1] = wave[i])
well except for the fact that both of the arguments are evaluated first so it does a proper swap instead of setting wave[i] = wave[i+1] and then wave[i+1] = wave[i+1]
well why wouldn't it work if we just did it like
(lambda n,wave: wave[i], wave[i+1] = wave[i], wave[i+1] for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1]))(int(input()),list(map(int,input().split())))
is there a reason we had to use __setitem__ or was that just for illustration?
wave[i], wave[i+1] = wave[i+1], wave[i] is a statement, not an expression
you can't put it in a lambda
it's invalid syntax
we had to use __setitem__ because function calls are an expression, which means you can put them in a lambda
it's the same reason that you use the (lambda x: ...)(input()) trick instead of just (x = input(), ...)
the second one is invalid syntax
oh right, I didn't know that
also, we're not printing wave at the end though are we
oh good point
(lambda n,wave:([(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])],print(wave)))(int(input()),list(map(int,input().split())))```
oh we're allowed to put commas like that
i used a tuple for the __setitem__ stuff too
uhm, one sec
[(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])]
Why's the whole thing a list
like, wouldn't it be just valid to have it without the enclosing [ ] as well?
Like (lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])
uhm, yeah but well why do we need a list comprehension here
to emulate the for loop
we can't put a normal for loop inside a lambda but we can put a list comprehension in and a list comprehension can be used as a for loop
>>> [print(x) for x in range(5)]
0
1
2
3
4
[None, None, None, None, None]```
this is the same as py for x in range(5): print(x) but it's an expression so you can put it in a lambda
if you remove the [ ] around it then it's not a list comprehension anymore, it's just invalid and python won't like it
oh, that makes it very clear, but one more thing, the placement of the brackets
lemme break the inner lambda down
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y))) this one?
yeah
the for loop is in the second part, yet we placed the brackets around the whole thing and not just the second part, what I mean is
(lambda x,y:
(
wave.__setitem__(i,x),
wave.__setitem__(i+1,y)
)
)```
hm yeah
so the second part
where the loop is
(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])
shouldn't just that part be inside the list comprehension
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))[(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])]
what??
um, sorry
why wouldn't (lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y))) be inside the list comprehension?
I mean, it's just the second part that has the for loop
?!
look at the original code
for i in range(n - 1):
if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1]):
wave[i], wave[i+1] = wave[i+1], wave[i]```
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y))) is basically wave[i], wave[i+1] =
which is in the for loop
it's essentially two parts right, one is
(lambda x,y:
(
wave.__setitem__(i,x),
wave.__setitem__(i+1,y)
)
)
and the second
(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])
no
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) is one thing
you can't separate (lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y))) from (wave[i+1],wave[i]) and have it still make sense
imagine if you went back to the original code and did this py wave[i], wave[i+1] = for i in range(n - 1): if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1]): wave[i+1], wave[i]
erm but isn't the general structure like so:
(lambda name_of_variables: code)(variables)
so here, the code is (wave.__setitem__(i,x),wave.__setitem__(i+1,y))
right?
wait hang on a second
ok so, yes, that proves that i'm right ._.
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i])
x,y is the name_of_variables
(wave.__setitem__(i,x),wave.__setitem__(i+1,y)) is the code
wave[i+1],wave[i] is the variables
the general structure is (lambda name_of_variables: code)(variables), not (lambda name_of_variables: code)[(variables) for i in range(n-1)]
AH yes, so after the whole lambda expression do you have the for loop, which is why the whole thing becomes a list comprehension
yes
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) is wave[i], wave[i+1] = wave[i+1], wave[i]
Ah, damn, yes
which is inside the for loop (which became a list comprehension)
right, now all of it makes sense
so the structure will always be
[(lambda name_of_variables: code)(variables) for i in range(n-1)]
right?
i've actually used this once in my code because hey it was a lot shorter than make a function and do so
and hence, that whole thing breaks down like so:
(
lambda n,wave:( #this bracket is so we get a tuple, to fit in print()
[(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))(wave[i+1],wave[i]) for i in range(n-1) if (i%2==0 and wave[i] > wave[i+1]) or (i%2==1 and wave[i] < wave[i+1])] #The inner lambda expression
,print(wave)) #the tuple ends here
) #the lambda thing ends here
(int(input()),list(map(int,input().split()))) #and these are the variables
range(n-1) is only because that's what it was in the original code
and you're not required to have a lambda call in the list comprehension
yeah, I just copy pasted your sentence from above and didn't change it but yeah I get the idea
the general structure for a list comprehension is [thing for thing in thing], and you can also have an if thing in there ([thing for thing in thing if thing])
and they don't all have to be the same i just couldn't be bothered to come up with more interesting placeholders
what do you mean I don't need to have the lambda call inside the comprehension? Didn't we just discuss why we needed that inside?
why wouldn't
(lambda x,y:(wave.__setitem__(i,x),wave.__setitem__(i+1,y)))be inside the list comprehension?
here
in this specific case you do need it
but in general, it is possible to have a list comprehension which doesn't have a lambda inside it
i showed you an example earlier
[print(x) for x in range(5)]```
this list comprehension isn't particularly useful because what it does is so ridiculously simple
but yeah, not all list comprehensions have lambdas
oh right, I read it the other way round
you meant that not all list comprehensions require a lambda, which is, well yeah
what I misread it as there could be lambda expressions which had their code inside a list comprehension but weren't called inside it
which well, is yes again true if I look at the outer lambda expression in the solution you wrote to my original question
well technically you can do this py [(lambda x: x) for i in range(5)]
it's just silly
because that is basically (lambda n, wave: [code])(variables)
hm yeah
Right, I learnt a lot of things from that talk. Thanks a lot for breaking everything down into such a simple manner, that was beautiful. โค๏ธ
Meanwhile if I manage to do my bytecode modifying thing, we might be able to write actually nice multi line lambdas
bytecode? you mean from advent of code?
yep
the interpreter doesn't directly run the source code, it turns it into bytecode and then executes that
How could this be optimized:
for i in range(1800,2401,4):not i%50and i%16or print(i)
in terms of chars, I mean
not speed
yes, Python is executed just like Java, though people call Java compiled and Python interpreted
for i in range(1800,2401,4):not i%50and i%16or print(i)
how's the interpreter able to interpret it right when there's no space before and and or
cause it knows an identifier can't start with a digit
the lexical analyzer sees "5" and starts building an int. It stops when it gets to "a" because that can't be part of an int.
oh wow, that's very clever
yeet
@burnt pasture also, java has this jvm that makes it portable, python doesn't have that sort of thing yeah?
@cursive plover Python has a VM also. It's not named something like "JVM", but it's the same idea
yeah I get that, I'm figuring out how they're the same
cause if i%16!=0 then i%16>0
and we can replace the 0 with i%50
cause we need that to be 0
and then we just check if the i%50 is actually 0
by doing a <1, ah that's very nice
I have recently come across a code golf technique: e.g.```py
"otnweo"[n::2]
In what cases is it beneficial to use this?
whenever you need to decide between 2 strings of similar length based on a predicate
or more than 2?
you could do something like (lambda n:[(print('zottffssenenwhoiieiireoruvxvgno ere ehe e nt '[int(i)::10])) for i in n])(input()) to print all of the digits in a number as words, or similar things for different scenarios.
seldom do you a situation where the stars align so that it works
what would be a case where you would get 0,1,2 and need print different things based on that?
oh, maybe that's how primo did it
๐คท
I found a relatively concise version using that method and it's still 30th
primo moves in mysterious ways
sweaty boi
it kinda ignores my submissions, they don't get scored or saved
maybe it's a browser thing
yeah, Safari 11.1.2 is no good
anyone know if there's somewhere to discuss stackless python?
here? makes sense to me
makes sense to me too, but not sure if anyone else around would have used it before. ๐
In particular, I'm having an issue with using stacklesssocket + boto3
and online resources seem to be rather scarce... so if any of you have some experience here, ding me in DM or hit me up in channel if you could.
yeah it works in chrome
b who
sick skills
afaik on python he's highest apart from primo
true
primo must literally spend 90% of every day doing these lol
lmao
when u minify ur code and it's a byte longer
Every time I come to this server I look at this room in awe thinking about what I would have to do to be able to post into esoteric Python.
what's the shortest way to sum the digits in a string?
can't sum strings to get the codepoint sum so I'm guessing just a simple sum with a genexp
what do you mean by "digits in a string"?
if you int the string and then do %9 then (iirc) you get the correct result modulo nine
but that probably isn't very useful- yeah
I need modulo 2
...i wonder if there's a similar trick for that
count the ones
.count('1') would be quite short but i reckon somebody else can do better
Aah beat me๐
is there a maximum length of the numbers?
6
I mean if there's a way to do that without converting to binary in the first place
if they're 6 digit binary numbers, int()%9%2 should work and be a bit shorter than .count('1')
int of whatever string you have
%9%6 maybe?
%9%2 works
But doesn't work with 111111
it gets 0
still open to even shorter suggestions tho
any mathsy way to check if the number of 1s in a number's binary will be even?
this not very nice(and very long) bit of code should tell you if a number's binary will have an even number of ones:
import math
def m(n):
return bool(sum([int(n/(2**i))%2for i in range(int(math.log2(n)))])%2)
```of course, it doesn't work for 0, but this is the most mathsy I could get it to be. I'm sure someone could make it much shorter.
int(n/(2**i))%2
n//2**i%2
n//2**i%2 isn't a solution, i'm just golfing pie42's code
right now we have bool(sum([n//2**i%2for i in range(int(math.log2(n)))])%2)
that doesn't need to be a list comprehension bool(sum(n//2**i%2for i in range(int(math.log2(n))))%2)
we don't need bool() either honestly sum(n//2**i%2for i in range(int(math.log2(n))))%2
difficult to golf it more than that though
iirc the current shortest way we've found is s.count('1')%2
this is basically the same as converting the number to binary and counting the ones
i mean pie's code, not jan's
there's no real shortcut besides that
what're some ways to replace an if __name__ == "__main__": main() with a single function call instead
I guess one option is to directly pass along __name__
is there some other hack to climb up the stack and pull out __name__?
(lambda name: (main if name == "__main__" else lambda:None)())(__name__)
ah, to clarify, the goal is to have an importable function that could be called
e.g.
from lib import run_only_if_main
run_only_if_main()
or more usefully
from lib import run_only_if_main
def main():
pass
run_only_if_main(f=main)
run_only_if_main = lambda f:globals()['__name__'] == '__main__' and f()```
run_only_if_main(main)
right, but you'd still have to pass in __name__
honestly i think that would work
if run_only_if_main is defined in another file, it'll still pull __name__ as the imported module name
hmm
decorate it
@zealous widget __name__ is still pulled as the imported module tho
so
so how can the decorator check if the module it was imported into is main?
the decorator isn't imported
the goal is something that can be imported by itself
for i in range(51):int(f'{i:b}')%9%2or print(i)
@analog rock why do we have a %2 there?
Also, the %9 trick to get sum of digits in a number would fail if the number is a multiple of 9, no?
if
run_only_if_mainis defined in another file, it'll still pull__name__as the imported module name
@old flint```python
#in your lib utils
run_only_if_main = lambda f,g:g['name'] == 'main' and f()
#in the caller
from lib.utils import run_only_if_main
def main():
print("i am the main code")
run_only_if_main()(f=main,g=globals())
nothing magical but at least you avoid typing the annoying '__name__'
apparently you could make use of inspect but its dependent of the python implementaion so not suitable for production
yea, I guess that's what I'd be looking at
wondering if there's some hacky solution to look for __name__ where I want it
probably it'll be too finicky for me to use in any actual code but was curious if there was any obvious strategy I'm missing
walk up the stack til you find the calling frame, check __name__ in its f_globals
in this case, you'd only need to go 1 level up the stack i suppose
if sys._getframe(1).f_globals.get('__name__', None) == '__main__':
@old flint
@low acorn how often do you see python builds that cpython based in production?
that aren't*
haha that might be it. Yea, I'll be using the assumption that I'm exactly one import away
can I do +=1 on a string?
to make it go to the next ascii value?
cause that'd be niceee
no
didn't think so tbh
probably hackery that is too long for my golf
You'd be able to save ~9 chars by taking advantage of lower chars, probably
but that requires the chr(ord( so saves nothing
yeah that's very longg
So I had this horrendously long code
redacted code (link here: https://rextester.com/ORGD62976)
which I managed to reduce to
x = bin(int(input()))[2:]
even = x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1')
odd = x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')
y = ""
for i in range(len(x)):
y += ("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0")
print(int(y,2))
now if someone could help me baar this:
y = ""
for i in range(len(x)):
y += ("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0")
then, I could get the whole program to be a baar thing
(lambda x, even, odd: (The baar code, print(int(y,2)))(input stuff)
so how do I get those three statements into a baar? :/
A list comp and a join?
oh wait yeah, lemme try that
"".join(("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))) ```
y = [("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))]
yep!
x = bin(int(input()))[2:]
even = x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1')
odd = x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')
y = [("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))]
print(int(("".join(y)),2))
bingo! thank you! so my final code would be this:
python (lambda x, even, odd: print(int(("".join([("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))])),2))) (bin(int(input()))[2:], x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1'), x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1'))
wait that gives me an error: NameError: name 'x' is not defined
Nevermind, that lambda conversion failed.
How do i convert the code into a lambda expression
Okay I got it, I got rid of even and odd and did it like this:
(lambda x: print(int(("".join([("0" if i%2 == 0 else x[i]) if (((x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')) < (x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1')) and len(x)%2 != 0) or ((x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')) >= (x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1')) and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))])),2)))(bin(int(input()))[2:])
I wonder if there's a better way to write that :/
x = bin(int(input()))[2:]
even = x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1')
odd = x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')```
you were using x to create even and odd
so you would need to do py (lambda x: (lambda even, odd: ...)(<define even>, <define odd>))(<define x>)
@sick hound well, then I did
(lambda x:
(lambda even, odd:
print(int(("".join([("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))])),2))))
(x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1'), x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1'))
(bin(int(input()))[2:])
@sick hound it's not fixed but I still get name error
wait
(lambda x:
(lambda even, odd:
print(int(("".join([("0" if i%2 == 0 else x[i]) if ((odd < even and len(x)%2 != 0) or (odd >= even and len(x)%2 == 0)) else (x[i] if i%2 == 0 else "0") for i in range(len(x))])),2)))
(x[::2].count('1') if len(x)%2 == 0 else x[1::2].count('1'), x[1::2].count('1') if len(x)%2 == 0 else x[::2].count('1')))
(bin(int(input()))[2:])
is the fixed version
AND THAT WORKS!
Oh yeah! thank you
not pretty but yeah xd
x = f'{int(input()):b}'
@analog rock I know the f is for f string, but I googled that whole thing and I couldn't find much. How's that managing to convert the input to binary? What would I have to change b to get it into other bases? What should be the keywords I should be googling for this? I didn't find it in the docs either: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
what is the :b thing called
no idea I'm afraid
ah, alright
oh great, thank you ! โค๏ธ
@low acorn how often do you see python builds that cpython based in production? that aren't*
@brazen geyser no idea, i just repeated the piece of advice i found on stackoverflow about inspect or even sys.get_frame , the guy seemed to have a solid stacko reputation so i trusted him as i have yet to use python in a professional context for now .
from the doc https://docs.python.org/3/library/inspect.html#inspect.currentframe or https://docs.python.org/3/library/sys.html?highlight=_getframe#sys._getframe CPython implementation detail: This function should be used for internal and specialized purposes only. It is not guaranteed to exist in all implementations of Python.
Thing is, very few companies use python that is not CPython
i see
The bad thing about implementation details isn't only avability but also backwards compability. It is easy to do backwards-incompatible changes on implementation details rather then language specs (which is really rare)
re: availability i think jython, ironpython and stackless all have _getframe and inspect. micropython and circuitpython probably dont
oh yeah pypy has them i think
Oh thatโs neat
I donโt think they have gc tho
since like, PyPy does not even have a GC
This page is about pypy-stm, a special in-development version of PyPy which can run multiple independent CPU-hungry threads in the same process in parallel. It is a solution to what is known in the Python world as the โglobal interpreter lock (GIL)โ problem โ it is an implementation of Python without the GIL.
wow that's kinda insane
there is bunch of europython talks about that subject
it is a pretty cool concept
Armin Rigo - Using All These Cores: Transactional Memory in PyPy
[EuroPython 2014]
[23 July 2014]
PyPy, the Python implementation written in Python, experimentally supports Transactional Memory (TM). The strength of TM is to enable a novel use of multithreading, inheritently ...
Armin Rigo - The GIL is dead: PyPy-STM
[EuroPython 2015]
[23 July 2015]
[Bilbao, Euskadi, Spain]
Take a big, non-multithreaded program, and run in on multiple cores!
PyPy, the Python implementation written in Python, experimentally
supports Software Transactional Memory (STM...
is there a more concise way to say x>y<1
what's y? (can it be a float or negative?)
lol I wrote that
with jan's and akarys' help
I think I posted the normal long program before it which is baar here?
You start with a more reasonable program and work your way down
Following some basic rules along with some creativity
x and y are ints
@cursive plover what does that function do?
@vocal nest
okay so what that program does is:
You're given a number, which you are to convert to binary.
Then in the binary form, you need to check number of 1s at 'even' and 'odd' positions(the rightmost digit is at position 1, then the digit towards the left of it at position 2, so on..)
if number of ones at the 'odd' positions are less than those at 'even' positions, then make the numbers at odd positions 0.
Otherwise, make the numbers at even positions 0.
after that, print the resultant number
@vocal nest it would be 5
because its an odd right?"
at position 1 you have a 1,
at position 2 you have a 0
at position 3 you have a 1
yeayea
so 1s at odd positions are greater than those at even positions, so make everything at even positions 0
(in this case, everything is already 0 at even positions)
so it remains 101, which is 5
if it was 12
it would bw 1100, which would become 0100 which is 4
yup
Very cool, how are you using it?
erm, just came across it somewhere
someone was solving these algorithmic practice questions and she showed me this
oh right
i thought it would be good practice to hone my baar coding skills
yea thats awesome
since I'm very new to it
lakmatiol here introduced it to me
awesome
someone made this wonderful factorial function
assert (lambda f: (lambda x: x(x))(lambda y: f(lambda x: y(y)(x))))(
lambda f: lambda n: (
lambda n: n(lambda f: lambda x: lambda y: y())(lambda x: lambda y: x())
)(n)(lambda: lambda f: lambda x: f(x))(
lambda: (lambda x: lambda y: lambda f: y(x(f)))(n)(
f(
(
lambda n: (lambda p: p(lambda x: lambda y: y))(
n(
(
lambda p: (lambda a: lambda b: lambda s: s(a)(b))(
(lambda n: lambda f: lambda x: f(n(f)(x)))(
(lambda p: p(lambda x: lambda y: x))(p)
)
)((lambda p: p(lambda x: lambda y: x))(p))
)
)(
(lambda a: lambda b: lambda s: s(a)(b))(
lambda f: lambda x: x
)(lambda f: lambda x: x)
)
)
)(n)
)
)
)
)(lambda f: lambda x: f(f(f(x))))(lambda x: x + 1)(0) == 6
#---------------------^ input here as a church numeral
Lambdas are amazing
But I dont rly get them
never used them although I know perfectly how they work
they are not that useful in actual code
what's x(x)
I mostly use them to do dumb things like integer math without integers
call x with x as an argument
did the person who write that also explain any parts of it?
I don't quite get how that works
it is just lambda calculus
I've never heard of that term before
I would suggest reading this https://arxiv.org/pdf/1503.09060.pdf
it is abstracted calculus*
it is quite unrelated to calc stuff like derivatives and intergrals
y'know how you have turing machines as the basis for computing. Lambda calculus is an alternative description of such machines, sometimes nice. e.g. erlang, haskell are based on it
right, thank you for the resource! I'll get onto it
I generally use operator.itemgetter for this
but key kwargs are one of the uses for lambdas
๐
i wish python had arrow lambdas
what's the difference?
shorter
it would make me not feel like a failure for using a lambda
That's exactly what guido wanted you to feel ๐
realised we can do this
In [2]: from types import SimpleNamespace
In [3]: a=SimpleNamespace()
In [4]: setattr(a, "1()", "Magic string")
In [5]: "{0.1()}".format(a)
Out[5]: 'Magic string'
privat-er vars
It'll be in there somewhere.
@edgy kelp what is the 1() doing there in setattr
Does SimpleNamespace have a custom __setattr__?
Just an object that can have instance vars.
Ought to work on them all, but it's a var only accessible through that, getattr and if there's anything else that fetches from strings
my little bot learned how to parse python code, lol
yea ast.parse + ast.dump with new indent = 4
a = lambda n: for i in range(n): print(f"You have been funny for {i} times!");a(1000)
Condense this
Please
for i in range(1000):print('You have been funny for',i,'times!')
a = lambda n: for i in range(n):print('You have been funny for',i,'times!');a(100)
(lambda n: [*map(print, map("You have been funny for {} times!".format,range(n)))])(1000)```
hmm
(lambda n: any(map(print, map("You have been funny for {} times!".format, range(n)))))(1000)``` @crystal mica
(lambda n:[0 for i in range(n)if print('You have been funny for',i,'times!'])(1000)```
(f := lambda n: (n > 0 and print(f"You have been funny for {n} seconds") or f(n-1)))(100)
(lambda n:[0for i in range(n)if print('You have been funny for',i,'times!'])(1000)```
lakmatiol left a space in
Why not just
(lambda n:[print('You have been funny for',i,'times!')for i in range(n)])(1000)
Who needs a list of 1000 0's?
the list is empty, because if print(... is always false
oh
Well, this is 1 character shorter:
(lambda n:any(print('You have been funny for',i,'times!')for i in range(n)))(1000)
If you fix the unmatched (
why not py any(print('You have been funny for',i,'times!')for i in range(1000))
or py for i in range(1000):print('You have been funny for',i,'times!') if we're allowing it to not be an expression
or py [print('You have been funny for',i,'times!')for i in range(1000)]
I have no idea
(lambda n:print(('You have been funny for {} times!\n'*n).format(*range(n))))(1000)
Hey on the topic of esoteric python / code-gore. What's anyone's favorite abuse of __init_subclass__ and metaclasses (or shudder using both at once)?
I used it to re-initalize mutable class defaults once
derp
(lambda n:print('You have been funny for %s times!\n'*n%(*range(n),)))(1000)
(lambda n:print(('You have been funny for %s times!\n'*n%(*range(n),))[:-1]))(1000)
for correct new lines
i=0;exec("print(f'You have been funny for {i} times');i+=1;"*1000)```
exec and ; are cheating!
shush! their wives might hear if you scream that too loudly.
Check this out.
https://repl.it/@int6h/DataDrivenTesting
It's my new data-driven unit testing framework!
Repl.it is a simple yet powerful online IDE, Editor, Compiler, Interpreter, and REPL. Code, compile, run, and host in 50+ programming languages: Clojure, Haskell, Kotlin (beta), QBasic, Forth, LOLCODE, BrainF, Emoticon, Bloop, Unlambda, JavaScript, CoffeeScript, Scheme, APL, L...
Suppose you have some code that you need to test, and that code consists of modules that export something.
Then you can create a 'mirrored' structure of folders that will be used to test these modules.
For example, if you want to test this file:
You will create a structure like this:
So each export corresponds to a test file
Tests can be 'imperative'
Or you can specify them as a data structure
And it automagically tests everything
Has anyone here used the usbserial4a package?
Soo? :)
first hint: PyRun_InteractiveOneObjectEx and the oenc

So I'm trying to bar this piece of code now, but it's got try catch in it so idk how I'd lambda that:
~~```python
a = input()
num = int(input())
ans = []
i = 0
j = i
while True:
try:
form = [int(char) for char in a[j:i+1]]
if sum(form) == num:
ans.append("".join(str(char) for char in form))
if (sum(form) + int(a[i+1])) <= num:
i += 1
elif (sum(form) - int(a[i])) <= num:
j += 1
else:
j = i
i += 1
except:
break
print(*ans)
What the program does btw is, given a string say `9390215671237812390`, and a number `9`, find all substrings whose sum equals `9`. The output in this case would be `9 9 90 81 9 90`
actually, I got rid of the try catch, but it's uglier now
a = input()
num = int(input())
ans = []
i = 0
j = i
while i < len(a):
form = [int(char) for char in a[j:i+1]]
if not form:
i += 1
continue
if sum(form) == num:
ans.append("".join(str(char) for char in form))
if i+1 < len(a) and (sum(form) + int(a[i+1])) <= num:
i += 1
elif (sum(form) - int(a[i])) <= num:
j += 1
else:
j = i
i += 1
print(*ans)
still unsure how I would baar this, especially with that while thing in it
Any help? :/
Well, try to simplify it first imo, then you can do it
The simplest, straightforward is like this
!e ```py
string = '9390215671237812390'
def get_sum(s: str) -> int:
return sum(map(int, s))
def generate_all_substrings(s: str) -> list:
substrings = []
for length in range(1, len(s)):
for start in range(0, len(s) - length):
substrings.append(s[start:start+length])
return substrings
print([substr for substr in generate_all_substrings(string) if get_sum(substr) == 9])```
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
['9', '9', '9', '90', '81']
This is easy to put into one line
!e py (lambda s,total:print([_s for _s in(s[i:i+l]for l in range(1,len(s))for i in range(len(s)-l))if sum(map(int,_s))==total]))('9390215671237812390',9)
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
['9', '9', '9', '90', '81']
There we go
The idea of how you approach it is very important
Missing a 1
(lambda s,t:print([_s for _s in(s[i:i+l]for l in range(1,len(s))for i in range(len(s)-l+1))if sum(map(int,_s))==t]))('9390215671237812390',9)```
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
['9', '9', '9', '90', '81', '90']
There we go
May I question why
Ah and also
@crystal mica I am currently working on braces in python

@crystal mica hm yeah, that's nice
but
isn't that O(n^2) while mine is essentially O(n)?
also
this does not maintain the order in which the substrings occur in the string, I forgot to mention that is a requirement too
how would you alter the code for that :/
Ah I see, there are adjustment that you can do to prevent that, for example stop when substr is greater than the t
@marsh void that sounds very bracely!
Indeed it does!
you mean if sum(substring) > total, don't append?
(lambda x,y:{for x in range(x){for y in range(y){print(x,y)}})(10,10)``` @crystal mica
hehe
Ah, AND! Top-level awaits on < 3.8!
I mean when sum(substring) > t then exit the inner loop, takewhile is a good candiate for that
!e py (lambda s,t:print([_s for _s in(s[i:j+1]for i in range(len(s))for j in range(i,len(s)))if sum(map(int,_s))==t]))('9390215671237812390',9)
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
['9', '9', '90', '81', '9', '90']
Changing the order of the loop -> preserve the order of finding
!e py (lambda s,t:print([_s for _s in(s[i:j+1]for i in range(len(s))for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))))if sum(map(int,_s))==t]))('9390215671237812390',9)
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
['9', '9', '90', '81', '9', '90']
itertools.takewhile(predicate, iterable)```
Make an iterator that returns elements from the iterable as long as the predicate is true. Roughly equivalent to:
```py
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
It's very handy!
thanks, I'm reading through it at the moment!
I'll ask you again when I don't understand smth in that solution, or in takewhile
โค๏ธ
I was wondering, is there any way to make this true?
(obj1 == obj2) and (obj1 != obj2) ```
Eh, by modifying ojb1.__eq__ I guess?
!e
py (lambda s,t:print([_s for _s in(s[i:j+1]for i in range(len(s))for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))))if sum(map(int,_s))==t]))('9390215671237812390',9)
@crystal mica
ok so 2 things
One, is the takewhile(lambda j: ...) assigning j a list or something? because it's for j in .... , so that either is a list, or a generator? Right?
Second, how did you just do the __import__ thing? How's it different from import ? Does this thing have a name?
And why is the itertools in inverted commas, 'itertools' and not itertools ?
when you dopy import stringyou basically dopy string = __import__('string')
And py import string as built_in_stringispy built_in_string = __import__('string')
In this case we're importing itertools without doing a separate import line
And assign it to a variable
takewhile returns a generator
oh okay
Basically it's the same as py for things in my_iterable: if condition: yield things else: break
ah, it's a generator cuz it has yield, right?
You can certainly check what it is
!e py import itertools a = itertools.takewhile(bool, range(100)) print(type(a), a)
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
<class 'itertools.takewhile'> <itertools.takewhile object at 0x7f3b5fb9df40>
It should be an iterator, so unless you unpack it
unless I unpack it, I won't be able to print its content
Yep, unless you actually iterate over it and consume it
itertools' objects are essentially iterable classes, not generators
!e py import itertools a = itertools.takewhile(bool, range(1, 10)) print(type(a)) print(list(a)) print(list(a))
@crystal mica :white_check_mark: Your eval job has completed with return code 0.
001 | <class 'itertools.takewhile'>
002 | [1, 2, 3, 4, 5, 6, 7, 8, 9]
003 | []
built-in != made in C
one more thing
(lambda s,t:print([_s for _s in(s[i:j+1]for i in range(len(s))for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))))if sum(map(int,_s))==t]))('9390215671237812390',9)```
why did the whole thing have to be inside brackets, for in
ie for _s in(...)
when I remove the brackets the code gives errors
when we do a for i in range(..) we don't need the brackets right? We don't do a for i in(range(...)), so why here
because s[i:j+1]for i in range(len(s) isn't syntactiaclly anything
it needs [] or () around it
um don't we do that sort of thing in a list comprehension though?
like x = [i for i in range(something) for j in range(something)]
oh wait, that's what you just said
my bad
right, got it. thanks lol
you can build it into the comprehension, so you'll have s[i:j+1] everywhere you have _s
which is two places
let me read it...
Yea, there is no yield in C level, so if you are going to define a generator it should be a class with iter and next
actually wait
print([_s for _s in(s[i:j+1]for i in range(len(s))for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))))if sum(map(int,_s))==t])
isn't the whole thing already in []
so
print([_s for _s in s[i:j+1] for i in range(len(s)) for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))) if sum(map(int,_s))==t])
no
it's now in the wrong order
!= isn't testing for __eq__ to return False?
i and j donn't exist at that point
@thin trout there's __ne__ I think, it probably falls back on eq but it doesn't need it
oh yeah, so I'll have to change the order
so it would be
hm nah, that looks like the only way to do it
print([s[i:j+1]for i in range(len(s))for j in __import__('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s)))if sum(map(int,s[i:j+1]))==t])
here
you replace _s with s[i:j+1] in two places and the tricky part is to find a closing ) to remove ๐
oh yeah that makes sense
so the earlier one had _s being picked from a generator, and being added to a list, which was finally printed
here, we've got a list which we append a substring directly to, and print the list out
yeah?
sure
and you can do this to "rename" it to _s anyway ```py
print([_s for i in range(len(s))for j in import('itertools').takewhile(lambda j:sum(map(int,s[i:j+1]))<=t,range(i,len(s))) for _s in [s[i:j+1]] if sum(map(int,_s)==t])
for _s in [s[i:j+1]]
make a list of one element to unpack it back under a different name
that's the problem with python comprehensions, that isn't there in haskell
where you can ..., let _s = s[i:j+1], ... , as a step
you mean assigning a list to a variable can be done inside a lambda expression in haskell?
inside a comprehension
like there are two things you can do in a comprehension, a "guard" with if and an iteration with for
you can interleave them in any order
in haskell they are separated with commas:
[x + 3 | x <- nums, mod x 2 == 0] this means
[x + 3 for x in nums if x % 2 == 0]
python omits | and ,
idk
also, when you say any order
I thought it's always [xyz for something if something] ?
you can have ifs interleaved with fors
but never an if before all the fors, yeah?
sure, you have no bound variables before the fors, so an if has nothing to work with
no I mean
something like
if x>y:
for i in range(something):
blah blah
i just checked, that;s invalid
yea
yeah
!e
a = 4
b = 9
print([d for d in[1,2,3]if a > b])
@formal sandal :white_check_mark: Your eval job has completed with return code 0.
[]
!e```
print([1 for x in "a" if False for y in 5])
exit()
except
enumerate
!e
print([1for x in"a"for y in"b"if""])
@formal sandal :white_check_mark: Your eval job has completed with return code 0.
[]
basically you try to iterate through an integer and it doesn;t crash
because it's after an if
so doesn't happen
๐
never noticed this ambiguity if""
On a related note, i.e. breaking out of comprehensions, this list(next(iter([])) if x == 'a' else x for x in 'blah') works in py2 but doesn't in py3 (list doesn't absorb the stopiteration). Anyone know a workaround?
ah in my head you've either got prehistoric python (py2) or current python (3.7+) lol
!e
print([1 for x in "a" if False for y in 5])
@sick hound :white_check_mark: Your eval job has completed with return code 0.
[]
I guess
Maybe I went too far with my toy type checking...
I can create new 'types' with tools like either, product, record etc. using simple data structures
Then I can use these 'types' as function annotations. The @tc makes them work as argument filters, and @mdtc allows overloading functions with help of this infrastructure.
I can't believe I even managed to make it all work together.
looks great except I guess a bit implicit @formal sandal
What do you mean?
Maybe change tc => template_check and md => multiple_dispatch or overload
But I also have mdtc which is their composition. It's kinda hard to find a name for it...
Maybe I shouldn't use md and rename mdtc to overload
that totally makes sense and IMHO looks better
overload might be conflict with typing.overload
Hm...
Well, it would be strange to use both typechecking systems at the same time.
So to avoid confusion when using both libraries, explicitly state which library you're using
Maybe you can use these 'templates' to do some additional checks. A 'template' doesn't necessarily mean 'type'.
This will throw an "Invalid UID" error because UID is a result of msg.
[*(lambda _____: _____((lambda _: _([-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False)) for __ in range((-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False)).__rmul__(-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False))).__rmul__(-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False))) + 8))]))(lambda ___: [____ for ____ in range(1, sum(___).__add__(1))])))(lambda ______: [__import__('builtins').__dict__['print'](''.join(list(map(str, _______)))) for _______ in __import__('itertools').__dict__['product'](______, repeat=len(______)) if not None])]```
prints 111111111 and goes up from there
yewah
nice
is there a reason you named your variables as _, __, ____, instead of like a,b,c lol
I wanna fractionate the code down to understand it, but I'm scared I'll end up breaking it by replacing those vars incorrectly lol
They're just to confuse people.
@tribal moon You don't need to use abs because you can multiply by booleans as if they were integers. And if you did need integers, you could use +False to be slightly shorter.
Yeah this is really messy
@cursive plover if you want, you can replace the underscores with your own variable names
You can use notepad++ to do it
yeah I'm gonna do that
what does it mean 'obfuscation'
making it hard to tell the actual purpose of a given part of code
[*(lambda _____: _____((lambda _: _([-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False)) for __ in range((-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False)).__rmul__(-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False))).__rmul__(-~True.__rmul__(abs(False))**-~True.__rmul__(abs(False))) + 8))]))(lambda ___: [____ for ____ in range(1, sum(___).__add__(1))])))(lambda ______: [__import__('builtins').__dict__['print'](''.join(list(map(str, _______)))) for _______ in __import__('itertools').__dict__['product'](______, repeat=len(______)) if not None])]```@tribal moon
tf
Deliberately making it hard to tell the actual purpose of a given part of code*
Involuntary obfuscation is something I wish happened to my code more rarely...
bunch of multiplications with 0
!e
def is_syntax_error(s):
try: compile(s,'','eval')
except SyntaxError: print(f"'{s}' = Syntax error")
else: print(f"'{s}' = OK")
is_syntax_error("2.__add__")
is_syntax_error("(2).__add__")
is_syntax_error("2. __add__")
is_syntax_error("2 .__add__")
is_syntax_error("2..__add__")
is_syntax_error("2. .__add__")
is_syntax_error("2.. __add__")
@formal sandal :white_check_mark: Your eval job has completed with return code 0.
001 | '2.__add__' = Syntax error
002 | '(2).__add__' = OK
003 | '2. __add__' = Syntax error
004 | '2 .__add__' = OK
005 | '2..__add__' = OK
006 | '2. .__add__' = OK
007 | '2.. __add__' = OK
!e
def catcher(handler, e=Exception):
def catch(f):
def _(*args, **kwargs):
try:
return f(*args, **kwargs)
except e as _e:
return handler(*args, _e=_e, **kwargs)
return _
return catch
# Now you can write crap like this:
v = catcher(lambda _e:eval("1 .__add__"))(lambda:eval("1. __add__"))()
print(v(41))
@formal sandal :white_check_mark: Your eval job has completed with return code 0.
42
class NumberAttributes(TokenTransformer):
@pattern("number", "name")
def fix_number_attributes(self, number, attribute):
if number.string.endswith("."):
return number._replace(string = f"{number.string[:-1]} ."), attribute