Determine if a string is collapsible: Difference between revisions

Added Racket implementation
(JavaScript added)
(Added Racket implementation)
Line 2,514:
 
 
</pre>
 
=={{header|Racket}}==
 
<lang Racket>#lang racket
 
(define (collapse text)
(if (< (string-length text) 2)
text
(string-append
(if (equal? (substring text 0 1) (substring text 1 2))
"" (substring text 0 1))
(collapse (substring text 1)))))
 
; Test cases
(define tcs
'(""
"\"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"
"😍😀🙌💃😍😍😍🙌"))
 
(for ([text tcs])
(let ([collapsed (collapse text)])
(display (format "Original (size ~a): «««~a»»»\nCollapsed (size ~a): «««~a»»»\n\n"
(string-length text) text
(string-length collapsed) collapsed))))
</lang>
{{out}}
<pre>Original (size 0): «««»»»
Collapsed (size 0): «««»»»
 
Original (size 72): «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
Collapsed (size 70): «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
 
Original (size 72): «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
Collapsed (size 4): «««.178»»»
 
Original (size 72): «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
Collapsed (size 69): «««I never give 'em hel, I just tel the truth, and they think it's hel. »»»
 
Original (size 72): ««« --- Harry S Truman »»»
Collapsed (size 17): ««« - Hary S Truman »»»
 
Original (size 80): «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
Collapsed (size 77): «««The beter the 4-whel drive, the further you'l be from help when ya get stuck!»»»
 
Original (size 16): «««headmistressship»»»
Collapsed (size 14): «««headmistreship»»»
 
Original (size 8): «««aardvark»»»
Collapsed (size 7): «««ardvark»»»
 
Original (size 8): «««😍😀🙌💃😍😍😍🙌»»»
Collapsed (size 6): «««😍😀🙌💃😍🙌»»»
</pre>