Archive for the 'Etc' Category

:) A job for ukrainian programmer

Test-task from http://www.dp76.com/php-test/

I wrote this script in three minutes… Maybe I can get a cool php-programmer job :)

  1. // http://www.dp76.com/php-test/
  2.  
  3. $s='1|0|Electronics
  4. 2|0|Video
  5. 3|0|Photo
  6. 4|1|MP3 player
  7. 5|1|TV
  8. 6|4|iPod
  9. 7|6|Shuffle
  10. 8|3|SLR
  11. 9|8|DSLR
  12. 10|9|Nikon
  13. 11|9|Canon
  14. 12|11|20D';
  15.  
  16. $s=explode("\n", $s);
  17. $items=array();
  18. foreach($s as $item){
  19.   $s1=array();
  20.   $s1=explode('|', trim($item));
  21.   $items[$s1[1]][$s1[0]]=$s1[2];
  22. }
  23.  
  24. echo '<pre>'.CatLst(0).'</pre>';
  25.  
  26.  
  27. function CatLst($parent_id, $indent=''){
  28. global $items;
  29.   if(isset($items[$parent_id])){
  30.     $out='';
  31.     while(list($item_key, $item_val)=each($items[$parent_id])){
  32.       $out.=$indent.$item_val.' (id: '.$item_key.', parent_id: '.$parent_id.")\r\n";
  33.       $out.=CatLst($item_key, $indent."\t");
  34.     }
  35.   }
  36.  
  37.   return $out;
  38. }

Tags: , ,