Variety of coding and coding

Some argue that programming is an art and, ultimately, I can only agree especially when it came across very different solutions for the same problem. To understand how true this is how an identical need in medisimo language (JavaScript) can be solved by completely different approaches and original.

Left zero pad

. A number, but the discussion is also valid for any string, such as 123 can be filled on the left - to tie - with a number of zeros, eg 00123 . This need has on several occasions and is useful to put in a column - or at least show - a number in a clean way, implicitly indicating its maximum value. . In the game, for example, the classical score (the score) is often referred to 001234 , indicating that at most we will arrive at 999999 . Here's how the same problem has been solved by various developers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/ **
* Left Pad String
*
* @ From http://snipplr.com/view/8423/left-pad-string/
* @ Author web-http://www.mechanicmatt.com/
*
* @ Param num - Striga to fill
* @ Param totalChars - Total number of characters, including the "zeros"
* @ Param padWidth - Character used to fill, default "0"
* /
num , totalChars , padWith ) { leadingZeros function (num, totalChars, padWith) {
"" ; num = num + "";
padWith ) ? padWith : "0" ; padWith = (padWith)? padWith: "0";
num. length < totalChars ) { if (no. length <totalChars) {
num. length < totalChars ) { while (no. length <totalChars) {
num ; num = num + padWith;
}
{ } Else {}}

num. length > totalChars ) { //if padWith was a multiple character string and num was overpadded if (no. length> totalChars) {/ / if padWith was multiple character string and num was overpadded
( ( num. length - totalChars ) , totalChars ) ; num = num. substring ((no. length - totalChars), totalChars);
{ } Else {}}

return num;
}
leadingZeros ( "asdf" , 10 , "0" ) ) ; alert (leadingZeros ("asdf", 10, "0"));

scegliendo anche il tipo di carattere da usare tramite padWidth , invece del default 0 . This solution is extremely articulate, yet allows you to add any number of 0 to the number num choosing the type of font used by padWidth , instead of the default 0 .
Very original, however, this solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
/ **
* String_pad
*
* @ From http://snipplr.com/view/700/stringpad/
* @ Author http://d.hatena.ne.jp/brazil/20060721/1153489937
*
* @ Param str - Striga to fill
* @ Param len - Number of characters, including the "zeros"
* @ Param ch - character used to fill
* /
str , len , ch ) { return new Array ( len - ( '' + str ) . length + 1 ) . join ( ch ) + str } function pad (str, len, ch) {return new Array (len - (''+ str). length + 1). join (ch) + str}

pad ( 56 , 4 , '0' ) ) ; // 0056 alert (pad (56, 4, '0 ')) / / 0056

Same result, with a completely different approach. . Again we have the opportunity to decide the number of padding characters using the parameter len and type of font to use with ch . Missing is the use of default provided in the above function.
The one I use, but ...:

1
2
3
4
5
6
7
8
9
10
11
12
/ **
* String_pad
*
* @ Author Giovambattista Fazioli
* @ Web http://www.undolog.com
*
* @ Param s - Striga to fill
* @ Param s - the character string that indicates is that the length
* Such as "0000" = "0" character length 4
* /
s , l ) { return ( l. substr ( 0 , ( l. length - s. length ) ) + s ) ; } padding function (s, l) {return (l substr (0, (l length - length s)) + s);}
padding ( '123' , '0000' ) ) ; alert (padding ('123 ', '0000'));

If you have other interesting solutions do not hesitate to comment : D

