RosettaCodeData/Task/Rep-string/NetRexx/rep-string.netrexx

62 lines
1.7 KiB
Plaintext

/* NetRexx */
options replace format comments java crossref symbols nobinary
/* REXX ***************************************************************
* 11.05.2013 Walter Pachl
**********************************************************************/
runSample(arg)
return
/**
* Test for rep-strings
* @param s_str a string to check for rep-strings
* @return Rexx string: boolean indication of reps, length, repeated value
*/
method repstring(s_str) public static
s_str_n = s_str.length()
rep_str = ''
Loop lx = s_str.length() % 2 to 1 By -1
If s_str.substr(lx + 1, lx) = s_str.left(lx) Then Leave lx
End lx
If lx > 0 Then Do label reps
rep_str = s_str.left(lx)
Loop ix = 1 By 1
If s_str.substr(ix * lx + 1, lx) <> rep_str Then
Leave ix
End ix
If rep_str.copies(s_str_n).left(s_str.length()) <> s_str Then
rep_str = ''
End reps
Return (rep_str.length() > 0) rep_str.length() rep_str
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
parse arg samples
if samples = '' then -
samples = -
'1001110011' -
'1110111011' -
'0010010010' -
'1010101010' -
'1111111111' -
'0100101101' -
'0100100' -
'101' -
'11' -
'00' -
'1'
loop w_ = 1 to samples.words()
in_str = samples.word(w_)
parse repstring(in_str) is_rep_str rep_str_len rep_str
sq = ''''in_str''''
tstrlen = sq.length().max(20)
sq=sq.right(tstrlen)
if is_rep_str then
Say sq 'has a repetition length of' rep_str_len "i.e. '"rep_str"'"
else
Say sq 'is not a repeated string'
end w_
return