Skip to content

Instantly share code, notes, and snippets.

@rampart81
Last active August 29, 2015 14:26
Show Gist options
  • Save rampart81/b0eb90b4d65cd06f3a22 to your computer and use it in GitHub Desktop.
Save rampart81/b0eb90b4d65cd06f3a22 to your computer and use it in GitHub Desktop.
김희택 & 송은우
import scala.collection.mutable
object Test1 extends App {
// #1 Accept & parsing input
val testCaseNum = 1
val lineNum = 4
val sentences = Seq(
"he drove into a cul de sac" ,
"elle a conduit sa voiture" ,
"il a conduit dans un cul de sac" ,
"il mange pendant que il conduit sa voiture"
)
val separator = " "
// #2 Graph generation
val graph: mutable.Map[String, Seq[Integer]] = mutable.Map()
sentences.map { line =>
val index: Integer = sentences.indexOf(line)
line.split(separator).map { word =>
graph.get(word) match {
case Some(sentences) => graph.update(word, sentences :+ index)
case None => graph.update(word, Seq(index))
}
}
}
// #3 Graph travelse & get the output
// 1. 문장 0 에 속해 있는 단어를 다 찾는다
// 2. 그 단어들이 속해 있는 문장을 하나 하나 찾아 간다
// 3. 그 중에 문장 1 이 나오면 +1 을 하고 exit
// 4. 그렇지 않고 모든 문장을 다 방문 했우면 exit
def findOutput(index: Integer): Long = {
val currentWords = graph.filter(_._2.contains(index)).keys
println(s"current words: $currentWords")
currentWords.map { word =>
val sen2 = graph(word)
println(s"Word: $word, graph: ${graph.mkString(", ")}")
graph -= word
sen2.map { sentence =>
if(sentence == 1) 1L
else 0L + findOutput(sentence)
}
}.toSeq.flatten.sum
}
findOutput(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment