How to resolve the algorithm Anagrams/Deranged anagrams step by step in the F# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Anagrams/Deranged anagrams step by step in the F# 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 F# programming language
Source code in the fsharp programming language
open System;
let keyIsSortedWord = Seq.sort >> Seq.toArray >> String
let isDeranged = Seq.forall2 (<>)
let rec pairs acc l = function
| [] -> acc
| x::rtail ->
pairs (acc @ List.fold (fun acc y -> (y, x)::acc) [] l) (x::l) rtail
[<EntryPoint>]
let main args =
System.IO.File.ReadAllLines("unixdict.txt")
|> Seq.groupBy keyIsSortedWord
|> Seq.fold (fun (len, found) (key, words) ->
if String.length key < len || Seq.length words < 2 then (len, found)
else
let d = List.filter (fun (a, b) -> isDeranged a b) (pairs [] [] (List.ofSeq words))
if List.length d = 0 then (len, found)
elif String.length key = len then (len, found @ d)
else (String.length key, d)
) (0, [])
|> snd
|> printfn "%A"
0
You may also check:How to resolve the algorithm HTTPS step by step in the Objeck programming language
You may also check:How to resolve the algorithm Loops/While step by step in the HolyC programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Nim programming language
You may also check:How to resolve the algorithm Concurrent computing step by step in the Rust programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Factor programming language