Determine if a string is squeezable: Difference between revisions
Content added Content deleted
(Added Algol 68) |
|||
Line 153: | Line 153: | ||
Input = <<< --- Harry S Truman >>>, length = 72 |
Input = <<< --- Harry S Truman >>>, length = 72 |
||
Output = <<< --- Hary S Truman >>>, length = 71</pre> |
Output = <<< --- Hary S Truman >>>, length = 71</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
<lang algol68>BEGIN |
|||
# returns a squeezed version of s # |
|||
# i.e. s with adjacent duplicate c characters removed # |
|||
PRIO SQUEEZE = 9; |
|||
OP SQUEEZE = ( STRING s, CHAR c )STRING: |
|||
IF s = "" |
|||
THEN "" # empty string # |
|||
ELSE # non-empty string # |
|||
[ LWB s : UPB s ]CHAR result; |
|||
INT r pos := LWB result; |
|||
result[ r pos ] := s[ LWB s ]; |
|||
FOR s pos FROM LWB s + 1 TO UPB s DO |
|||
IF result[ r pos ] /= s[ s pos ] |
|||
OR result[ r pos ] /= c |
|||
THEN |
|||
r pos +:= 1; |
|||
result[ r pos ] := s[ s pos ] |
|||
FI |
|||
OD; |
|||
result[ LWB result : r pos ] |
|||
FI # SQUEEZE # ; |
|||
# test the SQUEEZE operator # |
|||
PROC test squeeze = ( STRING s, CHAR c )VOID: |
|||
BEGIN |
|||
STRING z = s SQUEEZE c; |
|||
print( ( "Squeeing """, c, """ in ", "<<<", s, ">>> (length ", whole( ( UPB s + 1 ) - LWB s, 0 ), ")", newline ) ); |
|||
print( ( " -> ", "<<<", z, ">>> (length ", whole( ( UPB z + 1 ) - LWB z, 0 ), ")", newline ) ) |
|||
END # test squeee # ; |
|||
# task test cases # |
|||
test squeeze( "", " " ); |
|||
test squeeze( """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln ", "-" ); |
|||
test squeeze( "..1111111111111111111111111111111111111111111111111111111111111117777888", "7" ); |
|||
test squeeze( "I never give 'em hell, I just tell the truth, and they think it's hell. ", "." ); |
|||
STRING hst = " --- Harry S Truman "; |
|||
test squeeze( hst, " " ); |
|||
test squeeze( hst, "-" ); |
|||
test squeeze( hst, "r" ) |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
Squeeing " " in <<<>>> (length 0) |
|||
-> <<<>>> (length 0) |
|||
Squeeing "-" in <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (length 72) |
|||
-> <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (length 70) |
|||
Squeeing "7" in <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (length 72) |
|||
-> <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (length 69) |
|||
Squeeing "." in <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72) |
|||
-> <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72) |
|||
Squeeing " " in <<< --- Harry S Truman >>> (length 72) |
|||
-> <<< --- Harry S Truman >>> (length 20) |
|||
Squeeing "-" in <<< --- Harry S Truman >>> (length 72) |
|||
-> <<< - Harry S Truman >>> (length 70) |
|||
Squeeing "r" in <<< --- Harry S Truman >>> (length 72) |
|||
-> <<< --- Hary S Truman >>> (length 71) |
|||
</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |