Determine if a string is squeezable: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: UTF-8 ize) |
(→{{header|Perl 6}}: Add Python.) |
||
Line 301: | Line 301: | ||
Squeezed length: 71 <<< --- Hary S Truman >>> |
Squeezed length: 71 <<< --- Hary S Truman >>> |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>from itertools import groupby |
|||
def squeezer(s, txt): |
|||
return ''.join(item if item == s else ''.join(grp) |
|||
for item, grp in groupby(txt)) |
|||
if __name__ == '__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", |
|||
"😍😀🙌💃😍😍😍🙌", |
|||
] |
|||
squeezers = ' ,-,7,., -r,e,s,a,😍'.split(',') |
|||
for txt, chars in zip(strings, squeezers): |
|||
this = "Original" |
|||
print(f"\n{this:14} Size: {len(txt)} «««{txt}»»»" ) |
|||
for ch in chars: |
|||
this = f"Squeezer '{ch}'" |
|||
sqz = squeezer(ch, txt) |
|||
print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )</lang> |
|||
{{out}} |
|||
<pre> |
|||
Original Size: 0 «««»»» |
|||
Squeezer ' ' Size: 0 «««»»» |
|||
Original Size: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» |
|||
Squeezer '-' Size: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» |
|||
Original Size: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» |
|||
Squeezer '7' Size: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»» |
|||
Original Size: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» |
|||
Squeezer '.' Size: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» |
|||
Original Size: 72 ««« --- Harry S Truman »»» |
|||
Squeezer ' ' Size: 20 ««« --- Harry S Truman »»» |
|||
Squeezer '-' Size: 70 ««« - Harry S Truman »»» |
|||
Squeezer 'r' Size: 71 ««« --- Hary S Truman »»» |
|||
Original Size: 80 «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»» |
|||
Squeezer 'e' Size: 79 «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»» |
|||
Original Size: 16 «««headmistressship»»» |
|||
Squeezer 's' Size: 14 «««headmistreship»»» |
|||
Original Size: 8 «««aardvark»»» |
|||
Squeezer 'a' Size: 7 «««ardvark»»» |
|||
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»» |
|||
Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |