Sunday, 14 February 2010
Anagrams
Another programming challenge is to compute anagrams from a dictionary and output the longest sets of anagrams first. We submitted the following elegant 3-line F# solution:
let words = System.IO.File.ReadAllLines @"unixdict.txt" [|for _, words in Seq.groupBy (Array.ofSeq >> Array.sort) words -> Array.ofSeq words|] |> Array.sortBy (fun s -> -Seq.length s)
This tiny program produces the following sets of anagrams in only half second:
val it : string [] [] = [|[|"alger"; "glare"; "lager"; "large"; "regal"|]; [|"caret"; "carte"; "cater"; "crate"; "trace"|]; [|"abel"; "able"; "bale"; "bela"; "elba"|]; [|"elan"; "lane"; "lean"; "lena"; "neal"|]; [|"angel"; "angle"; "galen"; "glean"; "lange"|]; [|"evil"; "levi"; "live"; "veil"; "vile"|]; [|"aden"; "dane"; "dean"; "edna"|]; [|"emit"; "item"; "mite"; "time"|]; [|"pare"; "pear"; "rape"; "reap"|]; [|"esprit"; "priest"; "sprite"; "stripe"|]; [|"hare"; "hear"; "hera"; "rhea"|]; [|"resin"; "rinse"; "risen"; "siren"|]; [|"keats"; "skate"; "stake"; "steak"|]; [|"lascar"; "rascal"; "sacral"; "scalar"|]; [|"amen"; "mane"; "mean"; "name"|]; [|"nepal"; "panel"; "penal"; "plane"|]; [|"lemon"; "melon"; "menlo"; "monel"|]; [|"least"; "slate"; "stale"; "steal"|]; [|"leap"; "pale"; "peal"; "plea"|]; [|"ames"; "mesa"; "same"; "seam"|]; [|"leapt"; "petal"; "plate"; "pleat"|]; [|"lien"; "line"; "neil"; "nile"|]; [|"abet"; "bate"; "beat"; "beta"|]; [|"mate"; "meat"; "tame"; "team"|]; [|"beard"; "bread"; "debar"; "debra"|]; [|"lament"; "mantel"; "mantle"; "mental"|]; [|"aires"; "aries"; "arise"; "raise"|]; [|"enol"; "leon"; "lone"; "noel"|]; [|"cereus"; "recuse"; "rescue"; "secure"|]; [|"manor"; "moran"; "norma"; "roman"|]; [|"latus"; "sault"; "talus"; "tulsa"|]; [|"diet"; "edit"; "tide"; "tied"|]; [|"lima"; "mail"; "mali"; "mila"|]; [|"are"; "ear"; "era"; "rae"|]; [|"apt"; "pat"; "pta"; "tap"|]; [|"dare"; "dear"; "erda"; "read"|]; [|"ate"; "eat"; "eta"; "tea"|]; [|"ant"; "nat"; "tan"|]; [|"yates"; "yeast"; "yeats"|]; [|"nerve"; "never"; "verne"|]; [|"ether"; "there"; "three"|]; [|"dave"; "vade"; "veda"|]; [|"earn"; "near"; "rena"|]; [|"brag"; "garb"; "grab"|]; [|"magneto"; "megaton"; "montage"|]; [|"earnest"; "eastern"; "nearest"|]; [|"den"; "end"; "ned"|]; [|"lair"; "liar"; "rail"|]; [|"alton"; "talon"; "tonal"|]; [|"now"; "own"; "won"|]; [|"earth"; "hater"; "heart"|]; [|"dire"; "reid"; "ride"|]; [|"bard"; "brad"; "drab"|]; [|"bare"; "bear"; "brae"|]; [|"earthen"; "hearten"; "teheran"|]; [|"kale"; "lake"; "leak"|]; [|"arnold"; "roland"; "ronald"|]; [|"cpu"; "cup"; "puc"|]; [|"earthy"; "hearty"; "thayer"|]; [|"abut"; "tabu"; "tuba"|]; [|"lame"; "male"; "meal"|]; [|"hank"; "kahn"; "khan"|]; [|"riot"; "tori"; "trio"|]; [|"dater"; "trade"; "tread"|]; [|"rite"; "tier"; "tire"|]; [|"army"; "mary"; "myra"|]; [|"alert"; "alter"; "later"|]; [|"acts"; "cast"; "scat"|]; [|"carven"; "cavern"; "craven"|]; [|"grate"; "great"; "greta"|]; [|"rosa"; "soar"; "sora"|]; [|"argot"; "gator"; "groat"|]; [|"demo"; "dome"; "mode"|]; [|"klein"; "kline"; "liken"|]; [|"lisa"; "sail"; "sial"|]; [|"cruel"; "lucre"; "ulcer"|]; [|"along"; "anglo"; "logan"|]; [|"listen"; "silent"; "tinsel"|]; [|"list"; "silt"; "slit"|]; [|"iran"; "nair"; "rain"|]; [|"baird"; "braid"; "rabid"|]; [|"dearth"; "hatred"; "thread"|]; [|"ape"; "epa"; "pea"|]; [|"eros"; "rose"; "sore"|]; [|"ares"; "sear"; "sera"|]; [|"acm"; "cam"; "mac"|]; [|"gas"; "gsa"; "sag"|]; [|"acme"; "came"; "mace"|]; [|"part"; "rapt"; "trap"|]; [|"argon"; "groan"; "organ"|]; [|"ester"; "steer"; "terse"|]; [|"acre"; "care"; "race"|]; [|"brain"; "brian"; "rabin"|]; [|"parse"; "spare"; "spear"|]; [|"bin"; "ibn"; "nib"|]; [|"result"; "rustle"; "ulster"|]; [|"armco"; "macro"; "marco"|]; [|"janos"; "jason"; "jonas"|]; [|"ante"; "nate"; "neat"|]; [|"goer"; "gore"; "ogre"|]; ...|]
This is one of the shortest and fastest of all the solutions on Rosetta Code. For example, the Haskell solution is more than twice as long at 8 lines of code.
Labels: anagram, rosetta code, sequence expressions, sort, string
The correct answer is that given by most of the other examples such as Haskel, Common Lisp, Factor, Groovy, Python, ...
Might you have interpreted the task goal differently from others, and if so, how?
Thanks, Paddy.
- Paddy.
@Anonymous: I have uploaded a correct solution that is still substantially shorter than the Haskell.
You should compare number of characters, possibly renaming all variables to 1 character identifiers.
Subscribe to Post Comments [Atom]
Links to this post:
<< Home
Subscribe to Posts [Atom]

