Determine if a string is squeezable: Difference between revisions

no edit summary
No edit summary
Line 3,542:
old: 72 ««« --- Harry S Truman »»»
new: 71 ««« --- Hary S Truman »»»</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
<lang vlang>// Returns squeezed string, original and new lengths in
// unicode code points (not normalized).
fn squeeze(s string, c string) (string, int, int) {
mut r := s
le, mut del := r.len, 0
for i := le - 2; i >= 0; i-- {
if r[i..i+1] == c && r[i..i+1] == r[i+1..i+2] {
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",
"😍😀🙌💃😍😍😍🙌",
]
chars := [[' '], ['-'], ['7'], ['.'], [' ', '-', 'r'], ['e'], ['s'], ['a'], ['😍']]
for i, s in strings {
for c in chars[i] {
ss, olen, slen := squeeze(s, c)
println("specified character = $c")
println("original : length = ${olen:2}, string = «««$s»»»")
println("squeezed : length = ${slen:2}, string = «««$ss»»»\n")
}
}
}</lang>
 
{{out}}
<pre>
specified character =
original : length = 0, string = «««»»»
squeezed : length = 0, string = «««»»»
 
specified character = -
original : length = 72, string = «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
squeezed : length = 70, string = «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
 
specified character = 7
original : length = 72, string = «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
squeezed : length = 69, string = «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
 
specified character = .
original : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
squeezed : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
 
specified character =
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 20, string = ««« --- Harry S Truman »»»
 
specified character = -
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 70, string = ««« - Harry S Truman »»»
 
specified character = r
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 71, string = ««« --- Hary S Truman »»»
 
specified character = e
original : length = 80, string = «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
squeezed : length = 79, string = «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»
 
specified character = s
original : length = 16, string = «««headmistressship»»»
squeezed : length = 14, string = «««headmistreship»»»
 
specified character = a
original : length = 8, string = «««aardvark»»»
squeezed : length = 7, string = «««ardvark»»»
 
specified character = 😍
original : length = 32, string = «««😍😀🙌💃😍😍😍🙌»»»
squeezed : length = 32, string = «««😍😀🙌💃😍😍😍🙌»»»
</pre>
 
=={{header|Wren}}==
338

edits