forked from dylansun/leetcode-cn-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo640.scala
38 lines (34 loc) · 1.13 KB
/
No640.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Created by lilisun on 8/30/19.
*/
object No640 {
object Solution {
case class Coeff(n:Int, bias:Int){
def +(that: Coeff):Coeff = Coeff(n + that.n, bias + that.bias)
def -(that: Coeff):Coeff = Coeff(n - that.n, bias - that.bias)
def get():String = this match {
case Coeff(0, 0) => "Infinite solutions"
case Coeff(0, _) => "No solution"
case _ => "x=" + (-bias / n)
}
}
def notOp(ch:Char):Boolean = ch != '+' && ch != '-'
def parser(str:String, acc:List[Coeff] = Nil):List[Coeff] = str match {
case "" => acc
case _ => parser(str.tail dropWhile notOp, trans(str.head + str.tail.takeWhile(notOp))::acc)
}
def trans(str:String):Coeff = {
if(str.contains('x')) Coeff(myInt(str.replace("x","")), 0)
else Coeff(0, str.toInt)
}
def myInt(str:String):Int = str match {
case "" => 1
case "-" => -1
case "+" => 1
case _ => str.toInt
}
def solveEquation(equation: String): String = equation split "=" match {
case Array(str1, str2) => (parser(str1).reduce(_+_) - parser(str2).reduce(_+_)).get()
}
}
}