Determine if a string is squeezable: Difference between revisions

Content added Content deleted
(Add source for Rust)
Line 213:
=={{header|AutoHotkey}}==
<lang AutoHotkey>squeezable_string(str, char){
for i, ch in StrSplit(str){
if (ch <> prev) || !InStr(ch, char)
res .= ch
prev := ch
}
}
result := "
(ltrim
Original string:`t" StrLen(str) " characters`t«««" str "»»»
Squeezable Character «««" char "»»»
Resultant string:`t" StrLen(res) " characters`t«««" res "»»»
)"
return result
}</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. "]
char := ["","-","7","."]
for i, str in data
MsgBox % squeezable_string(str, char[i])
 
str := " --- Harry S Truman "
for i, char in [" ","-","r"]
MsgBox % squeezable_string(str, char)
return</lang>
Outputs:<pre>---------------------------
Original string: 0 characters «««»»»
Squeezable Character «««»»»
Resultant string: 0 characters «««»»»
---------------------------
Original string: 72 characters «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
Squeezable Character «««-»»»
Resultant string: 70 characters «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
---------------------------
Original string: 72 characters «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
Squeezable Character «««7»»»
Resultant string: 69 characters «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
---------------------------
Original string: 72 characters «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
Squeezable Character «««.»»»
Resultant string: 72 characters «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
---------------------------
Original string: 72 characters ««« --- Harry S Truman »»»
Squeezable Character ««« »»»
Resultant string: 20 characters ««« --- Harry S Truman »»»
---------------------------
Original string: 72 characters ««« --- Harry S Truman »»»
Squeezable Character «««-»»»
Resultant string: 70 characters ««« - Harry S Truman »»»
---------------------------
Original string: 72 characters ««« --- Harry S Truman »»»
Squeezable Character «««r»»»
Resultant string: 71 characters ««« --- Hary S Truman »»»
---------------------------</pre>
 
Line 1,429:
"I never give 'em hell, I just tell the truth, and they think it's hell. ",".",
" --- Harry S Truman "," -r"},
fmt = """
length %2d input: <<<%s>>>
length %2d squeeze(%c): <<<%s>>>
Line 1,555:
squeeze_( Chars, SqueezeChar, Result ),
string_chars( Collapsed, Result ).</lang>
{{out}}
<pre>
?- squeeze( "", ' ', New ).
Line 1,796:
"😍"-squeezed: «««😍😀🙌💃😍🙌»»» (size 6)
</pre>
 
=={{header|Rust}}==
See collapsible strings for the alternative implementation approach.
 
<lang Rust>fn squeezable_string<'a>(s: &'a str, squeezable: char) -> impl Iterator<Item = char> + 'a {
let mut previous = None;
 
s.chars().filter(move |c| match previous {
Some(p) if p == squeezable && p == *c => false,
_ => {
previous = Some(*c);
true
}
})
}
 
fn main() {
fn show(input: &str, c: char) {
println!("Squeeze: '{}'", c);
println!("Input ({} chars): \t{}", input.chars().count(), input);
let output: String = squeezable_string(input, c).collect();
println!("Output ({} chars): \t{}", output.chars().count(), output);
println!();
}
 
let harry = r#"I never give 'em hell, I just tell the truth, and they think it's hell.
--- Harry S Truman"#;
 
#[rustfmt::skip]
let inputs = [
("", ' '),
(r#""If I were two-faced, would I be wearing this one?" --- Abraham Lincoln "#, '-'),
("..1111111111111111111111111111111111111111111111111111111111111117777888", '7'),
(harry, ' '),
(harry, '-'),
(harry, 'r'),
("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", 'e'),
("headmistressship", 's'),
];
 
inputs.iter().for_each(|(input, c)| show(input, *c));
}</lang>
 
=={{header|Sidef}}==
Line 2,052 ⟶ 2,094:
 
=={{header|zkl}}==
<lang zkl>fcn squeeze(c,str){ // Works with UTF-8
s,cc,sz,n := Data(Void,str), String(c,c), c.len(), 0; // byte buffer in case of LOTs of deletes
while(Void != (n=s.find(cc,n))){ str=s.del(n,sz) } // and searching is faster for big strings