Encode/compress sequence of repeating integers

后端 未结 6 1411
后悔当初
后悔当初 2020-12-11 04:02

I have very long integer sequences that look like this (arbitrary length!):

0000000001110002220033333

Now I need some algorithm to convert

相关标签:
6条回答
  • 2020-12-11 04:10

    Here is a naive implementation of what you want.

    $toEncode = '0000000001110002220033333';
    $currentChar = '-1';
    $length = strlen($toEncode);
    $encoded = '';
    $currentNbrChar = 0;
    for($i = 0; $i < $length; $i++){
      if($toEncode[$i] != $currentChar){
        if($currentChar != '-1'){
          $encoded .= chr(97 + $currentChar).$currentNbrChar;
        }
        $currentNbrChar = 0;
        $currentChar = $toEncode[$i];
      }
      $currentNbrChar ++;
    }
    if($currentChar != '-1'){
      $encoded .= chr(97 + $currentChar).$currentNbrChar;
    }
    echo $encoded;
    
    0 讨论(0)
  • 2020-12-11 04:10

    Here's a shorter version:

    function smush(str) {
      return str.replace(/((.)\2*)/g, function(_, w, x) {
        return x + w.length;
      });
    }
    

    edit oh I see you want to encode with php; sorry I don't know that. Here's a decoder in a similar spirit:

    function unsmush(str) {
      return str.replace(/(.)(\d+)/g, function(_, x, n) {
        return new Array(parseInt(n, 10) + 1).join(x);
      });
    }
    
    0 讨论(0)
  • 2020-12-11 04:12

    Just FYI, you could probably gzip your data and the browse will automatically unzip it. For most implementations this is going to work better than RLE. But less fun obviously.

    0 讨论(0)
  • 2020-12-11 04:17

    It is called Run Length Encoding

    Basic encoder in PHP:

    function numStringToRle($s){
        $rle = '';
        $count = 1;
        $len = strlen($s);
        for ( $i = 0; $i < $len; $i++ ){
            if ( $i != $len && $s[$i] == $s[$i+1] ){
                $count++;                
            }else{
              $rle .= chr($s[$i] + 97).$count;    
              $count = 1;
            }
        }
        return $rle;
    }
    

    Be warned it will preform badly issues with a string like

     123456789123456789
    

    If you were going to be handling a string that may have a lot of individual single characters you would be better to add some complexity and not write the length of the run if the length of the run is 1.

    //change
    $rle .= chr($s[$i] + 97).$count;    
    
    //to
    $rle .= chr($s[$i] + 97).( $count == 1 ? '' : $count );   
    
    //or
    $rle .= chr($s[$i] + 97)
    if ( $count != 1 ){
        $rle .= $count;
    }
    
    0 讨论(0)
  • 2020-12-11 04:33
    $str="0000000001110002220033333";
    
    //$c will count the number of occurances.
    
    $c=1;
    
    $lastInt=substr($str,0,1);
    
    $str=substr($str,1);
    
    $resultStr='';
    
    $loopEnd=strlen($str);
    
    
    for($i=1; $i<=$loopEnd+1;$i++)
    
    {
    
        $nowInt=substr($str,0,1);   
        if($lastInt==$nowInt)
        {
            $c++;
            $str=substr($str,1);
        }
        else
        {
            $char=chr((int)$lastInt + 97);
            $resultStr=$resultStr.$char.$c;
            $str=substr($str,1);
            $c=1;
            $lastInt=$nowInt;
        }
    }
    
    // we use if condition since for loop will not take the last integer if it repeats.
    
    if($c>1)
    {
    
    $char=chr((int)$lastInt + 97);
    
    $resultStr=$resultStr.$char.$c;
    
    }
    
    echo $resultStr;
    
    0 讨论(0)
  • 2020-12-11 04:37
    function compress( $str) {
    $strArr = str_split($str.'0');
    $count = 0;
    $resStr = '';
    $strCheck = $strArr[0];
    foreach($strArr as $key => $value)
    {
        if($strCheck == $value)
        {
           $count++;
        } 
        else
        {
            if($count == 1)
            {
                $strCheck = $value;
                $resStr .= $strArr[$key-1];
                $count=1;
            }
            elseif($count == 2)
            {
                $strCheck = $value;
                $resStr .= $strArr[$key-1].$strArr[$key-1];
                $count=1;
            }
            else
            {
                $strCheck = $value;
                $resStr .= $strArr[$key-1].$count;
                $count=1;
            }
        } 
    
    } 
    return $resStr;
    

    }

    0 讨论(0)
提交回复
热议问题