How to resolve the algorithm Hex words step by step in the AppleScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hex words step by step in the AppleScript 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 AppleScript programming language
Source code in the applescript programming language
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
on hexToInt(hex)
set digits to "0123456789ABCDEF"
set n to 0
set astid to AppleScript's text item delimiters
ignoring case
repeat with thisDigit in hex
set AppleScript's text item delimiters to thisDigit
set n to n * 16 + (count digits's first text item)
end repeat
end ignoring
set AppleScript's text item delimiters to astid
return n
end hexToInt
on digitalRoot(r, base)
repeat until (r < base)
set n to r
set r to 0
repeat until (n = 0)
set r to r + n mod base
set n to n div base
end repeat
end repeat
return r as integer
end digitalRoot
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
on task()
set ca to current application
set unixdictPath to (path to desktop as text) & "unixdict.txt"
set allWords to (read (unixdictPath as «class furl») from 1 as «class utf8»)'s words
set hexwordFilter to ca's class "NSPredicate"'s ¬
predicateWithFormat:("self MATCHES '^[a-f]{4,}+$'")
set hexwords to ((ca's class "NSArray"'s arrayWithArray:(allWords))'s ¬
filteredArrayUsingPredicate:(hexwordFilter))
set {list1, list2} to {{}, {}}
repeat with thisWord in hexwords
set thisWord to thisWord as text
set decimal to hexToInt(thisWord)
set root to digitalRoot(decimal, 10)
set thisEntry to {thisWord, decimal, root}
set end of list1 to thisEntry
set distinctChars to (ca's class "NSSet"'s setWithArray:(thisWord's characters))
if (distinctChars's |count|() > 3) then set end of list2 to thisEntry
end repeat
-- Sort list1 on its sublists' digital root values.
script
on isGreater(a, b)
return (a's end > b's end)
end isGreater
end script
tell sorter to sort(list1, 1, -1, {comparer:result})
-- Reverse sort list2 on its sublists' decimal equivalent values.
script
on isGreater(a, b)
return (a's 2nd item < b's 2nd item)
end isGreater
end script
tell sorter to sort(list2, 1, -1, {comparer:result})
-- Format for display and output.
set padding to " "
repeat with thisList in {list1, list2}
repeat with thisEntry in thisList
tell thisEntry to set its contents to ¬
text 1 thru 9 of (its beginning & padding) & ¬
text -9 thru -1 of (padding & its 2nd item) & ¬
text -9 thru -1 of (padding & its end)
end repeat
set thisList's beginning to "
Word Decimal Root
----------------------------"
end repeat
set beginning of list1 to linefeed & ((count list1) - 1) & ¬
" 4+-character hexwords, sorted on their decimal equivalents' digital roots:"
set beginning of list2 to linefeed & "The " & ((count list2) - 1) & ¬
" with at least 4 /different/ characters, reverse sorted on their decimal equivalents:"
return join({list1, list2}, linefeed)
end task
task()
"
26 4+-character hexwords, sorted on their decimal equivalents' digital roots:
Word Decimal Root
----------------------------
ababa 703162 1
abbe 43966 1
dada 56026 1
deaf 57007 1
decade 14600926 1
cede 52958 2
feed 65261 2
abed 44013 3
added 712173 3
bade 47838 3
beebe 782014 4
decca 912586 4
dade 56030 5
bead 48813 6
deface 14613198 6
babe 47806 7
fade 64222 7
dead 57005 8
efface 15727310 8
facade 16435934 8
accede 11325150 9
beef 48879 9
cafe 51966 9
dacca 896202 9
deed 57069 9
face 64206 9
The 13 with at least 4 /different/ characters, reverse sorted on their decimal equivalents:
Word Decimal Root
----------------------------
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"
You may also check:How to resolve the algorithm The sieve of Sundaram step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Forth programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Beef programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Wren programming language