not genial but working...

    robert

def find_idx(elem, seq, start = 0)
  for i in start .. seq.length-1
    return i if elem == seq[i]
  end

  nil
end

def find_longest(seq_1, start_1, seq_2, start_2)
  i = 0

  while seq_1[start_1 + i] == seq_2[start_2 + i]
    i += 1
  end

  i
end

def match_seq(seq_1, start_1, seq_2, start_2, len)
  return false if start_1+len>=seq_1.length ||
start_2+len>=seq_2.length

  for i in 0..(len-1)
    return false unless seq_1[start_1 + i] == seq_2[start_2 + i]
  end

  true
end

def find_k(seq)
  start = 0

  while start < seq.length
    i2 = find_idx(seq[start], seq, start+1)

    if i2
      k = find_longest(seq, start, seq, i2)

      if k && k > 1 && k<=(i2-start)
        start2 = i2+k

        while match_seq(seq, start, seq, start2, k)
          start2 += k
        end

        if start2 == seq.length
          # puts "start = #{start}"
          return k
        end
      end
    end

    start += 1
  end

  nil
end

def test_seq(seq)
  puts "k = #{find_k(seq) || '(invalid)'}"
end

# MAIN

test_seq [0,1,2,3,7,2,3,2,3]
test_seq [0,1,2,3,7,2,3,2,3,2]
test_seq [0,1,2,3,7,2,3,2,3,5]