RosettaCodeData/Task/Hex-words/Pluto/hex-words.pluto
2026-04-30 12:34:36 -04:00

26 lines
1 KiB
Text

local int = require "int"
local word_list = "unixdict.txt" -- local copy
local nl = os.platform == "windows" ? "\r\n" : "\n"
local words = io.contents(word_list):rstrip():split(nl)
words = words:filter(|w| -> #w > 3 and w[1]:byte() < 103): reorder()
local hex_digits = "abcdef"
local lines = {}
for words as word do
if word:split(""):checkall(|c| -> c in hex_digits) then
local num = tonumber(word, 16)
local dr = int.digroot(num)[1]
lines:insert(string.format("%-7s -> %-9s -> %d", word, num, dr))
end
end
lines:sort(|a, b| -> a[-1]:byte() < b[-1]:byte())
print(lines:concat("\n"))
print($"\n{#lines} hex words with 4 or more letters found.\n")
local digits4 = {}
for lines as line do
local word = line:split("->")[1]:rstrip()
if #word:split(""):dedup():reorder() >= 4 then digits4:insert(line) end
end
digits4:sort(|a, b| -> tonumber(a:split("->")[2]) > tonumber(b:split("->")[2]))
print(digits4:concat("\n"))
print($"\n{#digits4} such words found which contain 4 or more different digits.")