Anagrams/Deranged anagrams: Difference between revisions
m
→{{header|AppleScript}}: New sort handler URL, tidy-up.
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
m (→{{header|AppleScript}}: New sort handler URL, tidy-up.) |
||
Line 706:
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
use scripting additions▼
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
▲<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later — for these 'use' commands!
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
▲use scripting additions
on longestDerangedAnagrams(listOfWords)
script o
property wordList : listOfWords
property
property
property output : {}
-- Test for any deranged pairs amongst the words of an anagram group.
on testPairs(a, b)
set anagramGroup to my wordList's items a thru b
set
set wordLength to (count beginning of anagramGroup)
repeat with i from 1 to (
set w1 to anagramGroup's item i
repeat with j from (i + 1) to
set w2 to anagramGroup's item j
set areDeranged to true
repeat with c from 1 to wordLength
if (w1's character c
set areDeranged to false
exit repeat
end if
end repeat
-- Append any deranged pairs found to the output
if (areDeranged) then
set end of
set
end if
end repeat
end repeat
end testPairs
-- Custom comparison handler for the sort. Text a should go after text b if
-- it's the same length and has a greater lexical value or it's shorter than b.
-- (The lexical sort direction isn't really relevant. It's just to group equal texts.)
end script
set wordCount to (count o's wordList)
ignoring case
--
set
tell sorter to sort(chrs, 1, -1, {})
set
end repeat
-- within lengths. Echo the moves in the original word list.
tell sorter to sort(o's
--
▲ 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
▲ tell sorter to sort(o's doctoredWords, 1, -1, {comparer:descendingByLengthAscendingByValue, slave:{o's wordList}})
set i to 1
set currentText to beginning of o's
repeat with j from 2 to (
set thisText to
if (thisText is not currentText) then
if (j - i > 1) then tell o to testPairs(i, j - 1)
Line 782 ⟶ 784:
set i to j
end if
-- Stop on reaching a
if ((count thisText) < o's
end repeat
if (j > i) then tell o to testPairs(i, j)
Line 791 ⟶ 793:
end longestDerangedAnagrams
local wordFile, wordList
set wordFile to ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class furl»▼
▲set wordFile to ((path to desktop as text) & "unixdict.txt") as «class furl»
set wordList to paragraphs of (read wordFile as «class utf8»)
return longestDerangedAnagrams(wordList)</syntaxhighlight>
Line 802 ⟶ 800:
{{output}}
<syntaxhighlight lang="applescript">{{"excitation", "intoxicate"}}</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
|