2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,22 +1,28 @@
Given a series of ones and zeroes in a string, define a repeated string or ''rep-string'' as a string which is created by repeating a substring of the ''first'' N characters of the string ''truncated on the right to the length of the input string, and in which the substring appears repeated at least twice in the original''.
For example, the string <code>'10011001100'</code> is a rep-string as the leftmost four characters of <code>'1001'</code> are repeated three times and truncated on the right to give the original string.
For example, the string &nbsp; &nbsp; '''10011001100''' &nbsp; &nbsp; is a rep-string as the leftmost four characters of &nbsp; &nbsp; '''1001''' &nbsp; &nbsp; are repeated three times and truncated on the right to give the original string.
Note that the requirement for having the repeat occur two or more times means that the repeating unit is ''never'' longer than half the length of the input string.
Note that the requirement for having the repeat occur two or more times means that the repeating unit is &nbsp; ''never'' &nbsp; longer than half the length of the input string.
The task is to:
* Write a function/subroutine/method/... that takes a string and returns an indication of if it is a rep-string and the repeated string. (Either the string that is repeated, or the number of repeated characters would suffice).
;Task:
* Write a function/subroutine/method/... that takes a string and returns an indication of if it is a rep-string and the repeated string. &nbsp; (Either the string that is repeated, or the number of repeated characters would suffice).
* There may be multiple sub-strings that make a string a rep-string - in that case an indication of all, or the longest, or the shortest would suffice.
* Use the function to indicate the repeating substring if any, in the following:
<dl><dd><pre>'1001110011'
'1110111011'
'0010010010'
'1010101010'
'1111111111'
'0100101101'
'0100100'
'101'
'11'
'00'
'1'</pre></dl>
<dl><dd>
<pre>
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1
</pre>
</dl>
* Show your output on this page.
<br><br>

View file

@ -0,0 +1,18 @@
(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)))))

View file

@ -0,0 +1,15 @@
(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)))

View file

@ -0,0 +1,29 @@
defmodule Rep_string do
def find(""), do: IO.puts "String was empty (no repetition)"
def find(str) do
IO.puts str
rep_pos = Enum.find(div(String.length(str),2)..1, fn pos ->
String.starts_with?(str, String.slice(str, pos..-1))
end)
if rep_pos && rep_pos>0 do
IO.puts String.duplicate(" ", rep_pos) <> String.slice(str, 0, rep_pos)
else
IO.puts "(no repetition)"
end
IO.puts ""
end
end
strs = ~w(1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1)
Enum.each(strs, fn str -> Rep_string.find(str) end)

View file

@ -0,0 +1,11 @@
(de repString (Str)
(let Lst (chop Str)
(for (N (/ (length Lst) 2) (gt0 N) (dec N))
(T
(use (Lst X)
(let H (cut N 'Lst)
(loop
(setq X (cut N 'Lst))
(NIL (head X H))
(NIL Lst T) ) ) )
N ) ) ) )

View file

@ -0,0 +1,12 @@
(test 5 (repString "1001110011"))
(test 4 (repString "1110111011"))
(test 3 (repString "0010010010"))
(test 4 (repString "1010101010"))
(test 5 (repString "1111111111"))
(test NIL (repString "0100101101"))
(test 3 (repString "0100100"))
(test NIL (repString "101"))
(test 1 (repString "11"))
(test 1 (repString "00"))
(test NIL (repString "1"))
(test NIL (repString "0100101"))

View file

@ -1,17 +1,16 @@
/*REXX pgm determines if a string is a repString, returns min. length repStr. */
parse arg s /*get optional strings from the C.L. */
/*REXX pgm determines if a string is a repString, it returns minimum length repString.*/
parse arg s /*get optional strings from the C.L. */
if s='' then s=1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 45
/* [↑] S not specified? Use defaults*/
do k=1 for words(s); _=word(s,k); w=length(_) /*process binary strings.*/
say right(_,max(25,w)) repString(_) /*show repString & result*/
end /*k*/ /* [↑] the "result" may be negatory.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
repString: procedure; parse arg x; L=length(x)
if \datatype(x,'B') then return " ***error!*** string isn't a binary string."
do j=1 for L-1 while j<=L%2; $=left(x,j); $$=copies($,L)
if left($$,L)==x then return ' rep string=' left($,15) '[length' j"]"
end /*j*/ /* [↑] we have found a good repString.*/
return ' (no repetitions)' /*(sigh)··· a failure to find repString*/
/* [↑] S not specified? Use defaults*/
do k=1 for words(s); _=word(s,k); w=length(_) /*process binary strings. */
say right(_,max(25,w)) repString(_) /*show repString & result.*/
end /*k*/ /* [↑] the "result" may be negatory.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
repString: procedure; parse arg x; L=length(x); @rep=' rep string='
if \datatype(x,'B') then return " ***error*** string isn't a binary string."
h=L%2
do j=1 for L-1 while j<=h; $=left(x,j); $$=copies($,L)
if left($$,L)==x then return @rep left($,15) "[length" j']'
end /*j*/ /* [↑] we have found a good repString.*/
return ' (no repetitions)' /*failure to find repString.*/