python
Context: testing something on all permutations of N variables. I used itertools, his claim: "if you add a for loop for each variable it's less error prone" SMH
I never share "bad code" because I prefer showing someone why their approach could be better but this guy's arrogance was beyond what I could stand (there's a whole thread).
My solution:
import itertools
NUM_VARS = 1000
for p in itertools.permutations(range(NUM_VARS)):
check_result(p)
Meh...
EDIT: NUM_VARS is obviously a high number just to show how flexible the approach is, I haven't tried iterating over !1000 permutations :)
I don’t know but loop over permutations of 1000 elements destroys my brain… has your test finished yet? Maybe get a coffee and take a look after the heat death of the universe
I love that they're saying adding a separate loop for each variable would be less error prone then have example code that has errors in it.
Also this isn't necessarily bad (and probably is just me being a nerd) but the logic in the print statement could be simplified to not x or not y and work the exact same for those two variables.
The point of this was to test that specific expression without applying any simplification. But the "bad code" is adding a nested loop for each value you want to test it with (in this case only 2, imagine with 3+ LOL).
For x in (False, True):
For y in (False, True):
Print(not((not x or y) and x)
Se crei un for per ogni variabile c'è meno rischio di errore [Translation (Google Translate): "If you create a for for each variable there is less risk of error".]
No with a range of numbers. You can use a counter and then use bit shifting to extract the bits that ultimately form a variable length permutation with the ones and zeros. From there you can convert it to numbers if you wish.
It iterates over every possible permutation of the N values you want to test a condition for. E.g. ["Hi", {"k": 123}, 1.0, object(), (1,2,3)]. Not sure what you mean by "iterating over every possible probability".
I think your a bit. I had suspected so. The above comment was supposed to evoke some sort of an emotional reaction - something most people would have after being called that. Your other comments seem to be off kilter, and I dunno… you just come off as a bot. I’m really, really sorry if I’m mistake but yeah
20
u/119b63 Sep 11 '21 edited Sep 12 '21
I never share "bad code" because I prefer showing someone why their approach could be better but this guy's arrogance was beyond what I could stand (there's a whole thread).
My solution:
Meh...
EDIT: NUM_VARS is obviously a high number just to show how flexible the approach is, I haven't tried iterating over !1000 permutations :)
8
u/shinydewott Sep 12 '21
Isn’t that print function missing a parentheses
5
u/Thumperfuzzy Sep 12 '21
I love that they're saying adding a separate loop for each variable would be less error prone then have example code that has errors in it.
Also this isn't necessarily bad (and probably is just me being a nerd) but the logic in the print statement could be simplified to not x or not y and work the exact same for those two variables.
10
u/seeroflights Sep 11 '21
Image Transcription: Reddit Comments
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
u/[deleted] Sep 12 '21
And I used bit shifting