Determine if a string is squeezable: Difference between revisions

Added Wren
(Add Ada)
(Added Wren)
Line 1,573:
----------------------
 
</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 squeezed string, original and new lengths in
// unicode code points (not normalized).
var squeeze = Fn.new { |s, ch|
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] == ch.codePoints[0] && 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",
"😍😀🙌💃😍😍😍🙌"
]
 
var chars = [ [" "], ["-"], ["7"], ["."], [" ", "-", "r"], ["e"], ["s"], ["a"], ["😍"] ]
 
var i = 0
for (s in strings) {
for (ch in chars[i]) {
var r = squeeze.call(s, ch)
System.print("Specified character = '%(ch)'")
System.print("original : length = %(rset.call(2, r[1])), string = «««%(s)»»»")
System.print("squeezed : length = %(rset.call(2, r[2])), string = «««%(r[0])»»»\n")
}
i = i + 1
}</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 = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
squeezed : length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
 
9,490

edits