Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

"1+2*3" => "7" => 1(0),+(1),2(2),*(3),3(4)

val input = "1+2*3"//7


val updatedInput = input.toCharArray.toList

val result = getMathematicalResult(input.toCharArray)

def getOperationNumber(input:Char):Int = {

input match {
case '*' => 2
case '+' => 1
case i => 0.0
}
}

val operations = separateOperations(updatedInput)//List(+,*)


val sortedOperations = getSorted(operations, Nil)//List(*,+)
val getNumbers = separateNumbers(updatedInput, sortedOperations)// List(1,2,3)

def separateOperations(input:List[Char],op:List[Char]):List[Char] = {
input.map(i => if(i=='*' || i=='+') op:+i else op
}

def getSorted(input:List[Char], op:List[Char]):List[Char] = {

if(input.length==1 || input.isEmpty) op
else {
if(getOperationNumber(input.head) == 2) getSorted(input.tail, input.head:+op) else
getSorted(input.tail, op+:input.head)//
}
}

def separateNumbers(l1:List[Char],l2:List[Char]):List[Char] = {
l1 diff l2
}

def getMathematicalResult(input:List[Char], updatedData:List[Char]):String = {

input.map{ i =>

if(getOperationNumber(i)==2) i+:updatedData//List(*1+2)
else if(getOperationNumber(i)==1) updatedData:+i//List(1+)
else if(getOperationNumber(i)==0.0) updatedData:+i//List(1) //List(1+2)
//List(3*1+2)
}

You might also like