16 comments to "A variety of coding and coding"

  1. October 14, 2008 Napolux :

    Line 12 of your guess function must be:

     alert (padding ('123 ', '0000'));
    
  2. October 14, 2008 Giovambattista Fazioli :

    @ Napolux:

    Line 12 of your guess function must be: alert (padding ('123 ', '0000'));

    Correct! : D

  3. October 15, 2008 Francis Gaven :

    Interesting, the 3 different approaches! :)

  4. October 20, 2008 Camelize, CamelCase | Undolog.com :

    [...] Already seen in the post Variety of coding and coding approaches that a developer may have to solve a problem are different and [...]

  5. November 6, 2008 unwiredbrain:

    I tested the timing of all three methods. The first is expected to be slower, while the third is damn fast.

    I launched 100000 times each method in order to make him the padding to ten digits of a four-digit number, and I have collected from time to time the execution time with a simple

    1
    2
    3
    4
    5
    6
    7
    8
    [ ] ; timings var = [];
    0 ; var time = 0;

    ... ) { for (...) {
    new Date ) . getTime ( ) ; time = (new Date). getTime ();
    robaDaEseguire ();
    = ( ( new Date ) . getTime ( ) - time ) ; timings [i] = ((new Date). getTime () - time);
    }

    In the end I calculated the arithmetic mean of the individual times.

    The first algorithm employs 0.06896 seconds it takes the second and the third goes 0.06896 up to 0.01331.

    Increasing the number of launches to one million, the difference between the three begins to be palpable:
    The first algorithm employs 0.047184 seconds, the second and the third just 0.028644 0.018635.

    My research has become useless for now. See you soon, young men.

    PS: this is obviously taken with a smile ... ;-)

  6. November 6, 2008 Napolux :

    @ Unwiredbrain:
    Out of curiosity, on what browser you have carried out the tests? Have you tried other browsers to see if there are differences between browsers and browser?

  7. November 6, 2008 Giovambattista Fazioli :

    @ Napolux: bravo! You have taken the words of mouth : D I would ... I was intrigued to try Google Chrome : D

  8. November 6, 2008 Napolux :

    PHP is still the same, JS changes based on the browser, right? : P

  9. November 7, 2008 Napolux :

    @ Napolux:

    PHP is still the same, JS changes based on the browser, right?

    I explained evil ... I mean "change the JS engine based on the browser"

  10. November 7, 2008 unwiredbrain:

    @ Napolux , Giovambattista Fazioli : Firefox 3.0.3 on Linux.

    Opera of course brings out the results to say the least exciting: the order 0.010882, 0.013681 and (hear hear) 0.007994 milliseconds! Practically, 77, 52 and 57% less!

    Ah, I see now that in the previous comment was talking about second: in fact it is milliseconds .

    I'd like to see how things are at home KHTML / WebKit and Firefox 3.1, but still do not have sufficient strength and stability to be able to make some serious testing. Please deny it if you know anything about it, I'd be happy ... :-)

    As for Chrome, when it's available for Linux to squeeze me pretty well ... ;-)

    Greetings.

    PS: Soon I will publish the sources of test suites, now I really have no time ...

  11. November 7, 2008 unwiredbrain:

    But most of all: why my gravatar is gone to merry maidens who are self-commercialization? Mysteries of getAvatar ...

  12. November 7, 2008 Giovambattista Fazioli :

    @ Napolux:

    I explained evil ... I mean "change the JS engine based on the browser"

    Correct! However, to be precise, but PHP is not quite the same, there are different versions on different servers with different extensions. But this is to be considered for cases far more complicated than ours. : D

  13. November 10, 2008 Napolux :

    @ Giovambattista Fazioli:

    @ Napolux: I explained evil ... I mean "change the JS engine based on the browser" Correct! However, to be precise, but PHP is not quite the same, there are different versions on different servers with different extensions. But this is to be considered for cases far more complicated than ours.

    Tax ... : P

  14. November 10, 2008 Giovambattista Fazioli :

    @ Napolux: Only with you I can not afford to be "precise" ... at least you understand what I meant : D and appreciate the taxation : D

  15. November 10, 2008 Napolux :

    Now I begin to trolleggiare here, a little 'to me like they did for KB / kb / kb and company.

    : Lol:

  16. November 10, 2008 unwiredbrain:

    @ Napolux : Kib and KiB solve everything.

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL