cjnevin / Dawg

Directed acyclic word graph in Swift
MIT License
7 stars 2 forks source link

Feature request: Search for anagrams with a minWordLength, maxWordLength #5

Open devindazzle opened 7 years ago

devindazzle commented 7 years ago

I think it could be helpful to have a function that provides all anagrams within a range for wordLength. Something like:

open func anagrams( withLetters letters: [Character], maxWordLength: Int, minWordLength: Int = 1, filledLetters: [Int: Character] = [Int: Character](), blankLetter: Character = "?") -> [String]?

cjnevin commented 7 years ago

Thanks for the suggestion, this has some complexities that might be unclear without proper errors being thrown and proper thought being put into it.

It could currently be achieved with a flatMap, however results should be validated for accuracy to ensure the edges don't clash with values in filledLetters that fall below the maxWordLength.

For example, if we find 'cat' (with a max word length of 3) but the 4th filled letter is a 'z' obviously 'catz' should be invalidated.

Example of a flatMap: let results = (2..<5).flatMap { dawg.anagrams(withLetters: letters, wordLength: $0, filledLetters: filledLetters) }

These results would need to be sanitised to resolve the issue raised above ('catz').