Entries Comments



Pequeno desafio

31 July, 2008 (14:34) | PHP, SEO, Scripts | By: João José

Foi pena só ler agora o desafio do Dustin Diaz mas de qualquer forma aqui está a minha solução despretensiosa obtida em cerca de 20 min. É engraçado como há tantas soluções para um problema.

Numa side note ora aí está um excelente link bait :-)

/*In English: Group together all duplicate items that occur anytime beyond twice by wrapping them with a tag, naturally “bookending” them.
Simple, right? No, really. Tease your brain for a few minutes, you can fix that bug after lunch.*/

$polvero = array('a','b','c','d','e','e','e','e','e','f','e','f','e','f','a','a','a','f','f','f');

foreach($polvero as $key=>$value){

if($twice==true and !array_key_exists(1,$twice)){
$string.=$starttag.$value;
}else{
$string.=$endtag.$value;
}

if($polvero[$key-1]==$value && $polvero[$key+1]==$value){
$twice[]=true;
$starttag=’<span>’;
}elseif($twice==true){
$endtag=’</span>’;
if(array_key_exists($key+1,$polvero)){
$twice=false;
}else{
$twice=false;
$end=true;
}
}else{
$endtag=false;
}

}

if($end==true){$string.=’</span>’;}

echo $string;

Comments

Comment from Carlos Martins
Time: July 31, 2008, 4:53 pm

Esse nem dá dores de cabeça…
Agora… há alguns do Google Code Jam.. que esses sim, são de atrofiar.

Cheguei à segunda eliminatória, e depois não tive oportunidade de fazer aquilo, e um dos problemas acho que conseguia resolver sem muitas complicações…

Comment from Rui Augusto
Time: September 2, 2008, 12:43 am

Para quando um novo artigo…que miséria.

Write a comment