How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Arturo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Arturo programming language
Table of Contents
Problem Statement
Two or more words are said to be anagrams if they have the same characters, but in a different order. By analogy with derangements we define a deranged anagram as two words with the same characters, but in which the same character does not appear in the same position in both words. Use the word list at unixdict to find and display the longest deranged anagram.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Arturo programming language
Source code in the arturo programming language
isDeranged?: function [p][
[a,b]: p
loop 0..dec size a 'i [
if a\[i] = b\[i] [return false]
]
return true
]
wordset: map read.lines relative "unixdict.txt" => strip
anagrams: #[]
loop wordset 'word [
anagram: sort to [:char] word
unless key? anagrams anagram ->
anagrams\[anagram]: new []
anagrams\[anagram]: anagrams\[anagram] ++ word
]
deranged: select values anagrams 'anagram [ 2 = size anagram]
maxDeranged: ["" ""]
loop deranged 'd [
if (size first d) > size first maxDeranged [
pair: @[first d, last d]
if isDeranged? pair [
maxDeranged: pair
]
]
]
print maxDeranged
You may also check:How to resolve the algorithm Sum of elements below main diagonal of matrix step by step in the Raku programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Arturo programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Wortel programming language
You may also check:How to resolve the algorithm Environment variables step by step in the hexiscript programming language
You may also check:How to resolve the algorithm Dice game probabilities step by step in the Tcl programming language