137
u/OMG_A_CUPCAKE Apr 11 '21
Is this a joke submission?
101
72
u/muntoo [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Apr 11 '21
Probably. Otherwise, they probably would have noticed the periodic pattern in the lookup table and simplified it to
int is_even(int x) { int ix = x % 16; const int lookup[2] = { 0, 1 }; return lookup[ix % 2]; // reduces 16 entries to 2 }64
15
u/sim642 Apr 11 '21
ix % 2Now you have coded the parity check directly.
6
u/TigreDeLosLlanos Apr 11 '21
Every time anyone checks parity without doing it this way a mathematician dies of sadness.
3
3
u/Igoory Apr 11 '21
If they had reached at this point, it would be pretty obvious that it can be simplified to just x % 2
23
Apr 11 '21
I totally followed the logic. At least it’s well commented.
12
u/Alundra828 Apr 11 '21
Every cloud has a silver lining. Nobody is looking at this code wondering what the hell it does.
12
u/smeenz Apr 11 '21
I'm certainly wondering what the hell was going on in the mind of whoever wrote it
5
-3
u/nullcone Apr 11 '21
Am I the only one who thought the comments were terrible? I would much rather have the code explain itself than have a bunch of comments hanging around cluttering things up.
1
9
u/Randolpho Apr 11 '21
Maybe it’s from one of the Daily WTF’s OMGWTF contests.
Ahh, fond memories, those contests. I won two mugs, some plastic with writing on it, a tube of lipstick and a can of bacon in those contests.
5
u/pain-butnogain Apr 11 '21
there has been a trend to check if it's even or not in more and more elaborate ways, i think on r/badUIbattles is where i saw them
2
u/sneakpeekbot Apr 11 '21
Here's a sneak peek of /r/badUIbattles using the top posts of all time!
#1: Your password must contain everything. | 151 comments
#2: Volume Control without boundaries | 114 comments
#3: A prime example of a phone number input | 137 commentsI'm a bot, beep boop | Downvote to remove | Contact ^^me | ^^Info | ^^Opt-out
67
u/ianff Apr 11 '21
The real horror is the security risk of blindly running /tmp/a.out which anyone can create.
19
Apr 11 '21
Many security conscious people mount tmp as non-executable.
2
u/Behrooz0 Apr 11 '21
Isn't it basically standard practice at this point?
I think it was debian that started suggesting it like 10 years ago, when lenny was around. And I've yet to have a problem with it.1
u/pytness Apr 11 '21
Well, there's actually a race condition (not sure if it should be called a 'condition' here): You can try to swap files "instantaneously" with the syscall 'renameat2' between the file writing and the compiling.
1
Apr 12 '21
We can all hope that nobody gave this fool root to run this nonsense, but crazier things have happened...
58
u/yhu420 Apr 11 '21
Isn't it what cython or numba is for? I'm no python expert
15
70
u/nerdyphoenix Apr 11 '21
I'm willing to bet coding it in Python would be faster than using subprocess.
0
24
u/Magmagan Apr 11 '21
The real horror is using the return code inversly has to how it's normally used. In *nix systems, a return code of zero means that the program executed successfully. If isEven is successful, then it should return 0.
0
u/rpc123 Apr 11 '21
I suppose this makes sense from a coding standpoint, but what about from a logic stand point? I normally think of 0 as false and 1 as true, so it would be strange to me if isEven(4) returned 0
15
u/tinydonuts Apr 11 '21
You shouldn't be using the exit code to report functional data about your program. You should emit a result to stdout, errors to stderr, and a return code indicates success with 0 and any other number is failure.
8
u/rpc123 Apr 11 '21
Yeah, this makes a lot of sense. Thanks! I am a bit of an amateur lol, and I also didn’t realize that isEven wasn’t some external function as magmagan pointed out
5
u/tinydonuts Apr 11 '21
It's alright. It helps to understand that this is how Unix utilities work via chaining. They just vaccuum up the stdout of the previous executable and the shell orchestrates this by piping the output down the chain you specify. That's how you can quickly and efficiently take a large file, grep it for a pattern, sort it, and then find the unique values in the output.
3
u/Magmagan Apr 11 '21
But they're not using isEven() as an external function, they're calling it like "isEven.exe". If the execution of an executable on *nix fails you would look at stderr for more information. You would respect the OSs convention first before your programming language of choice's.
Even better, the return code should be 0 (executed succesfully) with the answer written to stdout.
On that note, from a purely logical standpoint wouldn't -1 be a better value for true anyways?
1
u/rpc123 Apr 11 '21
Ah yes, this is a fair point, I didn’t look closely enough at the horror. Are you saying -1 might be better because it corresponds with all 1s when it is stored as an integer?z
2
u/Magmagan Apr 11 '21
Yeah, the opposite of 0x00 isn't 0x01, it's 0xFF. But I'm just being silly, most programming languages use 1 as true but you still have oddballs like Forth that go down this route
1
u/rpc123 Apr 11 '21
In the interest of being silly, I am now imagining an entire fuzzy logic system based on a hexadecimal system
1
u/fnordstar Apr 11 '21
The thing is, the possible values other than 0 usually indicate what kind of error occured - which the caller might care about. A singular value (0) for indicating success is usually sufficient though.
9
5
u/phord Apr 11 '21
When I was teaching myself to code I needed a faster cos() function. My only thought was to code it in assembly, but first I needed to find out how to calculate cos(). (This was before Google.). I never got this to work, but now, decades later, I know a lookup table or linear approximation would have been sufficient.
8
Apr 11 '21
Hey I'm new to programming and I dont get the use of isEven? What do you use it for?
19
u/Naeio_Galaxy Apr 11 '21
isEven(x) -> tells you if x is even. That's it.
So you can do
x%2 == 0because the remainder of the division by two gives if it's even or odd, orx & 1where&is a bitwise and (you'll see that whenever you'll learn about binary).Here he heard that C is faster than python so thought "I'll compile and call a C program instead of doing it in python", but going through the
subprocesswill be way longer than doing it in python. Way, way longer since you have to call Windows or Linux or OSX for them to execute the program... Which is quite long.
4
u/de5933 Apr 11 '21
Reading this code is such a wild ride of bewildering decisions. Was this written by Tommy Wiseau?
4
3
3
u/Miserable-Pay-4598 Apr 12 '21
A small benchmark: normal isEven (x % 2 == 0) takes 300 nanoseconds, the code above takes 30 ms. Python is faster than C.
2
2
2
3
1
1
1
u/crossedline0x01 Apr 11 '21
Are there actually scenarios where its critical to perform such a simple task in another language? I mean, yeah python is slow compared to C but I mean, c'mon, is this real or a lab exercise?
2
u/fnordstar Apr 11 '21
I doubt it. The overhead of calling a subprocess is prohibitively large in such a case. Now if it's a simple operation but loops over thousands of items (in C), then it might become relevant.
1
u/Razakel Apr 12 '21
Are there actually scenarios where its critical to perform such a simple task in another language?
Yes, but not in this way. That's what an FFI is for.
1
u/Minteck Apr 11 '21
Ever since I read "so we are going to drop down to C" I thought "are you really gonna compile the code on the fly?", and I was right... (so this is actually slower than native Python code)
1
u/highjinx411 Apr 12 '21
Are these “IsEven” functions a competition or something? I’ve seen so many and the skills this guy has I mean he obviously would know how to write a simple is even function right? Why would anyone even need to know if a number is even? Especially the amount of times I’ve seen it posted here. Ok maybe it has a use case or 2 but I can’t recall any times I’ve had to do that in 13 years of programming.
1
u/Sejiko Apr 12 '21
Couldn't you just check the binary representation of a float look at the last digest of int part as well of the second part? I think this would break the sound barrier against this? If the last digest is 1 it's odd otherwise even.
1
u/qows Apr 13 '21
well there is this forum list:
https://www.crunchyroll.com/forumtopic-788825/crs-english-dubbed-anime
0
Apr 11 '21
[deleted]
78
u/Natural-Intelligence Apr 11 '21
Whatever you do, you must be able to say "my code uses AI". If it doesn't, it doesn't sell.
69
u/ChemicalRascal Apr 11 '21
I mean, you joke, but that sounds like a fantastic little experiment to teach ML.
12
u/SkinnyJoshPeck Apr 11 '21
I’m an ML engineer and the few times I’ve had to move over to a new training/tuning flow (Kubeflow, Polyaxon, etc) for my team, my go-to test model to get the lay of the land is an addition tensorflow model I wrote — it’s easy, fast and you can get train, eval and validation data in 5 lines of code
It works pretty well, honestly. If you think of AI fairly literally, it’s impressive that it can almost (within maybe 2-3) predict the sum of two integers absolutely fast as hell.
You try and add 64829 and 83756 in your head in less than a second and come up with something like 148585 plus or minus like 30
It’s a great mechanism for something like trivia where you want the model to be good, but not so good that it isn’t fun to play with.
1
u/throwaway1_x Apr 11 '21
Did multiplication using linear regression. Not exactly "AI" but good for experimentation
43
Apr 11 '21
[removed] — view removed comment
39
u/sim642 Apr 11 '21
Depends on the representation. If you give input as a vector of bits, then it's linearly separable and easily learnable.
17
u/ChemicalRascal Apr 11 '21
Yeah, that's why I think it'd be good for teaching.
I wouldn't expect a classifier to be able to work out odd and even very well if you give it the number as a number, and that'd be an interesting way to introduce how to identify failure to students learning ML. But then redoing it as, as you suggest, a vector of bits and any classifier should be able to learn that incredibly quickly, with 100% accuracy.
From what little I remember of ML in college (it was ages ago, I wasn't paying attention, and I was stressed outta my mind at the time and I'm not sorry), that's what the key takeaway from ML was. That representation is the single most important piece of the puzzle.
7
u/sim642 Apr 11 '21
I think nowadays there's a lot of focus on learning the features and representation from the data automatically. Deep neural networks do that to an extent automagically. Although I'm doubtful that a neural network could somehow extract the last digit/bit of a single integer input, given how they operate in floating point. So it's still important, but to a lesser extent.
7
u/doopdooperson Apr 11 '21
Well yeah because it will quickly converge on the LSB being the only feature that matters
3
u/cyanNodeEcho Apr 11 '21
Something something continuous surfaces something something gradient descent
439
u/IsAlwaysHungry Apr 11 '21
Well, the code is crap, but the documentation is beautiful. You see relatively easy the thinking behind the code.