Sha256: 5fb8bd5239099091feace96996833e16757ad6a1fea7453e9260c6cef58aac67
Contents?: true
Size: 733 Bytes
Versions: 349
Compression:
Stored size: 733 Bytes
Contents
import algorithm, sequtils, strutils type TAnagram = tuple word: string chars: seq[char] # the lowercased and sorted chars of the word proc isAnagramTo(a, b: TAnagram): bool {.noSideEffect, procVar.} = a.chars == b.chars and cmpIgnoreCase(a.word, b.word) != 0 proc anagram(word: string): TAnagram = var chars = toSeq(word.toLower().items) sort(chars, cmp[char]) (word, chars) proc detectAnagrams*(word: string, candidates: seq[string]): seq[string] = ## Returns a sequence of those `candidates` that are anagrams of `word`. ## ## .. code-block:: nimrod ## assert detectAnagrams("Ant", @["tan", "ant"]) == @["tan"] let reference = anagram(word) candidates.filterIt(anagram(it).isAnagramTo(reference))
Version data entries
349 entries across 349 versions & 1 rubygems