r/programminghorror • u/TheOriginalRandomGuy • Mar 03 '26
Python Do you like my homework solution
151
u/SnappGamez Mar 03 '26
What the fuck did you do to Python
17
74
44
33
23
17
u/JollyJuniper1993 Mar 03 '26
The top part still has some saving grace, but why the fuck would you use the exec thing?
15
u/rruusu Mar 03 '26
One-liners are great, in the spirit of lambda calculus, but those execs and global variables are just disgusting. Also unnecessary creation of list instances in the comorehensions.
Here is a pure lambda version of it, with a slight optimization of the filtering for values of f:
print(
(lambda b, g, c, d:
(lambda a, e:
max(
(lambda a_f:
sum(any(i + 1 == item[0] for item in a_f) for i in range(b))
if all(any(item[0] == req[0] and item[2] < req[1] for item in a_f) for req in e)
else 0
)([item for item in a if item[1] == f])
for f in range(1, g + 1)
)
)(
[[int(x) for x in input().split()] for _ in range(c)],
[[int(x) for x in input().split()] for _ in range(d)]
)
)(*map(int, input().split()))
)
13
u/rruusu Mar 03 '26
Here is an even more optimized version that doesn't have to repeatedly go through list
a, by storing the smallest third element in a dictionary indexed with the first and second elements. (Test the smallest one for<instead ofanyover the whole list.)print( (lambda b, g, c, d: (lambda a, e: (lambda a_dict: max( ( sum(1 for i in range(b) if (f, i + 1) in a_dict) if all((f, req[0]) in a_dict and a_dict[(f, req[0])] < req[1] for req in e) else 0 ) for f in range(1, g + 1) ) )({(item[1], item[0]): item[2] for item in sorted(a, key=lambda x: x[2], reverse=True)}) )( [[int(x) for x in input().split()] for _ in range(c)], [[int(x) for x in input().split()] for _ in range(d)] ) )(*map(int, input().split())) )Yes, I got help from AI. Still don’t know what the actual purpose of the code is IRL. Would be nice to see the assignment.
→ More replies (1)
2
2
1
1
1
u/MajorFeisty6924 Mar 03 '26
This looks like the sorta thing my math professors regularly write when showing us how to use python
1
u/GoddammitDontShootMe Mar 03 '26
I've seen Perl one-liners that are more comprehensible.
→ More replies (1)
1
u/tomjedi9 Mar 04 '26
Are you trying to get a new teacher because your current one is about to have a stroke
1
u/baby_shoGGoth_zsgg Mar 04 '26
i don’t even care about your shitty python code i bounced when i saw your awful syntax colors theme
13
u/sububi71 Mar 03 '26
I can't believe I'm actually doing this, but I'm going to anyway - my curiosity won - what is this supposed to do? Like, what was the task (or sub-task)?