• North Shore Beach Bum (unregistered) in reply to Zecc
    "Vale tudo" means something like "everything goes"/"everything's fair" in Portuguese.
    Valer means "to have value, or to be worth (something)" tudo means "everything"

    So, vale tudo means that everything has worth.

  • Jason (unregistered) in reply to snoofle

    Someone who doesn't know about sprintf("%100d", ...)

  • Peter (unregistered) in reply to mike
    mike:
    Common idiom:
    if( x1 ) {
      foo1();
    else if ( x2 ) {
      foo2();
    else if ( x3 ) {
      foo3();
    }
    Er - in what language is it a common idiom to have three open braces but only one close brace?
  • fjf (unregistered) in reply to Peter
    Peter:
    mike:
    Common idiom:
    if( x1 ) {
      foo1();
    else if ( x2 ) {
      foo2();
    else if ( x3 ) {
      foo3();
    }
    Er - in what language is it a common idiom to have three open braces but only one close brace?
    One brace to close them all ...
  • (cs)

    This can be implemented with five lines of code and zero bugs in Java 5.0_22.

    String zeroFill(String data) throws Exception
    {
    for (Integer maxipad = 0; !new Integer(101).equals(maxipad); maxipad++)
    for (String dots = ""; !maxipad.equals(dots.length()); dots+= '\u002e')
    if(java.util.regex.Pattern.compile(dots).matcher(data).matches()&&true)
    return this.getClass().newInstance().zeroFill("0".substring(0) + data);
    return data;
    }
    
    
  • Peter (unregistered) in reply to fjf
    fjf:
    Peter:
    Er - in what language is it a common idiom to have three open braces but only one close brace?
    One brace to close them all ...
    Preciouss likeses it!
  • (cs) in reply to informatimago

    I actually adore LISP. I left a note to that effect inside the article if you view-source.

  • Tio (unregistered)

    LOL!

  • Null (unregistered) in reply to snoofle

    Whats if Data is null. :)

  • Snarf (unregistered) in reply to snoofle
    snoofle:
    Snarf:
    I hope this was computer-generated code.
    What self-respecting computer would generate code like this?
    You appear to have interpreted my sarcastic post as a serious one.

    Please accept my sincerest apologies for not being more clear.

  • tsql (unregistered)

    select right(replicate('0',100) + ltrim(rtrim(cast(Data as varchar(100)))),100)

  • (cs) in reply to ParkinT
    ParkinT:
    Now that's a Maxi-Pad! And it appears to contain NO LEAKS.

    You go to hell! You go to hell and you die!

  • evnidetr (unregistered)

    Somebody call the brain police, clearly that programmer's has been stolen.

  • Eric (unregistered)
    (defun pad-zeros (in)
     (labels
      ((stupid-recurse (li c)
        (cond ((< c (- 100 (length in)))
    	   (stupid-recurse (cons #\0 li) (1+ c)))
         (t (map nil #'(lambda (x)
    		    (push x li))
    	 in)
          (nreverse li)))))
      (make-array 100
       :element-type 'character
       :initial-contents
       (stupid-recurse nil 0))))
    
  • Esse (unregistered)

    He obviously tried to make it thread-safe, in case the length changed in between if-test and substr.

  • Excelsior (unregistered)

    That is not possible. No sane person would have done that.

    I highly doubt about the truth of that WTF.

  • z f k (unregistered) in reply to too_many_usernames
    too_many_usernames:
    Ben L.:
    That's like cutting a sandwich into one piece...
    Normally I'm pretty forgiving, but I'm really interested in knowing how to cut something into a single piece!
    You have to be REALLY slow, to do it, so the part that you cut heals up. When finished, it will be whole again.

    Or, you can use some ice and a wire: http://www.csiro.au/resources/slicing-ice-activity.html

    CYA

  • ZanZan (unregistered) in reply to snoofle

    Why on earth the substring part? He substrings between 0 and length each time.

  • amr (unregistered) in reply to snoofle

    that ass, he should've used switch not if

    looool, JK

  • Chris (unregistered) in reply to snoofle
    snoofle:
    Snarf:
    I hope this was computer-generated code.
    What self-respecting computer would generate code like this?

    One that gets extra memory for ever line it codes?

  • Chris (unregistered) in reply to Jonathan
    Jonathan:
    This can be implemented with five lines of code and zero bugs in Java 5.0_22.
    String zeroFill(String data) throws Exception
    {
    for (Integer maxipad = 0; !new Integer(101).equals(maxipad); maxipad++)
    for (String dots = ""; !maxipad.equals(dots.length()); dots+= '\u002e')
    if(java.util.regex.Pattern.compile(dots).matcher(data).matches()&&true)
    return this.getClass().newInstance().zeroFill("0".substring(0) + data);
    return data;
    }
    
    

    It looks like php, which has a handy dandy built in function str_pad:

    return str_pad($input, 100, '0', STR_PAD_LEFT);

  • helmric (unregistered)

    What a wonderful peace of code! The "developer" has left room for future extensions with calculating the substrings starting point (even thoug they all starts from zero)

    Indeed nice one!

  • Jimmy Jones (unregistered) in reply to трпаьтонр
    трпаьтонр:
    It's performance optimisation

    Yep.

    Java strings are immutable and any changes to a string cause mallocs. You don't want to be adding the zeros one at a time or performance will take a nosedive.

  • (cs) in reply to Jimmy Jones
    Jimmy Jones:
    Java strings are immutable and any changes to a string cause mallocs. You don't want to be adding the zeros one at a time or performance will take a nosedive.
    Yep. Better to just concatenate all hundred zeros on at once and then take the last hundred characters as a substring. (Really! Then it's just inefficient!)
  • Alan (unregistered)

    Python:

    def maxipad(data):
        return data.rjust(100, '0')
    

    Or without

    str.rjust
    :

    def maxipad(data):
        return '0' * max(0, 100 - len(data)) + data
    
  • jes (unregistered) in reply to fjf
    fjf:
    jes:
    what about using the library functions? Perl, but works simialrly in C or Python
    
    sub pad {
        my ($n, $w) = @_;
        my $format = sprintf("%%%d.%dd", $w, $w);
        return sprintf ($format, $n);
    }
    
    Doesn't Perl support "sprintf ("%0*d", $w, $n)"? (Not that a variable width is required in the original problem in the first place ...)

    Yes, it does, I was too lazy to look up the details:

    sub pad {
        my ($n, $w) = @_; 
        return sprintf("%*.*d", $w, $w, $n);
    }
    printf "%s\n", pad(123, 50);
    
  • Robin (unregistered)

    I feel ill

  • anyone (unregistered) in reply to ParkinT

    try null as parameter

  • Rhialto (unregistered) in reply to Mark Bowytz
    Mark Bowytz:
    Zecc:
    I like the syntax highlighter. That's new, isn't it?.

    Yes - big kudos go out to Remy for suggesting and implementing.

    Syntax highlighter?

    Oh, the one that I had not seen because it requires Javascript, and therefore repeats the work over and over again on each viewer's browser, instead of just once statically before putting it on the webserver...

  • thequark (unregistered)

    LOL and it has O(1) complexity

  • Loathing computer? (unregistered) in reply to snoofle

    Maybe it is self-loathing one?

  • Shinobu (unregistered) in reply to Robyrt
    Robyrt:
    The 100-digit routing number is probably a concatenation of several smaller numbers, say ten 10-digit identifiers with only the last one being mandatory.
    In that case they simply should have used an array of numbers. This function shouldn't have to exist at all. TRWTF is storing just over 10 longs worth of fixed length data in a variable length container. (Or possibly, enforcing a fixed length of an inherently variable length list of numbers, it's hard to tell from the article.) All other problems flowed from this architectural error, and even if you coded perfectly, you still couldn't get rid of all the annoyances this causes. For example, if the length is greater than a hundred, or if non-digits appear, it should throw an exception, which you then either have to catch everywhere (probably dealing with it by asking the user to fix it and so on), or not (but in that case every call to the damn thing is a potential bug).
  • Skilldrick (unregistered)

    Well, seeing as no-one else is going to do it...

    This sort of thing is actually common in embedded systems, where there isn't space for library functions.

  • Skilldrick (unregistered)

    Cornify on LISP! Awesome Remy :)

  • (cs)

    -________________________________________________________-

  • Risky (unregistered)

    As it's about the only language it hasn't been written here

    strVBAPad= String(100 - Len(strInput), "0") & strInput

  • Olivier de Rivoyre (unregistered)

    The real WTF is to do not use a divide and conquier algo:

    function ZeroFill(Data) { if (Data.length + 64 <= 100) { Data= padleft0("", 64) + Data; } if (Data.length + 32 <= 100) { Data= padleft0("", 32) + Data; } if (Data.length + 16 <= 100) { Data= padleft0("", 16) + Data; } if (Data.length + 8 <= 100) { Data= padleft0("", 8) + Data; } if (Data.length + 4 <= 100) { Data= padleft0("", 4) + Data; } if (Data.length + 2 <= 100) { Data= padleft0("", 2) + Data; } if (Data.length + 1 <= 100) { Data= padleft0("", 1) + Data; } return Data; }

    Of course this way of doing presuppose that you already have the padleft0(s, i) function. /sarcasm

  • (cs)

    If you're havin' girl problems i feel bad for you son I got 99 problems but a bitch ain't one

  • Camel Case (unregistered)

    I deplore the absence of XML in the implementation

  • (cs) in reply to Peter
    Peter:
    mike:
    Common idiom:
    if( x1 ) {
      foo1();
    else if ( x2 ) {
      foo2();
    else if ( x3 ) {
      foo3();
    }
    Er - in what language is it a common idiom to have three open braces but only one close brace?

    I'm sure he meant

    if( x1 ) {
    foo1();
    } else if ( x2 ) {
    foo2();
    } else if ( x3 ) {
    foo3();
    }

    And to whoever had the 'one brace' line, well done

  • (cs) in reply to Ben L.
    Ben L.:
    var length = Data.length;
    var NewData;
    if (length >= 100) {
        NewData = Data.substring(0,length);  
    }
    I know what it's supposed to do, but I think they missed the point entirely. That's like cutting a sandwich into one piece and expecting to be able to share it without any further action.

    If the meat in the sandwich is prime rib, you can only divide it by itself, so you only get one piece.

  • wytbw (unregistered)

    I've seen that style of coding before... so your company utilizes the amazing skillz of offshore developers too?

  • loljavascript (unregistered)

    yup, javascript is the basic of our times. retards inc!

  • Hourly Paid (unregistered)

    If you're paid hourly to do a job, you would want to stretch it as far as possible. ;)

  • Ken (unregistered)

    Well, it works, doesn't it?!?

  • Ben (unregistered) in reply to snoofle

    One with very low self esteem

    Captca: verror- the fith error

  • (cs) in reply to jes
    jes:

    Yes, it does, I was too lazy to look up the details:

    sub pad {
        my ($n, $w) = @_; 
        return sprintf("%*.*d", $w, $w, $n);
    }
    printf "%s\n", pad(123, 50);
    

    This was as stupid an attempt as I could come up with:

    sub pad {
      my $n = shift;
      my $w = $_[0];
    
      do {
        $n = '0' . $n
      } until length($n) == length($w);
    
      return $n;
    }
    
    print pad(123, 100);
    

    I tried to make it more convoluted but lost initiative.

    On the plus side, if $w <= $n, it'll give you an infinite loop, and the return value is actually a string (not that it matters much).

    But the cool part is that you could really screw with someone down the line, since "print pad(123, 'four');" would work just fine in a very non-obvious way.

  • Paul (unregistered)

    My usual trick is to prepend 100 0s and then truncate at 100 right justified

  • (cs)

    .Net version:

    return Data.ToString("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
    
    
  • (cs)

    I know my post is late, but I was busy integrating the mainframe again. Anyway...

    WTF was this developer thinking?! Obviously they should have done the length tests from low-to-high, like this:

           if (length == 1) {
              NewData = "0" + Data.substring(length-1,length);
           }
           if (length == 2) {
              NewData = "0" + Data.substring(length-2,length);
           }
    
           /*  ...SNIP...  */
    
           if (length == 97) {
              NewData = "0" + Data.substring(length-97,length);
           }
           if (length == 98) {
              NewData = "0" + Data.substring(length-98,length);
           }

    Which will 1) optimize the code so that only one "0" per test is needed, saving space, and 2) that each test is true for (length >= n), ensuring full CPU usage.

Leave a comment on “Maximum Pad”

Log In or post as a guest

Replying to comment #312027:

« Return to Article