State name puzzle: Difference between revisions

m
→‎{{header|AppleScript}}: New sort handler URL, tidy-up.
m (→‎{{header|11l}}: `sorted(String)` now returns `String`, not `Array[Char]`)
m (→‎{{header|AppleScript}}: New sort handler URL, tidy-up.)
Line 81:
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script ¬
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://www.macscripter.net/viewtopic.php?pid=194430#p194430t/timsort-and-nigsort/71383/3>
 
on stateNamePuzzle()
Line 98 ⟶ 99:
"Washington", "West Virginia", "Wisconsin", "Wyoming", ¬
"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"}
property namePairsworkList : {}
property checkList : {}
-- Custom comparison handler for the sort.
on isGreater(a, b)
return (beginning of a comes after> beginning of b)
end isGreater
end script
Line 105 ⟶ 110:
-- Remove duplicates.
repeat with i from 1 to (count o's stateNames)
set thisName to item i of o's stateNames's item i
if ({thisName} is not in o's checkListworkList) then set end of o's checkListworkList to thisName
end repeat
set o's stateNames to o's checkListworkList
-- Build a list of lists containing unique pairs of names preceded by
-- textstext composed of their combined and sorted visible characters, sorted.
set o's workList to {}
set stateCount to (count o's stateNames)
repeat with i from 1 to (stateCount - 1)
set name1 to item i of o's stateNames's item i
repeat with j from (i + 1) to stateCount
set name2 to item j of o's stateNames's item j
set chrs to characters of (name1 & name2)'s characters
tell sorter to sort(chrs, 1, -1, {})
set end of o's namePairsworkList to {word 1 of join(chrs, "")'s word 1, {name1, name2}}
end repeat
end repeat
-- Sort the lists on the character strings
set pairCount to (count o's namePairsworkList)
tell sorter to sort(o's namePairsworkList, 1, pairCount, {comparer:meo})
-- Look for groups of equal character strings and match
Line 132 ⟶ 138:
set l to 1
repeat while (l < pairCount)
set chrs to beginning of item l of o's namePairsworkList's item l
set r to l
repeat while ((r < pairCount) and (beginning of o's workList's item (r + 1) of o's namePairs = chrs))
set r to r + 1
end repeat
if (r > l) then
repeat with i from l to (r - 1)
set {name1, name2} to rest of item iend of o's namePairsworkList's item i
set text1 to join(result, " and ") & " --> "
repeat with j from (i + 1) to r
set pair2 to rest of item jend of o's namePairsworkList's item j
if (not (({name1} is in pair2) or ({name2} is in pair2))) then
set end of output to text1 & join(pair2, " and ")
end if
Line 155 ⟶ 161:
return join(output, linefeed)
end stateNamePuzzle
 
-- Custom comparison handler used when the sort comparer is 'me'. Compares the first items of two lists.
on isGreater(a, b)
return (beginning of a comes after beginning of b)
end isGreater
 
on join(lst, delim)
557

edits