Rep-string: Difference between revisions

no edit summary
m (added whitespace before the TOC (table of contents), added other whitespace to the task's preamble.)
No edit summary
Line 364:
0
1 is no rep string!</pre>
 
=={{header|Common Lisp}}==
<lang lisp>
(ql:quickload :alexandria)
(defun rep-stringv (a-str &optional (max-rotation (floor (/ (length a-str) 2))))
;; Exit condition if no repetition found.
(cond ((< max-rotation 1) "Not a repeating string")
;; Two checks:
;; 1. Truncated string must be equal to rotation by repetion size.
;; 2. Remaining chars (rest-str) are identical to starting chars (beg-str)
((let* ((trunc (* max-rotation (truncate (length a-str) max-rotation)))
(truncated-str (subseq a-str 0 trunc))
(rest-str (subseq a-str trunc))
(beg-str (subseq a-str 0 (rem (length a-str) max-rotation))))
(and (string= beg-str rest-str)
(string= (alexandria:rotate (copy-seq truncated-str) max-rotation)
truncated-str)))
;; If both checks pass, return the repeting string.
(subseq a-str 0 max-rotation))
;; Recurse function reducing length of rotation.
(t (rep-stringv a-str (1- max-rotation)))))
 
(setf test-strings '("1001110011"
"1110111011"
"0010010010"
"1010101010"
"1111111111"
"0100101101"
"0100100"
"101"
"11"
"00"
"1"
))
 
(loop for item in test-strings
collecting (cons item (rep-stringv item)))
</lang>
{{out}}
<pre>
(("1001110011" . "10011") ("1110111011" . "1110") ("0010010010" . "001")
("1010101010" . "1010") ("1111111111" . "11111")
("0100101101" . "Not a repeating string") ("0100100" . "010")
("101" . "Not a repeating string") ("11" . "1") ("00" . "0")
("1" . "Not a repeating string"))
</pre>
 
=={{header|D}}==
Anonymous user