Skip to content

Instantly share code, notes, and snippets.

@syedatifakhtar
Last active December 10, 2018 11:19
Show Gist options
  • Save syedatifakhtar/89ee76e4cbecb19b66ec551244a72e04 to your computer and use it in GitHub Desktop.
Save syedatifakhtar/89ee76e4cbecb19b66ec551244a72e04 to your computer and use it in GitHub Desktop.
AnagramExplorer - Internal vs External Motivation
import scala.io.Source
object Anagrams {
type Word = String
type Sentence = List[Word]
type Occurrences = List[(Char, Int)]
val dictionary: List[Word] = {
Source.fromFile("/Users/syedatifakhtar/Downloads/linuxwords.txt").getLines().toList
}
def wordOccurrences(w: Word): Occurrences = {
w.toLowerCase.groupBy(c => c).map(x => (x._1, x._2.length)).toList.sortBy(x => x)
}
def sentenceOccurrences(s: Sentence): Occurrences = wordOccurrences(s.mkString)
lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = dictionary.groupBy(wordOccurrences).withDefaultValue(Nil)
def wordAnagrams(word: Word): List[Word] = dictionaryByOccurrences(wordOccurrences(word))
def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match {
case Nil => List(List())
case (char, n) :: tail => {
for {
i <- 0 to n
next <- combinations(tail)
} yield if (i == 0) next else (char, i) :: next
}.toList
}
def subtract(x: Occurrences, y: Occurrences): Occurrences = {
y.foldLeft(x.toMap)((acc, y1) => acc(y1._1) - y1._2 match {
case 0 => acc - y1._1
case n => acc.updated(y1._1, n)
}).toList.sortBy(x => x._1)
}
def sentenceAnagrams(sentence: Sentence): List[Sentence] = {
def anagrams(occurrences: Occurrences): List[Sentence] = occurrences match {
case Nil => List(List())
case _ =>
for {
occurrence <- combinations(occurrences)
word <- dictionaryByOccurrences(occurrence)
tail <- anagrams(subtract(occurrences, occurrence))
} yield word :: tail
}
anagrams(sentenceOccurrences(sentence))
}
}
object AnagramExplore {
type Word = String
type Sentence = List[Word]
type Occurrences = List[(Char, Int)]
def wordOccurrences(w: Word): Occurrences = ???
def wordAnagrams: Word => List[Word] = ???
def sentenceOccurrences: Sentence => Occurrences = ???
def subtract(x: Occurrences, y: Occurrences): Occurrences = ???
def combinations: Occurrences => List[Occurrences] = ???
lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = ???
def anagrams: Occurrences => List[Sentence] = {
occurrences =>
for {
occurrence <- combinations(occurrences)
word <- dictionaryByOccurrences(occurrence)
tail <- anagrams(subtract(occurrences, occurrence))
} yield word :: tail
}
def sentenceAnagrams: Sentence => List[Sentence] = sentenceOccurrences.andThen(anagrams).apply(_)
def paragraphAnagrams: String => List[Sentence] => List[Sentence] = ???
}
val occurrences = Anagrams.sentenceOccurrences(List("I", "Love", "You"))
val list = Anagrams.combinations(occurrences).map(Anagrams.dictionaryByOccurrences).filter(_.nonEmpty)
Anagrams.sentenceAnagrams(List("I", "Love", "You"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment