Determine if a string is collapsible: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) (Added Arturo implementation) |
No edit summary |
||
Line 3,682: | Line 3,682: | ||
String : <<< --- Harry S Truman >>> Lenght : 72 |
String : <<< --- Harry S Truman >>> Lenght : 72 |
||
Collapsed : <<< - Hary S Truman >>> Lenght : 17</pre> |
Collapsed : <<< - Hary S Truman >>> Lenght : 17</pre> |
||
=={{header|Vlang}}== |
|||
{{trans|Go}} |
|||
<lang vlang>// Returns collapsed string, original and new lengths in |
|||
// unicode code points (not normalized). |
|||
fn collapse(s string) (string, int, int) { |
|||
mut r := s |
|||
le, mut del := r.len, 0 |
|||
for i := le - 2; i >= 0; i-- { |
|||
if r[i] == r[i+1] { |
|||
r = r[..i]+r[i+1..] |
|||
del++ |
|||
} |
|||
} |
|||
if del == 0 { |
|||
return s, le, le |
|||
} |
|||
r = r[..le-del] |
|||
return r, le, r.len |
|||
} |
|||
fn main() { |
|||
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 { |
|||
cs, olen, clen := collapse(s) |
|||
println("original : length = ${olen:2}, string = «««$s»»»") |
|||
println("collapsed: length = ${clen:2}, string = «««$cs»»»\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 = 32, string = «««😍😀🙌💃😍😍😍🙌»»» |
|||
collapsed: length = 32, string = «««😍😀🙌💃😍😍😍🙌»»» |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |