Task: Determine if a string is squeezable






PHP: <?php

function squeezeString($string, $squeezeChar) {
    $previousChar = null;
    $squeeze = '';
    $charArray = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
    for ($i = 0 ; $i < count($charArray) ; $i++) {
        $currentChar = $charArray[$i];
        if ($previousChar !== $currentChar || $currentChar !== $squeezeChar) {
            $squeeze .= $charArray[$i];
        }
        $previousChar = $currentChar;
    }
    return $squeeze;
}

function isSqueezable($string, $squeezeChar) {
    return ($string !== squeezeString($string, $squeezeChar));
}

$strings = array(
    ['-', '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '],
    ['1', '..1111111111111111111111111111111111111111111111111111111111111117777888'],
    ['l', "I never give 'em hell, I just tell the truth, and they think it's hell. "],
    [' ', '                                                    --- Harry S Truman  '],
    ['9', '0112223333444445555556666666777777778888888889999999999'],
    ['e', "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"],
    ['k', "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"],
);
foreach ($strings as $params) {
    list($char, $original) = $params;
    echo 'Original   : <<<', $original, '>>> (len=', mb_strlen($original), ')', PHP_EOL;
    if (isSqueezable($original, $char)) {
        $squeeze = squeezeString($original, $char);
        echo 'Squeeze(', $char, ') : <<<', $squeeze, '>>> (len=', mb_strlen($squeeze), ')', PHP_EOL, PHP_EOL;
    } else {
        echo 'Squeeze(', $char, ') : string is not squeezable (char=', $char, ')...', PHP_EOL, PHP_EOL;
    }
}





Fortran: program main
implicit none
character(len=:),allocatable :: strings(:)

strings=[ character(len=72) :: &
'', &
'"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln',  &
'..1111111111111111111111111111111111111111111111111111111111111117777888', &
'I never give ''em hell, I just tell the truth, and they think it''s hell.',&
'                                                    --- Harry S Truman'    &
]

   call printme( trim(strings(1)), ' ' )
   call printme( strings(2:4),     ['-','7','.'] )
   call printme( strings(5),       [' ','-','r'] )

contains

impure elemental subroutine printme(str,chr)
character(len=*),intent(in) :: str
character(len=1),intent(in) :: chr
character(len=:),allocatable :: answer
   write(*,'(a)')repeat('=',9)
   write(*,'("IN:   <<<",g0,">>>")')str
   answer=compact(str,chr)
   write(*,'("OUT:  <<<",g0,">>>")')answer
   write(*,'("LENS: ",*(g0,1x))')"from",len(str),"to",len(answer),"for a change of",len(str)-len(answer)
   write(*,'("CHAR: ",g0)')chr
end subroutine printme

elemental function compact(str,charp) result (outstr)

character(len=*),intent(in)  :: str
character(len=1),intent(in)  :: charp
character(len=:),allocatable :: outstr
character(len=1)             :: ch, last_one
integer                      :: i, pio 

   outstr=repeat(' ',len(str))      
   if(len(outstr)==0)return         
   last_one=str(1:1)                
   outstr(1:1)=last_one
   pio=1

   do i=2,len(str)
      ch=str(i:i)
      pio=pio+merge(0,1, ch.eq.last_one.and.ch.eq.charp) 
      outstr(pio:pio)=ch  
      last_one=ch
   enddo

   outstr=outstr(:pio)              
end function compact

end program main
}