Anagrams/Deranged anagrams: Difference between revisions

Added AppleScript.
(Added AppleScript.)
Line 225:
Longest deranged anagrams: intoxicate and excitation
</pre>
 
=={{header|AppleScript}}==
 
The task description implies that there's just one longest deranged anagram in the file, so this solution only examines instances where exactly two words share the same characters and returns the first longest deranged pair found. Code written without foreknowledge of a lone result would need to be able to return several (as with the [[Anagrams|Anagrams]] task) and have a policy about what to do where three or more words share the same characters and at least one is deranged against all the others, but the others aren't necessarily deranged amongst themselves.
 
<lang applescript>use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later — for these 'use' commands!
-- Uses the customisable AppleScript-coded sort shown at <https://macscripter.net/viewtopic.php?pid=194430#p194430>.
-- It's assumed scripters will know how and where to install it as a library.
use sorter : script "Custom Iterative Ternary Merge Sort"
use scripting additions
 
on derangedAnagramsTask()
script o
property wordList : missing value
property doctoredWords : {}
property output : {}
-- Test the two words of an anagram pair for derangement.
on areDeranged(a, b)
set {w1, w2} to items a thru b of my wordList
repeat with i from 1 to (count w1)
if (character i of w1 = character i of w2) then return false
end repeat
set output to {w1, w2}
return true
end areDeranged
end script
-- The words in "unixdict.txt" are in alphabetical order, one per line.
-- Some contain punctuation characters, so they're best extracted as 'paragraphs' rather than as 'words'.
set wordFile to ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class furl»
set o's wordList to paragraphs of (read wordFile as «class utf8»)
ignoring case
-- Build another list containing doctored versions of the same words with their characters lexically sorted.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
repeat with thisWord in o's wordList
set theseChars to thisWord's characters
-- A straight ascending in-place sort here.
tell sorter to sort(theseChars, 1, -1, {}) -- Params: (list, start index, end index, customisation spec.).
set end of o's doctoredWords to theseChars as text
end repeat
set AppleScript's text item delimiters to astid
-- Sort the list of doctored words into descending order by length and ascending order by value within
-- each length, rearranging the original-word list in parallel to maintain the index correspondence.
script descendingByLengthAscendingByValue
on isGreater(a, b)
set lenA to (count a)
set lenB to (count b)
if (lenA = lenB) then return (a > b)
return (lenB > lenA)
end isGreater
end script
tell sorter to sort(o's doctoredWords, 1, -1, {comparer:descendingByLengthAscendingByValue, slave:{o's wordList}})
-- Locate each run of just two equal doctored words and test the corresponding originals for derangement.
set i to 1
set currentText to beginning of o's doctoredWords
repeat with j from 2 to (count o's doctoredWords)
set thisText to item j of o's doctoredWords
if (thisText is not currentText) then
if ((j - i = 2) and o's areDeranged(i, j - 1)) then exit repeat -- Finish as soon as there's a hit.
set currentText to thisText
set i to j
end if
end repeat
if (j - i = 1) then o's areDeranged(i, j)
end ignoring
return o's output
end derangedAnagramsTask
 
derangedAnagramsTask()</lang>
 
{{output}}
<lang applescript>{"excitation", "intoxicate"}</lang>
 
=={{header|AutoHotkey}}==
557

edits