Determine if a string is squeezable: Difference between revisions

Content added Content deleted
mNo edit summary
(Added Prolog)
Line 938: Line 938:
Original : <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> (len=80)
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>
Squeeze(k) : string is not squeezable (char=k)...</pre>

=={{header|Prolog}}==
<lang Prolog>squeeze_( [], _, [] ).
squeeze_( [A], _, [A] ).
squeeze_( [A,A|T], A, R ) :- squeeze_( [A|T], A, R ).
squeeze_( [A,A|T], B, [A|R] ) :- dif( A, B ), squeeze_( [A|T], B, R ).
squeeze_( [A,B|T], S, [A|R] ) :- dif( A, B ), squeeze_( [B|T], S, R ).

squeeze( Str, SqueezeChar, Collapsed ) :-
string_chars( Str, Chars ),
squeeze_( Chars, SqueezeChar, Result ),
string_chars( Collapsed, Result ).</lang>
{{out}}
<pre>
?- squeeze( "", ' ', New ).
New = "".

?- squeeze( "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", -, New ).
New = "\"If I were two-faced, would I be wearing this one?\" - Abraham Lincoln " .

?- squeeze( "..1111111111111111111111111111111111111111111111111111111111111117777888", '7', New ).
New = "..1111111111111111111111111111111111111111111111111111111111111117888" .

?- squeeze( "I never give 'em hell, I just tell the truth, and they think it's hell. ", '.', New ).
New = "I never give 'em hell, I just tell the truth, and they think it's hell. " .

?- squeeze( " --- Harry S Truman ", ' ', New ).
New = " --- Harry S Truman " .

?- squeeze( " --- Harry S Truman ", '-', New ).
New = " - Harry S Truman " .

?- squeeze( " --- Harry S Truman ", 'r', New ).
New = " --- Hary S Truman " .
</pre>
To determine if a string is squeezed or not then the input and output strings should be the same, eg:
<pre>
?- S = "..1111111111111111111111111111111111111111111111111111111111111117888", squeeze( S, '1', S ).
false.

?- S = "..1111111111111111111111111111111111111111111111111111111111111117888", squeeze( S, '7', S ).
S = "..1111111111111111111111111111111111111111111111111111111111111117888"
</pre>




=={{header|Python}}==
=={{header|Python}}==