CHALLENGE DESCRIPTION:
Write a program to determine the shortest repetition in a string. A string is said to have period p if it can be formed by concatenating one or more repetitions of another string of length p. For example, the string "xyzxyzxyzxyz" has period 3, since it is formed by 4 repetitions of the string "xyz". It also has periods 6 (two repetitions of "xyzxyz") and 12 (one repetition of "xyzxyzxyzxyz").
INPUT SAMPLE:
Your program should accept as its first argument a path to a filename. Each line will contain a string of up to 80 non-blank characters. E.g.
abcabcabcabc bcbcbcbcbcbcbcbcbcbcbcbcbcbc dddddddddddddddddddd adcdefg
OUTPUT SAMPLE:
Print out the smallest period of the input string. E.g.
3 2 1 7
My Code
#!/usr/bin/env ruby -w def shortest_repetition(str) candidate = '' str.each_char do |c| candidate << c return candidate.length if repeatable?(str, candidate) end end def repeatable?(str, candidate) repeat_num = str.length / candidate.length str == candidate * repeat_num end ARGF.each_line do |line| puts shortest_repetition(line.chomp) end