Hex words: Difference between revisions

Content added Content deleted
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Added AppleScript.)
Line 260: Line 260:
Found 13 hex words with 4 or more distinct digits
Found 13 hex words with 4 or more distinct digits
</pre>
</pre>

=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">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" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>

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 unixdictPath to (path to desktop as text) & "unixdict.txt" -- HFS path. Edit as required.
set allWords to (read (unixdictPath as «class furl») from 1 as «class utf8»)'s words
set hexwordFilter to current application's class "NSPredicate"'s
predicateWithFormat:("self MATCHES '^[a-f]{4,}+$'")
set hexwords to ((current application'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) -- Actually an AppleScript integer.
set root to digitalRoot(decimal, 10) -- Ditto.
set thisEntry to {thisWord, decimal, root}
set end of list1 to thisEntry
if ((current application's class "NSSet"'s setWithArray:(thisWord's characters))'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()</syntaxhighlight>

{{output}}
<syntaxhighlight lang="applescript">"
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"</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==