Skip to content

Instantly share code, notes, and snippets.

Created February 7, 2011 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/815282 to your computer and use it in GitHub Desktop.
Save anonymous/815282 to your computer and use it in GitHub Desktop.
<?php
// while($d?$fc+1===$s:$fc-1===$s)
// waere schoener gewesen, aber langsamer ;)
function chuck_compress($a)
{
$r=array();
$l=count($a);
$s=$a[0];
$d=false;
for($i=1;$i!==$l;++$i)
{
$f=$s;
$s=$a[$i];
if(($d=$f+1===$s)||$f-1===$s)
{
if(++$i===$l)
{
$r[]=$f."-".$s;
return $r;
}
$fc=$s;
$s=$a[$i];
if($d)
while($fc+1===$s)
{
if(++$i===$l)
{
$r[]=$f."-".$s;
return $r;
}
$fc=$s;
$s=$a[$i];
}
else
while($fc-1===$s)
{
if(++$i===$l)
{
$r[]=$f."-".$s;
return $r;
}
$fc=$s;
$s=$a[$i];
}
$r[]=$f."-".$fc;
}
else
$r[]=$f;
}
$r[]=$s;
return $r;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment