Rep-string: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
m Fix Perl 6 -> Raku in comments
Crystal code
Line 758: Line 758:
("101" . "Not a repeating string") ("11" . "1") ("00" . "0")
("101" . "Not a repeating string") ("11" . "1") ("00" . "0")
("1" . "Not a repeating string"))
("1" . "Not a repeating string"))
</pre>

=={{header|Crystal}}
{{trans|Go}}
<lang ruby>def rep(s : String) : Int32
x = s.size // 2

while x > 0
return x if s.starts_with? s[x..]
x -= 1
end

0
end

def main
%w(
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1
).each do |s|
n = rep s
puts n > 0 ? "\"#{s}\" #{n} rep-string \"#{s[..(n - 1)]}\"" : "\"#{s}\" not a rep-string"
end
end

main
</lang>

{{out}}
<pre>
"1001110011" 5 rep-string "10011"
"1110111011" 4 rep-string "1110"
"0010010010" 3 rep-string "001"
"1010101010" 4 rep-string "1010"
"1111111111" 5 rep-string "11111"
"0100101101" not a rep-string
"0100100" 3 rep-string "010"
"101" not a rep-string
"11" 1 rep-string "1"
"00" 1 rep-string "0"
"1" not a rep-string
</pre>
</pre>