State name puzzle: Difference between revisions
m
→{{header|AppleScript}}: New sort handler URL, tidy-up.
Alextretyak (talk | contribs) 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 ¬
on stateNamePuzzle()
Line 98 ⟶ 99:
"Washington", "West Virginia", "Wisconsin", "Wyoming", ¬
"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"}
property
-- Custom comparison handler for the sort.
on isGreater(a, b)▼
end isGreater▼
end script
Line 105 ⟶ 110:
-- Remove duplicates.
repeat with i from 1 to (count o's stateNames)
set thisName to
if ({thisName} is not in o's
end repeat
set o's stateNames to o's
-- Build a list of lists containing unique pairs of names preceded by
--
set o's workList to {}
set stateCount to (count o's stateNames)
repeat with i from 1 to (stateCount - 1)
set name1 to
repeat with j from (i + 1) to stateCount
set name2 to
set chrs to
tell sorter to sort(chrs, 1, -1, {})
set end of o's
end repeat
end repeat
-- Sort the lists on the character strings
set pairCount to (count o's
tell sorter to sort(o's
-- Look for groups of equal character strings and match
Line 132 ⟶ 138:
set l to 1
repeat while (l < pairCount)
set chrs to beginning
set r to l
repeat while ((r < pairCount) and (beginning of o's workList's item (r + 1)
set r to r + 1
end repeat
if (r > l) then
repeat with i from l to (r - 1)
set {name1, name2} to
set text1 to join(result, " and ") & " --> "
repeat with j from (i + 1) to r
set pair2 to
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
▲on isGreater(a, b)
▲ return (beginning of a comes after beginning of b)
▲end isGreater
on join(lst, delim)
|