Anonymous user
Determine if a string is collapsible: Difference between revisions
Determine if a string is collapsible (view source)
Revision as of 01:52, 29 January 2020
, 4 years agoAdded Prolog
mNo edit summary |
(Added Prolog) |
||
Line 856:
Original : <<<😍😀🙌💃😍😍😍🙌>>> (len=8)
Collapse : <<<😍😀🙌💃😍🙌>>> (len=6)</pre>
=={{header|Prolog}}==
<lang prolog>collapse_( [], [] ).
collapse_( [A], [A] ).
collapse_( [A,A|T], R ) :- collapse_( [A|T], R ).
collapse_( [A,B|T], [A|R] ) :- dif( A, B ), collapse_( [B|T], R ).
collapse( Str, Collapsed ) :-
string_chars( Str, Chars ),
collapse_( Chars, Result ),
string_chars( Collapsed, Result ).</lang>
{{out}}
<pre>
?- collapse("122333444455555666666777777788888888999999999", New).
New = "123456789" .
?- collapse("", New).
New = "" .
?- collapse("\"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 " .
?- collapse("..1111111111111111111111111111111111111111111111111111111111111117777888", New).
New = ".178" .
?- collapse("I never give 'em hell, I just tell the truth, and they think it's hell. ", New).
New = "I never give 'em hel, I just tel the truth, and they think it's hel. " .
?- collapse(" --- Harry S Truman ", New).
New = " - Hary S Truman " .
?- collapse("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", New).
New = "The beter the 4-whel drive, the further you'l be from help when ya get stuck!" .
33 ?- collapse("headmistressship", New).
New = "headmistreship" .
</pre>
=={{header|Python}}==
|