Determine if a string is collapsible: Difference between revisions
Content deleted Content added
Add Ada |
Added Wren |
||
Line 1,643: | Line 1,643: | ||
Subbed (length 14): headmistreship |
Subbed (length 14): headmistreship |
||
---------------------- |
---------------------- |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
<lang ecmascript>var rset = Fn.new { |m, n| |
|||
var s = "%(n)" |
|||
var c = s.count |
|||
return (m > c) ? " " * (m - c) + s : s |
|||
} |
|||
// Returns collapsed string, original and new lengths in |
|||
// unicode code points (not normalized). |
|||
var collapse = Fn.new { |s| |
|||
var c = s.codePoints.toList |
|||
var le = c.count |
|||
if (le < 2) return [s, le, le] |
|||
for (i in le-2..0) { |
|||
if (c[i] == c[i+1]) c.removeAt(i) |
|||
} |
|||
var cc = c.reduce("") { |acc, cp| acc + String.fromCodePoint(cp) } |
|||
return [cc, le, cc.count] |
|||
} |
|||
var strings = [ |
|||
"", |
|||
"\"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 ", |
|||
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!", |
|||
"headmistressship", |
|||
"aardvark", |
|||
"😍😀🙌💃😍😍😍🙌" |
|||
] |
|||
for (s in strings) { |
|||
var r = collapse.call(s) |
|||
System.print("original : length = %(rset.call(2, r[1])), string = «««%(s)»»»") |
|||
System.print("collapsed: length = %(rset.call(2, r[2])), string = «««%(r[0])»»»\n") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
original : length = 0, string = «««»»» |
|||
collapsed: length = 0, string = «««»»» |
|||
original : length = 72, string = «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» |
|||
collapsed: length = 70, string = «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» |
|||
original : length = 72, string = «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» |
|||
collapsed: length = 4, string = «««.178»»» |
|||
original : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» |
|||
collapsed: length = 69, string = «««I never give 'em hel, I just tel the truth, and they think it's hel. »»» |
|||
original : length = 72, string = ««« --- Harry S Truman »»» |
|||
collapsed: length = 17, string = ««« - Hary S Truman »»» |
|||
original : length = 80, string = «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»» |
|||
collapsed: length = 77, string = «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»» |
|||
original : length = 16, string = «««headmistressship»»» |
|||
collapsed: length = 14, string = «««headmistreship»»» |
|||
original : length = 8, string = «««aardvark»»» |
|||
collapsed: length = 7, string = «««ardvark»»» |
|||
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»» |
|||
collapsed: length = 6, string = «««😍😀🙌💃😍🙌»»» |
|||
</pre> |
</pre> |
||