[ create a new paste ] login | about

Link: http://codepad.org/alRDCWlU    [ raw code | output | fork ]

PHP, pasted on Aug 30:
<?php
function getPath($id)
{
    if ($id < 100) return "0".DIRECTORY_SEPARATOR;
    $id = str_pad($id,strlen($id)+(3-strlen($id)%3),"0",STR_PAD_LEFT);
    $in = array_map(create_function('$x','return ($x >= 1) ? ltrim($x,\'0\') : "0";'),str_split($id,3));
    array_pop($in);
    $in = array_reverse($in);
    return rtrim(implode(DIRECTORY_SEPARATOR,$in),DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}

printf("getPath(%d) = %s\n",3,getPath(3));
printf("getPath(%d) = %s\n",300,getPath(300));
printf("getPath(%d) = %s\n",3000,getPath(3000));
printf("getPath(%d) = %s\n",3000000,getPath(3000000));
printf("getPath(%s) = %s\n",3000000000,getPath(3000000000));

for ($i=0;$i < 10;$i++)
{
   $id = rand();
   printf("getPath(%d) = %s\n",$id,getPath($id));
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
getPath(3) = 0/
getPath(300) = 0/
getPath(3000) = 3/
getPath(3000000) = 0/3/
getPath(3000000000) = 0/0/3/
getPath(915046990) = 46/915/0/
getPath(1449303710) = 303/449/1/
getPath(2024616501) = 616/24/2/
getPath(1347748048) = 748/347/1/
getPath(1730388063) = 388/730/1/
getPath(1506101131) = 101/506/1/
getPath(1271390239) = 390/271/1/
getPath(1860402009) = 402/860/1/
getPath(787580679) = 580/787/0/
getPath(955724637) = 724/955/0/


Create a new paste based on this one


Comments: