RosettaCodeData/Task/Rep-string/Julia/rep-string.julia

24 lines
641 B
Text
Raw Permalink Normal View History

2018-06-22 20:57:24 +00:00
function repstring(r::AbstractString)
2015-11-18 06:14:39 +00:00
n = length(r)
2018-06-22 20:57:24 +00:00
replst = String[]
for m in 1:n÷2
s = r[1:chr2ind(r, m)]
if (s ^ cld(n, m))[1:chr2ind(r, n)] != r continue end
2015-11-18 06:14:39 +00:00
push!(replst, s)
end
return replst
end
2018-06-22 20:57:24 +00:00
tests = ["1001110011", "1110111011", "0010010010", "1010101010", "1111111111",
"0100101101", "0100100", "101", "11", "00", "1",
"\u2200\u2203\u2200\u2203\u2200\u2203\u2200\u2203"]
2015-11-18 06:14:39 +00:00
for r in tests
2018-06-22 20:57:24 +00:00
replst = repstring(r)
if isempty(replst)
println("$r is not a rep-string.")
2015-11-18 06:14:39 +00:00
else
2018-06-22 20:57:24 +00:00
println("$r is a rep-string of ", join(replst, ", "), ".")
2015-11-18 06:14:39 +00:00
end
end