Rep-string: Difference between revisions
Content added Content deleted
m (Paddy3118 moved page Repeated string to Rep-string: Catchier title) |
Walterpachl (talk | contribs) (REXX added) |
||
Line 57: | Line 57: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: |
===Python: Procedural=== |
||
<lang python>def is_repeated(text): |
<lang python>def is_repeated(text): |
||
'check if the first part of the string is repeated throughout the string' |
'check if the first part of the string is repeated throughout the string' |
||
Line 128: | Line 128: | ||
'0100101101' is not a repeated string |
'0100101101' is not a repeated string |
||
'0100100' repeats '010' |
'0100100' repeats '010' |
||
'101' is not a repeated string |
|||
'1' is not a repeated string</pre> |
|||
=={{header|REXX}}== |
|||
<lang rexx>/* REXX *************************************************************** |
|||
* 11.05.2013 Walter Pachl |
|||
**********************************************************************/ |
|||
Call repstring '1001110011' |
|||
Call repstring '1110111011' |
|||
Call repstring '0010010010' |
|||
Call repstring '1010101010' |
|||
Call repstring '1111111111' |
|||
Call repstring '0100101101' |
|||
Call repstring '0100100' |
|||
Call repstring '101' |
|||
Call repstring '1' |
|||
Exit |
|||
repstring: |
|||
Parse Arg s |
|||
sq=''''s'''' |
|||
n=length(s) |
|||
Do l=length(s)%2 to 1 By -1 |
|||
If substr(s,l+1,l)=left(s,l) Then Leave |
|||
End |
|||
If l>0 Then Do |
|||
rep_str=left(s,l) |
|||
Do i=1 By 1 |
|||
If substr(s,i*l+1,l)<>rep_str Then |
|||
Leave |
|||
End |
|||
If left(copies(rep_str,n),length(s))=s Then |
|||
Say sq 'has a repetition length of' length(rep_str), |
|||
'i.e.' ''''rep_str'''' |
|||
Else |
|||
Say sq 'is not a repeated string' |
|||
End |
|||
Else |
|||
Say sq 'is not a repeated string' |
|||
Return</lang> |
|||
Output: |
|||
<pre>'1001110011' has a repetition length of 5 i.e. '10011' |
|||
'1110111011' has a repetition length of 4 i.e. '1110' |
|||
'0010010010' has a repetition length of 3 i.e. '001' |
|||
'1010101010' has a repetition length of 4 i.e. '1010' |
|||
'1111111111' has a repetition length of 5 i.e. '11111' |
|||
'0100101101' is not a repeated string |
|||
'0100100' has a repetition length of 3 i.e. '010' |
|||
'101' is not a repeated string |
'101' is not a repeated string |
||
'1' is not a repeated string</pre> |
'1' is not a repeated string</pre> |