Advertisement
Guest User

Untitled

a guest
Sep 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.72 KB | None | 0 0
  1. package made
  2.  
  3. object Task4 extends App {
  4.  
  5.   import scala.io.StdIn.{readInt, readLine}
  6.   import scala.collection.mutable.ListBuffer
  7.  
  8.   class Node() {
  9.     var name: String = ""
  10.     var children: ListBuffer[Node] = new ListBuffer[Node]()
  11.     var increment: Int = 0
  12.  
  13.     def this(name: String) {
  14.       this()
  15.       this.name = name
  16.     }
  17.  
  18.     @Override
  19.     override def toString(): String = {
  20.       name + ": " + children.toString
  21.     }
  22.  
  23.     def add(path: String) = {
  24.       val splitPath = path.split("/")
  25.       addNode(splitPath, 1, this)
  26.     }
  27.  
  28.     def addNode(splitPath: Array[String], index: Int, node: Node): Unit = {
  29.       var nodeAdded = false
  30.  
  31.       for (child <- node.children) {
  32.         if (child.name == splitPath(index)) {
  33.           addNode(splitPath, index + 1, child)
  34.           nodeAdded = true
  35.         }
  36.       }
  37.  
  38.       if (!nodeAdded) {
  39.         var addingNode = new Node(splitPath(index))
  40.         node.children += addingNode
  41.         if (index != splitPath.length - 1) {
  42.           addNode(splitPath, index + 1, addingNode)
  43.         }
  44.       }
  45.     }
  46.  
  47.     def calc: Int = {
  48.       deleteFile(this)
  49.  
  50.       increment
  51.     }
  52.  
  53.     def deleteFile(node: Node): Unit = {
  54.       if (node.children.nonEmpty) {
  55.         increment += 1
  56.         for (child <- node.children) {
  57.           if (child.children.nonEmpty) {
  58.             deleteFile(child)
  59.             increment += 1
  60.           }
  61.         }
  62.       }
  63.     }
  64.  
  65.   }
  66.  
  67. //  val n = readInt
  68. //  var paths = List[String]()
  69. //  for (_ <- 0 until n) {
  70. //    paths = readLine :: paths
  71. //  }
  72.   val n = 2
  73.   val paths = List[String]("/out/qq/ww", "/out/qq/ee")
  74.  
  75.   val test = new Node()
  76.   for (path <- paths) {
  77.     test.add(path)
  78.   }
  79.  
  80.   println(test.calc)
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement