Determine if a string is squeezable: Difference between revisions

m (→‎{{header|Ruby}}: Added Ruby)
Line 806:
length 71 squeeze(r): <<< --- Hary S Truman >>>
</pre>
 
=={{header|PHP}}==
 
<lang 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;
}
}</lang>
 
{{out}}
<pre>Original : <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (len=72)
Squeeze(-) : <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (len=70)
 
Original : <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len=72)
Squeeze(1) : <<<..17777888>>> (len=10)
 
Original : <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len=72)
Squeeze(l) : <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> (len=69)
 
Original : <<< --- Harry S Truman >>> (len=72)
Squeeze( ) : <<< --- Harry S Truman >>> (len=20)
 
Original : <<<0112223333444445555556666666777777778888888889999999999>>> (len=55)
Squeeze(9) : <<<0112223333444445555556666666777777778888888889>>> (len=46)
 
Original : <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> (len=80)
Squeeze(e) : <<<The better the 4-whel drive, the further you'll be from help when ya get stuck!>>> (len=79)
 
Original : <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> (len=80)
Squeeze(k) : string is not squeezable (char=k)...</pre>
 
=={{header|Python}}==
Anonymous user