Advertisement
Andre1314

Untitled

Oct 10th, 2023
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.03 KB | Source Code | 0 0
  1. import scala.util.control.Breaks._
  2.  
  3. object Lab1 {
  4.   def main(args: Array[String]): Unit = {
  5.     println(balance("(()())".toList))
  6.   }
  7.  
  8.   def Balance(text: String, stack: String): Boolean = {
  9.     if (text.isEmpty){
  10.       return stack.isEmpty
  11.     }
  12.     else if (text.head == '('){
  13.       return Balance(text.tail, stack + text.head)
  14.     }
  15.     else if (text.head == ')'){
  16.       return stack.nonEmpty && stack.last == '(' && Balance(text.tail, stack.substring(0, stack.length - 1))
  17.     }
  18.  
  19.     return Balance(text.tail, stack)
  20.   }
  21.  
  22.   def CountChange(nominals: Array[Int], index: Int, num: Int): Int = {
  23.     if (num == 0) return 1
  24.     if(num < 0 || index <= 0) return 0
  25.  
  26.     val withoutNum = CountChange(nominals, index - 1, num)
  27.     val withNum = CountChange(nominals, index, num - nominals(index - 1))
  28.  
  29.     return withoutNum + withNum
  30.   }
  31.  
  32.   /**
  33.    * Трикутник Паскаля
  34.    */
  35.   def pascal(col: Int, row: Int): Int = {
  36.     if (col == 0 || col == row) return 1
  37.  
  38.     return pascal(col - 1, row - 1) + pascal(col, row - 1)
  39.   }
  40.  
  41.   /**
  42.    * Балансування дужок
  43.    */
  44. //  def balance(chars: List[Char]): Boolean = {
  45. //    if (chars.isEmpty) return true
  46. //    if (chars.length == 1) return false
  47. //    if (chars.head == ')') return false
  48. //
  49. //    val n = chars.length
  50. //
  51. //    var i = 1
  52. //    var count = 0
  53. //    breakable {
  54. //      while (i < n) {
  55. //        if (chars(i) == chars.head) {
  56. //          count += 1
  57. //        }
  58. //        if (chars(i) == ')') {
  59. //          if (count == 0) break
  60. //          count -= 1
  61. //        }
  62. //
  63. //        i += 1
  64. //      }
  65. //    }
  66. //
  67. //    if(i == n) return false
  68. //
  69. //    if(i == 2) return balance(chars.slice(i, n - 1))
  70. //
  71. //    return balance(chars.slice(1, i)) && balance(chars.slice(i + 1, n))
  72. //  }
  73. def balance(chars: List[Char]): Boolean = {
  74.   if (chars.isEmpty) return true
  75.  
  76.   return balanced(chars.slice())
  77. }
  78.  
  79.   /**
  80.    * Підрахунок решти
  81.    */
  82.   def countChange(money: Int, coins: List[Int]): Int = {
  83.     return 0
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement