Determine if a string is squeezable: Difference between revisions
Content added Content deleted
m (added related tasks.) |
|||
Line 1,366: | Line 1,366: | ||
old: <<< --- Harry S Truman >>>, length = 72 |
old: <<< --- Harry S Truman >>>, length = 72 |
||
new: <<< --- Hary S Truman >>>, length = 71</pre> |
new: <<< --- Hary S Truman >>>, length = 71</pre> |
||
<lang Matlab> |
|||
function r = squeezee(s,c) |
|||
ix = []; |
|||
c = unique(c); |
|||
for k=1:length(c) |
|||
ix=[ix; find((s(1:end-1)==s(2:end)) & (s(1:end-1)==c(k)))+1]; |
|||
end |
|||
r=s; |
|||
r(ix)=[]; |
|||
fprintf(1,'Character to be squeezed: "%s"\n',c); |
|||
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 |
|||
squeezee('', ' ') |
|||
squeezee('║╚═══════════════════════════════════════════════════════════════════════╗', '-') |
|||
squeezee('║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║', '7') |
|||
squeezee('║..1111111111111111111111111111111111111111111111111111111111111117777888║', '.') |
|||
squeezee('║I never give ''em hell, I just tell the truth, and they think it''s hell. ║', '.') |
|||
squeezee('║ --- Harry S Truman ║', '.') |
|||
squeezee('║ --- Harry S Truman ║', '-') |
|||
squeezee('║ --- Harry S Truman ║', 'r') |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
octave:1> test squeezee |
|||
Character to be squeezed: " " |
|||
Input: <<<>>> length: 0 |
|||
Output: <<<>>> length: 0 |
|||
Character to be squeezed: " " |
|||
ans = |
|||
Character to be squeezed: "-" |
|||
Input: <<<║╚═══════════════════════════════════════════════════════════════════════╗>>> length: 222 |
|||
Output: <<<║╚═══════════════════════════════════════════════════════════════════════╗>>> length: 222 |
|||
Character to be squeezed: "-" |
|||
ans = ║╚═══════════════════════════════════════════════════════════════════════╗ |
|||
Character to be squeezed: "7" |
|||
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: 78 |
|||
Character to be squeezed: "7" |
|||
ans = ║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║ |
|||
Character to be squeezed: "." |
|||
Input: <<<║..1111111111111111111111111111111111111111111111111111111111111117777888║>>> length: 78 |
|||
Output: <<<║.1111111111111111111111111111111111111111111111111111111111111117777888║>>> length: 77 |
|||
Character to be squeezed: "." |
|||
ans = ║.1111111111111111111111111111111111111111111111111111111111111117777888║ |
|||
Character to be squeezed: "." |
|||
Input: <<<║I never give 'em hell, I just tell the truth, and they think it's hell. ║>>> length: 78 |
|||
Output: <<<║I never give 'em hell, I just tell the truth, and they think it's hell. ║>>> length: 78 |
|||
Character to be squeezed: "." |
|||
ans = ║I never give 'em hell, I just tell the truth, and they think it's hell. ║ |
|||
Character to be squeezed: "." |
|||
Input: <<<║ --- Harry S Truman ║>>> length: 78 |
|||
Output: <<<║ --- Harry S Truman ║>>> length: 78 |
|||
Character to be squeezed: "." |
|||
ans = ║ --- Harry S Truman ║ |
|||
Character to be squeezed: "-" |
|||
Input: <<<║ --- Harry S Truman ║>>> length: 78 |
|||
Output: <<<║ - Harry S Truman ║>>> length: 76 |
|||
Character to be squeezed: "-" |
|||
ans = ║ - Harry S Truman ║ |
|||
Character to be squeezed: "r" |
|||
Input: <<<║ --- Harry S Truman ║>>> length: 78 |
|||
Output: <<<║ --- Hary S Truman ║>>> length: 77 |
|||
Character to be squeezed: "r" |
|||
ans = ║ --- Hary S Truman ║ |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |