-
-
Save soursop/fef4c908758b1c047e60 to your computer and use it in GitHub Desktop.
scala File Fix it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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