Determine if a string is collapsible: Difference between revisions
Content deleted Content added
Alpha bravo (talk | contribs) Added AutoHotkey |
|||
Line 171: | Line 171: | ||
result <<< - Hary S Truman >>> (length 17) |
result <<< - Hary S Truman >>> (length 17) |
||
</pre> |
</pre> |
||
=={{header|AutoHotkey}}== |
|||
<lang AutoHotkey>collapsible_string(str){ |
|||
for i, ch in StrSplit(str){ |
|||
if (ch <> prev) |
|||
res .= ch |
|||
prev := ch |
|||
} |
|||
return "original string:`t" StrLen(str) " characters`t«««" str "»»»`nresultant string:`t" StrLen(res) " characters`t«««" res "»»»" |
|||
}</lang> |
|||
Examples:<lang AutoHotkey>data := ["" |
|||
, """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln " |
|||
, "..1111111111111111111111111111111111111111111111111111111111111117777888" |
|||
, "I never give 'em hell, I just tell the truth, and they think it's hell. " |
|||
, " --- Harry S Truman "] |
|||
for i, v in data |
|||
MsgBox % := collapsible_string(v) |
|||
return</lang> |
|||
Outputs:<pre>----------------------------------------- |
|||
original string: 0 characters «««»»» |
|||
resultant string: 0 characters «««»»» |
|||
----------------------------------------- |
|||
original string: 72 characters «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» |
|||
resultant string: 70 characters «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» |
|||
----------------------------------------- |
|||
original string: 72 characters «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» |
|||
resultant string: 4 characters «««.178»»» |
|||
----------------------------------------- |
|||
original string: 72 characters «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» |
|||
resultant string: 69 characters «««I never give 'em hel, I just tel the truth, and they think it's hel. »»» |
|||
----------------------------------------- |
|||
original string: 72 characters ««« --- Harry S Truman »»» |
|||
resultant string: 17 characters ««« - Hary S Truman »»» |
|||
-----------------------------------------</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |