Determine if a string is collapsible: Difference between revisions

Content added Content deleted
(→‎{{header|Bracmat}}: Added Bracmat example)
Line 1,454: Line 1,454:
old: 72 <<< --- Harry S Truman >>>
old: 72 <<< --- Harry S Truman >>>
new: 17 <<< - Hary S Truman >>></pre>
new: 17 <<< - Hary S Truman >>></pre>

=={{header|MATLAB}} / {{header|Octave}}==
<lang Matlab>
function r = collapse(s)
ix=find((s(1:end-1)==s(2:end))+1;
r=s;
r(ix)=[];

fprintf(1,'Input: <<<%s>>> length: %d\n',s,length(s));
fprintf(1,'Output: <<<%s>>> length: %d\n',r,length(r));
fprintf(1,'Character to be squeezed: "%s"\n',c);

end


collapse('', ' ')
collapse('║╚═══════════════════════════════════════════════════════════════════════╗', '-')
collapse('║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║', '7')
collapse('║..1111111111111111111111111111111111111111111111111111111111111117777888║', '.')
collapse('║I never give ''em hell, I just tell the truth, and they think it''s hell. ║', '.')
collapse('║ --- Harry S Truman ║', '.')
collapse('║ --- Harry S Truman ║', '-')
collapse('║ --- Harry S Truman ║', 'r')

</lang>

{{out}}
<pre>
Input: <<<>>> length: 0
Output: <<<>>> length: 0
ans =
Input: <<<║╚═══════════════════════════════════════════════════════════════════════╗>>> length: 222
Output: <<<║╚═══════════════════════════════════════════════════════════════════════╗>>> length: 222
ans = ║╚═══════════════════════════════════════════════════════════════════════╗
Input: <<<║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║>>> length: 78
Output: <<<║"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ║>>> length: 76
ans = ║"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ║
Input: <<<║..1111111111111111111111111111111111111111111111111111111111111117777888║>>> length: 78
Output: <<<║.178║>>> length: 10
ans = ║.178║
Input: <<<║I never give 'em hell, I just tell the truth, and they think it's hell. ║>>> length: 78
Output: <<<║I never give 'em hel, I just tel the truth, and they think it's hel. ║>>> length: 75
ans = ║I never give 'em hel, I just tel the truth, and they think it's hel. ║
Input: <<<║ --- Harry S Truman ║>>> length: 78
Output: <<<║ - Hary S Truman ║>>> length: 23
ans = ║ - Hary S Truman ║
Input: <<<║ --- Harry S Truman ║>>> length: 78
Output: <<<║ - Hary S Truman ║>>> length: 23
ans = ║ - Hary S Truman ║
Input: <<<║ --- Harry S Truman ║>>> length: 78
Output: <<<║ - Hary S Truman ║>>> length: 23
ans = ║ - Hary S Truman ║
PASSES 1 out of 1 test
</pre>



=={{header|Perl}}==
=={{header|Perl}}==