Hex words

From Rosetta Code
Revision as of 16:24, 10 November 2021 by PureFox (talk | contribs) (Created a new draft task, Hex words, and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Hex words is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

For the purposes of this task a hex word means a word which (in lower case form) consists entirely of the letters a, b, c, d, e and f.

Task

Using unixdict.txt, find all hex words with 4 letters or more.

Convert each such word to its decimal equivalent and compute its base 10 digital root.

Display all three in increasing order of digital root and show the total count of such words.

Also filter out all such words which contain at least 4 distinct letters and display the same statistics but in reverse order of decimal equivalent together with their total count.

Wren

Library: Wren-ioutil
Library: Wren-fmt
Library: Wren-math
Library: Wren-seq

<lang ecmascript>import "./ioutil" for FileUtil import "./fmt" for Conv, Fmt import "./math" for Int import "./seq" for Lst

var words = FileUtil.readLines("unixdict.txt").where { |w| w.count > 0 && w[0].bytes[0] < 103 } var hexDigits = "abcdef" var lines = [] for (word in words) {

   if (word.count < 4) continue
   if (word.all { |c| hexDigits.contains(c) }) {
       var num = Conv.atoi(word, 16)
       var dr = Int.digitalRoot(num)[0] 
       lines.add(Fmt.swrite("$-7s -> $-9s -> $d", word, num, dr))
   }

} lines.sort { |a, b| a[-1].bytes[0] < b[-1].bytes[0] } System.print(lines.join("\n")) System.print("\n%(lines.count) hex words with 4 or more letters found.\n") var digits4 = [] for (line in lines) {

   var word = line.split("->")[0].trimEnd()
   if (Lst.distinct(word.toList).count >= 4) digits4.add(line)

} digits4.sort { |a, b| Num.fromString(a.split("->")[1]) > Num.fromString(b.split("->")[1]) } System.print(digits4.join("\n")) System.print("\n%(digits4.count) such words found which contain 4 or more different digits.")</lang>

Output:
decade  -> 14600926  -> 1
ababa   -> 703162    -> 1
abbe    -> 43966     -> 1
dada    -> 56026     -> 1
deaf    -> 57007     -> 1
feed    -> 65261     -> 2
cede    -> 52958     -> 2
added   -> 712173    -> 3
bade    -> 47838     -> 3
abed    -> 44013     -> 3
decca   -> 912586    -> 4
beebe   -> 782014    -> 4
dade    -> 56030     -> 5
deface  -> 14613198  -> 6
bead    -> 48813     -> 6
babe    -> 47806     -> 7
fade    -> 64222     -> 7
dead    -> 57005     -> 8
facade  -> 16435934  -> 8
efface  -> 15727310  -> 8
dacca   -> 896202    -> 9
beef    -> 48879     -> 9
cafe    -> 51966     -> 9
deed    -> 57069     -> 9
face    -> 64206     -> 9
accede  -> 11325150  -> 9

26 hex words with 4 or more letters found.

facade  -> 16435934  -> 8
efface  -> 15727310  -> 8
deface  -> 14613198  -> 6
decade  -> 14600926  -> 1
accede  -> 11325150  -> 9
decca   -> 912586    -> 4
fade    -> 64222     -> 7
face    -> 64206     -> 9
deaf    -> 57007     -> 1
cafe    -> 51966     -> 9
bead    -> 48813     -> 6
bade    -> 47838     -> 3
abed    -> 44013     -> 3

13 such words found which contain 4 or more different digits.