require "table2" local fmt = require "fmt" local tests = {"abracadabra", "seesaw", "elk", "grrrrrr", "up", "a"} for tests as s do local t = s:split(""):shuffle() for i = 1, #t do for j = 1, #t do if i != j and t[i] != s[j] and t[j] != s[i] then t[i], t[j] = t[j], t[i] break end end end local c = range(1, #s):count(|i| -> s[i] == t[i]) fmt.print("%s, %s, (%d)\n", s, t:concat(""), c) end