Determine if a string is squeezable: Difference between revisions

(→‎{{header|J}}: boldface run section to accord with style of other sub-heads)
Line 2,659:
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»»
Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»</pre>
 
=={{header|Racket}}==
 
<lang racket>#lang racket/base
 
(define (squeeze-string s c)
(let loop ((cs (string->list s)) (squeezing? #f) (l 0) (acc null))
(cond [(null? cs) (values l (list->string (reverse acc)))]
[(and squeezing? (char=? (car cs) c)) (loop (cdr cs) #t l acc)]
[else (loop (cdr cs) (char=? (car cs) c) (add1 l) (cons (car cs) acc))])))
 
(define (report-squeeze s c)
(define-values (l′ s′) (squeeze-string s c))
(printf "Squeezing ~s in «««~a»»» (length ~a)~%" c s (string-length s))
(printf "Result: «««~a»»» (length ~a)~%~%" s′ l′))
 
(define (Determine-if-a-string-is-squeezeable)
(report-squeeze "" #\space)
(report-squeeze "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " #\-)
(report-squeeze "..1111111111111111111111111111111111111111111111111111111111111117777888" #\7)
(report-squeeze "I never give 'em hell, I just tell the truth, and they think it's hell. " #\.)
(define truman-sig " --- Harry S Truman ")
(report-squeeze truman-sig #\space)
(report-squeeze truman-sig #\-)
(report-squeeze truman-sig #\r))
 
(module+ main
(Determine-if-a-string-is-squeezeable))</lang>
 
{{out}}
 
<pre>Squeezing #\space in «««»»» (length 0)
Result: «««»»» (length 0)
 
Squeezing #\- in «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72)
Result: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70)
 
Squeezing #\7 in «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72)
Result: «««..1111111111111111111111111111111111111111111111111111111111111117888»»» (length 69)
 
Squeezing #\. in «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
Result: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
 
Squeezing #\space in ««« --- Harry S Truman »»» (length 72)
Result: ««« --- Harry S Truman »»» (length 20)
 
Squeezing #\- in ««« --- Harry S Truman »»» (length 72)
Result: ««« - Harry S Truman »»» (length 70)
 
Squeezing #\r in ««« --- Harry S Truman »»» (length 72)
Result: ««« --- Hary S Truman »»» (length 71)</pre>
 
=={{header|Raku}}==
569

edits