How to resolve the algorithm Hex words step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hex words step by step in the V (Vlang) 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 V (Vlang) programming language
Source code in the v programming language
import os
import strconv
import math
fn digital_root(nn i64) i64 {
mut n := nn
for n>9 {
mut tot := i64(0)
for n>0 {
tot += n%10
n = int(math.floor(n/10))
}
n = tot
}
return n
}
fn main() {
hex_digits := 'abcdef'
words := os.read_lines('unixdict.txt')?
mut lines := [][]string{}
//println(words)
for word in words {
if word.len < 4 {
continue
}
if word.split('').all(hex_digits.index(it) or {-1} >= 0) {
num := strconv.parse_int(word, 16, 32) or {-1}
lines << [word, num.str(), digital_root(num).str()]
}
}
lines.sort_with_compare(fn(mut a []string, mut b []string) int {
if a[2].int()<b[2].int(){
return -1
}else if a[2].int()>b[2].int(){
return 1
}
return 0
})
for line in lines {
println('${line[0]:8} -> ${line[1]:9} -> ${line[2]}')
}
println('$lines.len hex words with 4 or more letters found')
lines = lines.filter(fn (a []string) bool {
mut s := map[string]bool{}
for t in a[0].split('') {
if t !in s {
s[t] = true
}
}
return s.len > 3
})
for line in lines {
println('${line[0]:8} -> ${line[1]:9} -> ${line[2]}')
}
println('$lines.len hex words with 4 or more distinct letters found')
}
You may also check:How to resolve the algorithm Atomic updates step by step in the Nim programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Array length step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Sexy primes step by step in the Haskell programming language