How to resolve the algorithm Hex words step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hex words step by step in the Wren programming language

Table of Contents

Problem Statement

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. 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. Keeping only words which contain at least 4 distinct letters, display the same statistics but in decreasing order of decimal equivalent together with their total count.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hex words step by step in the Wren programming language

Source code in the wren programming language

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.")


  

You may also check:How to resolve the algorithm Flatten a list step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the S-BASIC programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Scala programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Unlambda programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Elena programming language