Anagrams: Difference between revisions
m
→{{header|AppleScript}}: Cosmetic makeover.
m (→{{header|AppleScript}}: Cosmetic makeover.) |
|||
Line 518:
<lang applescript>use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later — for these 'use' commands!
--
-- It's assumed
use sorter : script "Custom Iterative Ternary Merge Sort"
use scripting additions
on anagramsTask()
script o
property wordList :
property doctoredWords : {}
property longestRanges : {}
property
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
set
-- 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
end repeat
set AppleScript's text item delimiters to astid
-- Sort the list of doctored words to group them, rearranging the original
tell sorter to sort(o's doctoredWords, 1, -1, {slave:{o's wordList}})
-- Find the
set longestRunLength to 1
set i to 1
repeat with j from
if (thisText is not currentText) then
set thisRunLength to j - i
if (thisRunLength > longestRunLength) then
Line 563 ⟶ 564:
set end of o's longestRanges to {i, j - 1}
end if
set
set i to j
end if
end repeat
set o's longestRanges to {{i, j}}
else if (
set end of o's longestRanges to {i, j}
end if
-- The stable parallel sort above will have kept each group's words in alphabetical order.
set {i, j} to thisRange
end repeat▼
on isGreater(a, b)▼
return (a's beginning > b's beginning)▼
end isGreater▼
end script
end ignoring
▲ -- Get the original words occupying the same range(s) in their list.
▲ set {i, j} to item i of o's longestRanges
▲ set end of o's resultList to items i thru j of o's wordList
▲ end repeat
▲ -- As a final flourish, arrange the groups in order of their first words.
▲ on isGreater(a, b)
▲ return (a's beginning > b's beginning)
▲ end isGreater
▲ end script
▲ tell sorter to sort(o's resultList, 1, -1, {comparer:byFirstItem})
▲ return o's resultList
end anagramsTask
Line 597:
{{output}}
<
=={{header|AutoHotkey}}==
|