Skip to content

Instantly share code, notes, and snippets.

@soursop
Last active August 29, 2015 14:11
Show Gist options
  • Save soursop/fef4c908758b1c047e60 to your computer and use it in GitHub Desktop.
Save soursop/fef4c908758b1c047e60 to your computer and use it in GitHub Desktop.
scala File Fix it
package folder
object FileFixItTest {
def main(args: Array[String]) {
val os = OS(List(Dir("/c")))
assert(os.mkdir(Dir("/a/b")) == 2)
assert(os.mkdir(Dir("/e/b/c/d")) == 4)
}
}
case class OS(input: List[Dir]) {
var exist = input.map(_.dirs).toSet
def mkdir(dir: Dir): Int = {
(dir.dirs.size until 0 by -1).foreach{
i =>
if (exist.contains(dir.dirs.take(i))) {
exist += dir.dirs
return dir.dirs.size - i
}
println("not exits %s".format(dir.dirs.take(i)))
}
exist += dir.dirs
return dir.dirs.size
}
}
case class Dir(dir: String) {
val dirs = dir.tail.split("/").toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment