Determine if a string is collapsible: Difference between revisions

no edit summary
No edit summary
Line 1,914:
37 ?- S = "123456789", collapse(S,S).
S = "123456789" .
</pre>
 
=={{header|PureBasic}}==
<lang PureBasic>EnableExplicit
DataSection
STR1:
Data.s ""
STR2:
Data.s "'If I were two-faced, would I be wearing this one?' --- Abraham Lincoln "
STR3:
Data.s "..1111111111111111111111111111111111111111111111111111111111111117777888"
STR4:
Data.s "I never give 'em hell, I just tell the truth, and they think it's hell. "
STR5:
Data.s " --- Harry S Truman "
EndDataSection
 
Procedure.s collapse(txt$)
Define *c.Character=@txt$, last.c=0, result$
While *c\c
If *c\c<>last
result$+Chr(*c\c)
last=*c\c
EndIf
*c+SizeOf(Character)
Wend
ProcedureReturn result$
EndProcedure
 
OpenConsole("")
Define *p_data=?STR1, buf$
While *p_data<=?STR4+StringByteLength(PeekS(?STR4))+2
buf$=PeekS(*p_data)
PrintN("Before collapse: «««"+buf$+"»»» (length: "+Str(Len(buf$))+")")
buf$=collapse(PeekS(*p_data))
PrintN("After collapse: «««"+buf$+~"»»» (length: "+Str(Len(buf$))+~")\n")
*p_data+StringByteLength(PeekS(*p_data))+2
Wend
Input()</lang>
{{out}}
<pre>Before collapse: «««»»» (length: 0)
After collapse: «««»»» (length: 0)
 
Before collapse: «««'If I were two-faced, would I be wearing this one?' --- Abraham Lincoln »»» (length: 72)
After collapse: «««'If I were two-faced, would I be wearing this one?' - Abraham Lincoln »»» (length: 70)
 
Before collapse: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length: 72)
After collapse: «««.178»»» (length: 4)
 
Before collapse: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length: 72)
After collapse: «««I never give 'em hel, I just tel the truth, and they think it's hel. »»» (length: 69)
 
Before collapse: ««« --- Harry S Truman »»» (length: 72)
After collapse: ««« - Hary S Truman »»» (length: 17)
</pre>
 
164

edits