Determine if a string is squeezable: Difference between revisions

Content added Content deleted
No edit summary
Line 714: Line 714:
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
squeezed : length = 6, string = «««😍😀🙌💃😍🙌»»»
squeezed : length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
=={{header|Haskell}}==
<lang haskell>
import Text.Printf (printf)

input :: [(String, Char)]
input = [ ("", ' ')
, ("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", 'e')
, ("headmistressship", 's')
, ("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", '-')
, ("..1111111111111111111111111111111111111111111111111111111111111117777888", '7')
, ("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.')
, (" --- Harry S Truman ", 'r')
, ("aardvark", 'a')
, ("😍😀🙌💃😍😍😍🙌", '😍')
]

collapse :: Eq a => [a] -> a -> [a]
collapse s c = go s
where go [] = []
go (x:y:xs)
| x == y && x == c = go (y:xs)
| otherwise = x : go (y:xs)
go xs = xs

main :: IO ()
main =
mapM_ (\(a, b, c) -> printf "squeeze: '%c'\nold: %3d «««%s»»»\nnew: %3d «««%s»»»\n\n" c (length a) a (length b) b)
$ (\(s, c) -> (s, collapse s c, c)) <$> input
</lang>
{{out}}
<pre>
squeeze: ' '
old: 0 «««»»»
new: 0 «««»»»

squeeze: 'e'
old: 80 «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
new: 79 «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»

squeeze: 's'
old: 16 «««headmistressship»»»
new: 14 «««headmistreship»»»

squeeze: '-'
old: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
new: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»

squeeze: '7'
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»

squeeze: '.'
old: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
new: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»

squeeze: 'r'
old: 72 ««« --- Harry S Truman »»»
new: 71 ««« --- Hary S Truman »»»

squeeze: 'a'
old: 8 «««aardvark»»»
new: 7 «««ardvark»»»

squeeze: '😍'
old: 8 «««😍😀🙌💃😍😍😍🙌»»»
new: 6 «««😍😀🙌💃😍🙌»»»
</pre>
</pre>