- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The last one is nice and compact, though I wince every time I come across
!!
and other abuse of "truthiness".With regards to @Remy's comments section: did the professor grade you more harshly or did it turn out alright?
Admin
So either the test passes, or it enters an infinite loop. Brillant!
I though VARCHARs supported variable length strings?Admin
Admin
It turned out all right, although I remained a dick through the whole project. It was a group project, right? So I volunteered to be the "centerpoint" in the whole process- everybody write their modules, and I'd put them together. I wrote the whole thing and threw out everybody else's code.
I was an incredible asshole when I was young.
Admin
Admin
It took me a while to understand the factorial example. I'd have assumed (1) that the !-operator was overloaded for integers to return the binary complement, (2) that false was converted to -1 instead of +1.
I copied the function to a C++ environment to verify its functionality. (I spend definitively too much time with C# etc.)
Still, I wonder, doesn't the conversion from int to bool and back take more CPU cycles than simply subtracting 1 resp. using 1 directly? So what is the point of this?
Admin
Being a jerk to your prof?
Admin
I've done some silly one-liners when I was in school. Things like:
Yes, this was useful code in a real assignment (apart from the variable names).
Admin
taking the second entry in a string consisting of words separated by spaces?
I've done similar things in SQL
Admin
That code isn't for trimming trailing spaces, it's for right-aligning a 2 character field by left-padding it with a space if it's trimmed length is 1 character. Not the most efficient way to do it, I would simply test the trimmed length first and only pad if the length is 1, but the real WTF is that both the submitter and editor can't understand what 3 very simple lines of code do.
The code checking the stack trace reeks of someone either being in a hurry or trying to be overly clever. Sure enough, a Google search reveals that this is an open problem - if the submitter thinks he has a foolproof way to do this, please build it into a library for the rest of us to use. In fact, the second answer (from 2009) on this StackOverflow post seems to be the source of this code: http://stackoverflow.com/questions/1535012/can-java-scala-etc-code-tell-when-it-is-being-run-by-tomcat
And I readily admit that I don't get the factorial code. What does the !! operator do?
Admin
It converts truthy values to 1 and falsy values to 0.
Admin
Double use of the ! (not) operator. In languages where 0 means false and anything else is true, if the number is 0 it stays as 0, if not it converts it to 1
Admin
Too obvious. But it seems to be the only viable really concise way to do it.
doesn't work.
Edit
does work in C#.
Admin
TIL it isn't that everyone stopped putting funny comments in articles, it's that the new site apparently broke my Show Comments bookmarklet somehow.
Admin
That's undefined behavior.
*
is not a sequence point (C++: does not create a "sequenced-before" relationship).Admin
Well, assuming i > 0...
Admin
A winner.... Not to mention WHY change the value of n at all??? You simply want to pass (n-1) to the (recursive) call.
Admin
In a few semesters, Corey will learn about tail call optimization and find yet another reason why his professor was unamused.
Admin
FTFY
Admin
Oh, there are plenty of groups he can exasperate. No need to limit it to just one or the other.
Admin
Yep.
Short code is not always easy for others to understand.
I remember when one of my coworkers had a large data file and wanted to plot 2 columns, but his excel pretty much died whenever he tried to open the file to plot it (bonus WTF for trying to use excel for science to begin with, but I digress..)
My solution to his problem was to ask if he realy needed all 2 milion data points (or so) or if it would be enough to simply plot every 5 or 10 point or so.
He said yes and I presented the following gem for him:
perl -pne '$_=""if($i++%5)' data.txt
He looked at me as if I was from Mars.... Fun times :smile:
Admin
Reminds me of a task when I still was in university where we were supposed to write a program (in a programming language of our choice) which would print out for each command line argument, whether the value is a string consisting of only validly balanced parentheses (i. e. the empty string or
(()())
is ok, but)
or())(()
ornonsense
is not).I submitted this perl one-liner:
I got my credits, but the tutor asked me if I can explain to him why and how it works (and I think next year the requirement was changed to list allowed programming languages).
Admin
Don't trust the return address, no really. You should never use the return address of the function calling you to make any kind of security decision, since there's no security boundary. Java code isn't any safer than C since JNI code could just as easily do the same tricks.
In this case, it's not making a security decision, but it's still a bad idea in general to use the caller to make any kind of decision, security-related or not.
Admin
https://www.youtube.com/watch?v=ehvpSWnW_LQ
Admin
If you're letting untrusted code load JNI code of its choosing, you've already lost.
Admin
If you're running untrusted Java code, you've already lost.
Admin
If you're running Java, you've already lost. :P
Admin
The factorial function shown is even more cryptic when converted to Perl 6:
In part because
? :
is written as?? !!
in Perl 6. ( in part to save the:
for more interesting uses )For small values of
n
( up to about 10,000 ) the most minimal is:Which is short for
The common example given is to create a new operator with a precedence that is tighter than
&infix:<*>
Admin
No one has yet mentioned the inherent pun of deliberately (torturously?) using the
!
operator in a factorial function? It's just a pity that it's a prefix rather than postfix operator...I do like the fact that
!0
==
0!Admin
I can understand his look. Every sane person would sense that perl is overkill here and would just use awk:
Or if gnu sed is available maybe just
Admin
Hey, we can solve one problem with this kind of truthiness:
boolean b = !!FILE_NOT_FOUND;
Admin
TRWTF of factorial function is not using unsigned, at least for the argument (and as a commodity for the result). So far it is bugged for 50% of input, so it deserves only half the full score for the exercise.
(not considering that any value bigger than the number of fingers from a panda would fail to provides an accurate value on usual 32 bits systems)
Admin
That are two of the (intended) points of that line of code:
"But it works on my machine!"
"Side effect? What side? What effect?"
FTFY
You just crunched my hope for if - else - otherwise constructs.
Admin
Not golfed enough.
Admin
Ahh, fun days!!!! I had a similar, and chose a language named FOCAL. I munged a few of the programs required to challenge a course, but the professor did not know the language so asked me in to explain how the code worked since it "seemed" wrong.
I said, "Let me have a copy of my answers, I will take them to where I work and run them on a hard copy terminal". He agreed, and two days later I returned with all of the (broken) programs running and producing the right answers.
He was quite confused, so I came clean....I had hacked (in the original sense) the interpreter so that it accepted the programs! I then pulled out a fanfold printout (about 3" thick) with the assembly source of the modified language.
We had a good laugh, and I was given credit not only for that semesters class but also a few additional classes...
Admin
Hey the CPUWizard's back again!
Admin
I have never left, you just have not been paying attention. :stuck_out_tongue_winking_eye:
Admin
Well it's hard to notice you when you're not blathering on about how great you are, how Intel gives you super-secret prototypes, how expensive your New York offices are, etc.
Where's the endless bragging?
Admin
2/10 needs more caps lock.