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 — for these 'use' commands!.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
use scripting additions
 
on join(lst, delim)
This can now return all the co-longest deranged anagrams when there are more than one. However it turns out that unixdict.txt only contains one. :)
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
-- Uses the customisable AppleScript-coded sort shown at <https://macscripter.net/viewtopic.php?pid=194430#p194430>.
set AppleScript's text item delimiters to astid
-- It's assumed scripters will know how and where to install it as a library.
return txt
use sorter : script "Custom Iterative Ternary Merge Sort"
end join
use scripting additions
 
on longestDerangedAnagrams(listOfWords)
script o
property wordList : listOfWords
property doctoredWordsgroupingTexts : {}wordList's items
property hitLengthderangementLength : 0
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 of my wordList
set groupCountgroupSize to b - a + 1
set wordLength to (count beginning of anagramGroup)
repeat with i from 1 to (groupCountgroupSize - 1)
set w1 to anagramGroup's item i of anagramGroup
repeat with j from (i + 1) to groupCountgroupSize
set w2 to anagramGroup's item j of anagramGroup
set areDeranged to true
repeat with c from 1 to wordLength
if (w1's character c of= w1 =w2's character c of w2) then
set areDeranged to false
exit repeat
end if
end repeat
-- Append any deranged pairs found to the output list and note theirthe wordwords' length.
if (areDeranged) then
set end of my output to {w1, w2}
set hitLengthderangementLength to wordLength
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.)
on isGreater(a, b)
set lenAaLen to (counta's a)length
set lenBbLen to (countb's b)length
if (lenAaLen = lenBbLen) then return (a > b) -- or (b < a)!
return (lenBaLen >< lenAbLen)
end isGreater
end script
set wordCount to (count o's wordList)
ignoring case
-- BuildReplace anotherthe listwords containing doctored versions ofin the inputgroupingTexts wordslist with theirsorted-character characters lexically sortedversions.
setrepeat astidwith toi AppleScript'sfrom text1 itemto delimiterswordCount
set AppleScriptchrs to o's textgroupingTexts's item delimiters toi's ""characters
tell sorter to sort(chrs, 1, -1, {})
repeat with thisWord in o's wordList
set theseCharso's to thisWordgroupingTexts's charactersitem i to join(chrs, "")
-- 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'sSort the list descending by text itemlength and ascending delimiters(say) toby astidvalue
-- within lengths. Echo the moves in the original word list.
tell sorter to sort(o's doctoredWordsgroupingTexts, 1, -1wordCount, {comparer:descendingByLengthAscendingByValueo, slave:{o's wordList}})
-- SortWork through the listruns of doctoredgrouping wordstexts, intostarting descendingwith order by length and ascending order bythe valuelongest withintexts.
-- 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 equal doctored words and test the corresponding originals for deranged pairs.
set i to 1
set currentText to beginning of o's doctoredWordsgroupingTexts
repeat with j from 2 to (count o's doctoredWordswordCount)
set thisText to item j of o's doctoredWordsgroupingTexts's item j
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 wordtext that's shorter than the longestany derangement(s) found.
if ((count thisText) < o's hitLengthderangementLength) then exit repeat
end repeat
if (j > i) then tell o to testPairs(i, j)
Line 791 ⟶ 793:
end longestDerangedAnagrams
 
-- The closing values of AppleScript 'run handler' variables not explicity declared local are
-- saved back to the script file afterwards — and "unixdict.txt" contains 25,104 words!
local wordFile, wordList
set wordFile to ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class furl»
-- The words in "unixdict.txt" are arranged one per line in alphabetical order.
-- Some contain punctuation characters, so they're best extracted as 'paragraphs' rather than as 'words'.
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}}
557

edits