• (disco)

    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?

  • (disco)
    while (i <= location.length) {
        if (i === location[i - 1].rank) {
            i++;
        }
    };
    

    So either the test passes, or it enters an infinite loop. Brillant!

    PaulaBean:
    Some fields, like Print_Code, are VARCHAR(2), which means many records end up with a trailing space.
    I though VARCHARs supported variable length strings?
  • (disco) in reply to VinDuv
    VinDuv:
    I though VARCHARs supported variable length strings?
    I have no doubt that there is an application out there which always puts spaces in empty fields when it was ported from a fixed-space format to a RDBMS.
  • (disco) in reply to JBert

    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.

  • (disco) in reply to Remy
    Remy:
    I was an incredible asshole when I was young.
    Does that mean you're now a *credible* asshole? :stuck_out_tongue_winking_eye: https://www.youtube.com/watch?v=g-4-gLlF0uw <!-- Emoji'd by MobileEmoji 0.2.0-->
  • (disco)

    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?

  • (disco) in reply to PWolff

    Being a jerk to your prof?

  • (disco)

    I've done some silly one-liners when I was in school. Things like:

    String dest = source.substring(source.indexOf(' '), source.indexOf(' ', source.indexOf(' ') + 1));
    

    Yes, this was useful code in a real assignment (apart from the variable names).

  • (disco) in reply to hungrier

    taking the second entry in a string consisting of words separated by spaces?

    I've done similar things in SQL

  • (disco)

    Wendy’s co-worker cleaned out that extra white-space with this block:

    String print_code = " ".concat(dto.getPrint_code().trim()); if (print_code.length() == 3) { print_code = print_code.trim(); }

    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?

  • (disco) in reply to AwesomeRick

    It converts truthy values to 1 and falsy values to 0.

  • (disco) in reply to AwesomeRick
    AwesomeRick:
    What does the !! operator do?

    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

  • (disco) in reply to aliceif
    aliceif:
    Being a jerk to your prof?

    Too obvious. But it seems to be the only viable really concise way to do it.

    int f(int n){return n?n*f(--n):1;}
    

    doesn't work.

    Edit

    static int f(int n) { return System.Convert.ToBoolean(n) ? n * f(--n) : 1; }
    

    does work in C#.

  • (disco)

    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.

  • (disco) in reply to PWolff
    int f(int n){return n?n*f(--n):1;}
    

    That's undefined behavior. * is not a sequence point (C++: does not create a "sequenced-before" relationship).

  • (disco) in reply to VinDuv
    VinDuv:
    So either the test passes, or it enters an infinite loop. Brillant!

    Well, assuming i > 0...

  • (disco) in reply to NYKevin
    That's undefined behavior. * is not a sequence point (C++: does not create a "sequenced-before" relationship).

    A winner.... Not to mention WHY change the value of n at all??? You simply want to pass (n-1) to the (recursive) call.

  • (disco)

    In a few semesters, Corey will learn about tail call optimization and find yet another reason why his professor was unamused.

  • (disco) in reply to kstrauser
    kstrauser:
    In a few semestersyears, Corey will learn about tail call optimizationmaintainability and find yet another reason why his professor wascoworkers are unamused.

    FTFY

  • (disco) in reply to Yamikuronue

    Oh, there are plenty of groups he can exasperate. No need to limit it to just one or the other.

  • (disco) in reply to kstrauser

    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:

  • (disco)

    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 ())(() or nonsense is not).

    I submitted this perl one-liner:

    #!/usr/bin/perl
    for(@ARGV){0while s/\(\)//;print+(/./?"Not ":"")."OK\n"}
    

    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).

  • (disco) in reply to AwesomeRick
    AwesomeRick:
    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

    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.

  • (disco) in reply to RaceProUK
    RaceProUK:
    Does that mean you're now a *credible* asshole? :stuck_out_tongue_winking_eye: https://www.youtube.com/watch?v=g-4-gLlF0uw

    <!-- Emoji'd by MobileEmoji 0.2.0-->

    https://www.youtube.com/watch?v=ehvpSWnW_LQ

  • (disco) in reply to Protoman
    Protoman:
    Java code isn't any safer than C since JNI code could just as easily do the same tricks.

    If you're letting untrusted code load JNI code of its choosing, you've already lost.

  • (disco) in reply to dkf
    dkf:
    If you're letting untrusted code load JNI code of its choosing, you've already lost.

    If you're running untrusted Java code, you've already lost.

  • (disco) in reply to Protoman
    Protoman:
    If you're running untrusted Java code, you've already lost.

    If you're running Java, you've already lost. :P

  • (disco)

    The factorial function shown is even more cryptic when converted to Perl 6:

    sub f(\n){n??f(n-!!n)*n!!!n}
    

    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:

    sub f{[*] 2..$^n}
    

    Which is short for

    sub f ( $n ) { (2..$n).reduce: &infix:<*> }
    

    The common example given is to create a new operator with a precedence that is tighter than &infix:<*>

    sub postfix:<!> ( Int() \n where * >= 0 ) is tighter(&[*]) { [*] 2..n }
    say 5!;
    
  • (disco)

    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!

  • (disco) in reply to Yazeran
    Yazeran:
    ... 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:

    I can understand his look. Every sane person would sense that perl is overkill here and would just use awk:

    awk 'NR%5==1' data.txt
    

    Or if gnu sed is available maybe just

    sed -n '1~5p' data.txt
    
  • (disco) in reply to JBert

    Hey, we can solve one problem with this kind of truthiness:

    boolean b = !!FILE_NOT_FOUND;

  • (disco)

    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)

  • (disco) in reply to NYKevin
    NYKevin:
    int f(int n){return n?n*f(--n):1;}

    That's undefined behavior. * is not a sequence point (C++: does not create a "sequenced-before" relationship).

    TheCPUWizard:
    A winner.... Not to mention WHY change the value of n at all??? You simply want to pass (n-1) to the (recursive) call.

    That are two of the (intended) points of that line of code:

    • "But it works on my machine!"

    • "Side effect? What side? What effect?"

     

    Yamikuronue:
    kstrauser:
    In a few semestersyears, Corey will learn about tail call optimizationmaintainabilitybecome department head and find yet anotherwon't see any reason why his professor wascoworkers are unamused.

    FTFY

    FTFY

     

    CoyneTheDup:
    boolean b = !!FILE_NOT_FOUND;

    You just crunched my hope for if - else - otherwise constructs.

  • (disco) in reply to Yazeran
    Yazeran:
    perl -pne '$_=""if($i++%5)' data.txt

    Not golfed enough.

    perl -pe '$_=""if$.%5'
    
  • (disco) in reply to mihi
    write a program (in a programming language of our choice)

    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...

  • (disco) in reply to TheCPUWizard

    Hey the CPUWizard's back again!

  • (disco)
    blakeyrat:
    Hey the CPUWizard's back again!

    I have never left, you just have not been paying attention. :stuck_out_tongue_winking_eye:

  • (disco) in reply to TheCPUWizard

    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?

  • (disco) in reply to blakeyrat

    2/10 needs more caps lock.

Leave a comment on “Efficient WTFery”

Log In or post as a guest

Replying to comment #:

« Return to Article