• Brian White (unregistered) in reply to Brian White

    Err, "write" this I mean.

  • Observant (unregistered) in reply to ParkinT
    ParkinT:
    Now that's a Maxi-Pad! And it appears to contain NO LEAKS.
    Except for if the box is left blank.

    CAPTCHA: validus : Latin meaning valid

  • FuBar (unregistered) in reply to Mike
    Mike:
    I would have done this in COBOL. Perfect for that!
    Those who don't know COBOL end up re-inventing it, eventually and badly.
  • Addem (unregistered) in reply to Ford
    Ford:
    You know what's the best part (besides the carefully typed out 0s)? That they used "substring" at all. I can't see any reason, every time they called it they used the entire string!
    But that's how my friend said to do it.
  • Ben L. (unregistered)
    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.

  • Friend (unregistered) in reply to Addem
    Addem:
    But that's how my friend said to do it.
    Because that's how we did it the last place I worked.
  • (cs) in reply to Ben L.
    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!
  • Some Wonk (unregistered)

    David St. Hubbins: It's such a fine line between stupid, and clever.

  • Ryan (unregistered)

    This person could have AT LEAST used else ifs

  • Pete (unregistered)

    Holy sh*t dudes

  • kftt (unregistered) in reply to @Deprecated
    @Deprecated:
    so I can print out TDWTF code! Now all I need is some matches...

    But i would suggest using the metal table for that. Or an oaken table.

  • Axl (unregistered) in reply to frits
    frits:
    "I don't know which is the more entertaining wtf: allowing strings longer than 100 characters, or calling substring with an index of 0 and length equal to the string's total length.".substring(0, "I don't know which is the more entertaining wtf: allowing strings longer than 100 characters, or calling substring with an index of 0 and length equal to the string's total length.".length).toString()

    FTFY.

  • EngleBart (unregistered) in reply to ParkinT
    ParkinT:
    Now that's a Maxi-Pad! And it appears to contain NO LEAKS.
    You are treading on dangerous ground.

    Apple has filed for a trademark on "max-iPad" It will blow the doors off of the current iPad. Packed with more memory, but fewer memory leaks.

  • Malkocoglu (unregistered)

    Maybe the compiler can optimize it to the point that a sane coder would write it :-)

  • Some Jerk (unregistered)

    This is the part of the code that I think is the most useful:

    function ZeroFill(Data) {

    var length = Data.length;

    var NewData;

    if (length >= 100) {

    NewData = Data.substring(0,length);

    }

    /// should be followed by: perhaps replacing: "NewData = Data.substring(0,length);" with: if(NewData != Data.substring(0,length)) writeln( "I am sorry to inform you that your computer is ill." );

    Captcha: usitas (it is... because it is too expensive to fix)

  • jrh (unregistered) in reply to fjf
    fjf:
    jrh:
    Wouldn't it be easier to maintain if he had written it like this?
       var length = Data.length;  
       var NewData;  
       if (length = 1) {  
          NewData = "0" = Data;  
       }  
       if (length == 2) {  
          NewData = "0" = Data;  
       }  
       if (length == 3) {  
          NewData = "0" = Data;  
       }  
       /* 
        ... 
        SNIPPED FOR REALLY LONG AND REPETITIVE CODE 
        ... 
       */  
       if (length == 98) {  
          NewData = "0" = Data;  
       }  
       if (length == 99) {  
          NewData = "0" = Data;  
       }  
       return NewData; 
    

    That's how I would have done it...

    At least the original code works (module NoAstronomer's comment). In your code I count 4 different errors at first glance, and if it worked as I suppose it's meant to, it would be vastly less efficient than the original code (copying the string up to 99 times).

    How embarrassing. It should look more like this:

    if (length == 1) {
    Data = "0" + Data;
    } if (length == 2) {
    Data = "0" + Data;
    }

    Sorry for the confusion caused by my inattention to code produced try to make a joke. It clearly interfered with your ability to detect sarcasm.

  • Some Jerk (unregistered) in reply to Some Jerk
    Some Jerk:
    This is the part of the code that I think is the most useful:

    function ZeroFill(Data) {

    var length = Data.length;

    var NewData;

    if (length >= 100) {

    NewData = Data.substring(0,length);

    }

    /// should be followed by: perhaps replacing: "NewData = Data.substring(0,length);" with: if(NewData != Data.substring(0,length)) writeln( "I am sorry to inform you that your computer is ill." );

    Captcha: usitas (it is... because it is too expensive to fix)

    OOPS :). so much stuff to make fun of... we will just forget I ever said anything.

  • Anonymous Coward (unregistered) in reply to Brian White
    Brian White:
    I don't know what you are talking about. The DWTF is not how you would right this in VB. The very first one line example answer works perfectly well in VB/VBA/VBScript. There is a Right function in VB languages.

    Right("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & "12345", 100) works fine.

    Shouldn't that be something like:

    Left("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 100-len("12345")) & "12345"

    ?

    captcha: nobis - a noble noob

  • Long (unregistered)

    Com'n now. He simply unrolled the loop.

  • someone (unregistered)

    ==== Teacher mode on ====

    function Pad(data as String, c as Char, size as Integer)
    {
      if (data.length() >= size) {
        return data.subString(0, size);
      }
      else
      {
        return c + Pad(data, c, size);
      }
    }
    ==== Teacher mode off ====

    Well, coming to think of it, I'd probably use a for-loop. But hey, it's almost a text-book example for this kind of stuff...

  • the beholder (unregistered) in reply to Anonymous Coward
    Anonymous Coward:
    Brian White:
    I don't know what you are talking about. The DWTF is not how you would right this in VB. The very first one line example answer works perfectly well in VB/VBA/VBScript. There is a Right function in VB languages.

    Right("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & "12345", 100) works fine.

    Shouldn't that be something like:

    Left("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 100-len("12345")) & "12345"

    ?

    Nope. Brian is right (no pun intended).

    I just think you do not need a hundred zeroes either, because I believe VB has a function to replicate a string. (Gladly, my VB-coding days are looooong gone.)

  • (cs)

    Ahh the joys of being paid by the line.

  • JJ (unregistered) in reply to the beholder
    the beholder:
    Anonymous Coward:
    Brian White:
    I don't know what you are talking about. The DWTF is not how you would right this in VB. The very first one line example answer works perfectly well in VB/VBA/VBScript. There is a Right function in VB languages.

    Right("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & "12345", 100) works fine.

    Shouldn't that be something like:

    Left("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 100-len("12345")) & "12345"

    ?

    Nope. Brian is right (no pun intended).

    I just think you do not need a hundred zeroes either, because I believe VB has a function to replicate a string. (Gladly, my VB-coding days are looooong gone.)

    That would be Right$(String$(100, "0") & stringToPad, 100) No need for the subtraction; just take the right 100 digits every time, like in the original code.

  • edthered (unregistered) in reply to frits
    frits:
    I don't know which is the more entertaining wtf: allowing strings longer than 100 characters, or calling substring with an index of 0 and length equal to the string's total length.

    I was thinking the same thing... the endless if's are one thing, but the wtf moment is in the first one... and they aren't even if/else's so each one has to be evaluated.

  • Dan (unregistered)

    what, no switch??

  • (cs) in reply to Remy Porter

    You sure do! Lots and lots of charming ones.

  • I just hope... (unregistered) in reply to ParkinT

    they were REALLY REALLY careful about counting the right number of zeros in every of the ONE HUNDRED use cases...

  • fjf (unregistered) in reply to jrh
    jrh:
    Sorry for the confusion caused by my inattention to code produced try to make a joke. It clearly interfered with your ability to detect sarcasm.
    OK, 3 bugs fixed, 1 to go, and still inefficient. And your point is?
  • Anon (unregistered) in reply to jrh
    jrh:
    How embarrassing. It should look more like this:

    if (length == 1) {
    Data = "0" + Data;
    } if (length == 2) {
    Data = "0" + Data;
    }

    Sorry for the confusion caused by my inattention to code produced try to make a joke. It clearly interfered with your ability to detect sarcasm.

    Apparently your inattention to code is rivaled only by your inattention to English.

  • jamza (unregistered) in reply to snoofle

    One who was being paid per line generated.

  • Valetudo (unregistered)

    The correct way would of course be:

    import std.range; NewData = take(cycle("0"), 100 - length) ~ Data;

  • d00d (unregistered)

    Uh-oh, looks like we've been hacked by Perez Hilton.

  • Martin (unregistered)

    I like how his >=100 check doesn't even work....

  • Buddy (unregistered)

    Truly horrifying. I hope it's a joke. Even someone coming out of the hackiest fly-by-night six-month tech "college" would do better. Hell, even a kid straight out of Comp Ed in high school would do better.

    Of course PHP already has something built in!

    function pad_left($input, $padding = '0', $length = 100)
    {
        return str_pad($input, $length, $padding, STR_PAD_LEFT);
    }
  • Patrick (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!
    That's easy, just don't cut it all the way through.
  • Christian (unregistered)

    Was the code at least correct? :-)

  • The Big Lebowski (unregistered) in reply to Wade

    Actually, VB (6.0 and earlier; not VB.NET) would make coding this problem more efficiently than the Java/.NET solutions offered: VB has a native "String$(number,string)" function which returns a string repeated "number" of times.

    Instead of having to manually define a string of 100 "0" characters, VB would allow you to reduce this function to a one-liner:

    Function ZeroFill(Data as String) As String

     return String$(100 - len(Data),"0") + Data
    

    End Function

    I'm sure there's Java / C# libraries that would implement the same functionality as the "String$" function, but VB 6.0 provides a decent solution using "native" calls.

    VB 6.0 FTW! :-P

  • (cs) in reply to The Big Lebowski
    The Big Lebowski:
    Actually, VB (6.0 and earlier; not VB.NET) would make coding this problem more efficiently than the Java/.NET solutions offered: VB has a native "String$(number,string)" function which returns a string repeated "number" of times.

    Instead of having to manually define a string of 100 "0" characters, VB would allow you to reduce this function to a one-liner:

    Function ZeroFill(Data as String) As String

     return String$(100 - len(Data),"0") + Data
    

    End Function

    I'm sure there's Java / C# libraries that would implement the same functionality as the "String$" function, but VB 6.0 provides a decent solution using "native" calls.

    VB 6.0 FTW! :-P

    Your definition of efficiency is amusing to me.

  • jrh (unregistered) in reply to fjf
    fjf:
    jrh:
    Sorry for the confusion caused by my inattention to code produced try to make a joke. It clearly interfered with your ability to detect sarcasm.
    OK, 3 bugs fixed, 1 to go, and still inefficient. And your point is?

    OK, I admit I didn't test the code I posted, and it was buggy. Next time I try to make a joke I'll be careful to do it with tested code, so we don't have to get bogged down in the details.

    My point, which has been totally lost by now, was to make it worse, and thereby possibly make someone chuckle. If I were to actually implement this functionality I'd probably just do this in C#

    return data.PadLeft(100, '0');

  • Anon (unregistered) in reply to The Big Lebowski
    The Big Lebowski:
    Actually, VB (6.0 and earlier; not VB.NET) would make coding this problem more efficiently than the Java/.NET solutions offered: VB has a native "String$(number,string)" function which returns a string repeated "number" of times.

    Instead of having to manually define a string of 100 "0" characters, VB would allow you to reduce this function to a one-liner:

    Function ZeroFill(Data as String) As String

     return String$(100 - len(Data),"0") + Data
    

    End Function

    I'm sure there's Java / C# libraries that would implement the same functionality as the "String$" function, but VB 6.0 provides a decent solution using "native" calls.

    VB 6.0 FTW! :-P

    Yes, .NET has both a String.PadLeft and String.PadRight function that does this as a one liner:

    public string ZeroFill(string data) { return data.PadLeft(100,"0"); }

    It doesn't even require you to work out the length of your string and how many extra characters you need to pad. It does it all for you. In fact, there really isn't much point in bothering with writing the ZeroFill function in the first place.

  • (cs)

    You can make the code an order of magnitude smaller:

    function ZeroFill(Data) {
           var zeros = "";
           
           switch (Data.length) {
              case 0:
                 zeros += "0";
              case 1:
                 zeros += "0";
              case 2:
                 zeros += "0";
              case 3:
                 zeros += "0";
              case 4:
                 zeros += "0";
              /*
              ...
              snip
              ...
               */
              case 98:
                 zeros += "0";
              case 99:
                 zeros += "0";
           }
           
           return zeros + Data;
    }
    
  • (cs) in reply to Drew
    Drew:
    Clearly it should've been this...

    var oneHundredZeroes = "0000....0000"; var length = Data.length;

    return oneHundredZeroes.substring(100-length) + data;

    /sarcasm

    But then you would have to type in 100 zeros!

    I would do this:

    var hundred = "";
    while(hundred.length < 100) {
        hundred += "0";
    }
    return hundred.substring(100-length) + data;
    
  • mike (unregistered) in reply to JohnFx
    JohnFx:
    Hector Virgen:
    This could be improved by using "else if".

    Sure. If the programmer wanted to achieve the world record for indenting depth and didn't have a code obfuscator handy.

    Common idiom:

    if( x1 ) {
      foo1();
    else if ( x2 ) {
      foo2();
    else if ( x3 ) {
      foo3();
    }
    
  • fjf (unregistered) in reply to joeyadams
    joeyadams:
    You can make the code an order of magnitude smaller:
    function ZeroFill(Data) {
           var zeros = "";
           
           switch (Data.length) {
              case 0:
                 zeros += "0";
              case 1:
                 zeros += "0";
              case 2:
                 zeros += "0";
              case 3:
                 zeros += "0";
              case 4:
                 zeros += "0";
              /*
              ...
              snip
              ...
               */
              case 98:
                 zeros += "0";
              case 99:
                 zeros += "0";
           }
           
           return zeros + Data;
    }
    

    This code calls for optimization using Duff's device.

  • informatimago (unregistered)

    Why the hate toward Lisp?

    In lisp, we wouldn't even need a library, it's in the core language:

    (format nil "~100,'0D" input)

  • Edgars (unregistered)

    Genius. I wish every company had such dedicated employee....

  • (cs) in reply to The Big Lebowski
    The Big Lebowski:
    VB would allow you to reduce this function to a one-liner:

    Function ZeroFill(Data as String) As String

     return String$(100 - len(Data),"0") + Data
    

    End Function

    VB 6.0 FTW! :-P

    cough< You mean:

    Function ZeroFill(Data as String) As String

     <b>ZeroFill =</b> String$(100 - len(Data), "0") <b>&</b> Data
    

    End Function

    FTFY. :)

  • Simon (unregistered) in reply to Anon
    Anon:
    jrh:
    Wouldn't it be easier to maintain if he had written it like this?
       var length = Data.length;  
       var NewData;  
       if (length = 1) {  
          NewData = "0" = Data;  
       }  
       if (length == 2) {  
          NewData = "0" = Data;  
       }  
       if (length == 3) {  
          NewData = "0" = Data;  
       }  
       /* 
        ... 
        SNIPPED FOR REALLY LONG AND REPETITIVE CODE 
        ... 
       */  
       if (length == 98) {  
          NewData = "0" = Data;  
       }  
       if (length == 99) {  
          NewData = "0" = Data;  
       }  
       return NewData; 
    

    That's how I would have done it...

    Ignoring what I assume is a typo (should be NewData = "0" + Data;). This is brillant. It keeps most of the WTF from the original and yet manages to make it less efficient.

    And also broken, since 'length' never changes, nor does 'Data'. As implemented, the entire function is a verbose way of saying 'return "0" + Data;'.

  • kirk (unregistered)

    I can't believe they didn't use a case statement.

    Actually I also like if (length >= 100) { NewData = Data.substring(0,length);}

    could at least bother to trim to 100 chars :-)

  • Simon (unregistered) in reply to fjf
    fjf:
    This code calls for optimization using Duff's device.

    No, this code calls for optimization with a blunt instrument, and a way of securely disposing of a body.

Leave a comment on “Maximum Pad”

Log In or post as a guest

Replying to comment #:

« Return to Article