Determine if a string is squeezable: Difference between revisions
Content added Content deleted
(julia example) |
(Added Go) |
||
Line 103: | Line 103: | ||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import "fmt" |
|||
// Returns squeezed string, original and new lengths in |
|||
// unicode code points (not normalized). |
|||
func squeeze(s string, c rune) (string, int, int) { |
|||
r := []rune(s) |
|||
le, del := len(r), 0 |
|||
for i := le - 2; i >= 0; i-- { |
|||
if r[i] == c && r[i] == r[i+1] { |
|||
copy(r[i:], r[i+1:]) |
|||
del++ |
|||
} |
|||
} |
|||
if del == 0 { |
|||
return s, le, le |
|||
} |
|||
r = r[:le-del] |
|||
return string(r), le, len(r) |
|||
} |
|||
func main() { |
|||
strings := []string{ |
|||
"", |
|||
`"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 := [][]rune{{' '}, {'-'}, {'7'}, {'.'}, {' ', '-', 'r'}, {'e'}, {'s'}, {'a'}, {'😍'}} |
|||
for i, s := range strings { |
|||
for _, c := range chars[i] { |
|||
ss, olen, slen := squeeze(s, c) |
|||
fmt.Printf("specified character = %q\n", c) |
|||
fmt.Printf("original : length = %2d, string = «««%s»»»\n", olen, s) |
|||
fmt.Printf("squeezed : length = %2d, string = «««%s»»»\n\n", slen, ss) |
|||
} |
|||
} |
|||
}</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> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 160: | Line 254: | ||
««« --- Hary S Truman »»» (length 71). |
««« --- Hary S Truman »»» (length 71). |
||
</pre> |
</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |