Sum machine!

Report a typo

Robot wants to emulate simple calculation of expressions: first, it defines variables, and then adds them up. With a limited amount of resources, Robot has a bunch of special conditions.

It can work with definitions like a=5,b=10 so that in one input line, each definition contains a string name of a variable, an equals sign, and an integer value. It is possible to define several variables with a comma as a separator. The calculated sum expression is defined in the next input string in the format a+b with the names of variables separated by a plus sign. It is allowed to use undefined variable names in the expression: in this case, the value of the variable will be 0.

Help Robot finish the program!

Sample Input 1:

a=1,b=2,c=3
a+b+c

Sample Output 1:

6
Write a program in Scala 3
object Summator extends App {
val inputDefinitions = scala.io.StdIn.readLine()
val inputExpression = scala.io.StdIn.readLine()

var variables = ???.empty[String, Int]
inputDefinitions
.split(',')
.filter(_.nonEmpty)
.map { ???.split('=') }
.map { array => variables += ??? }

println(inputExpression.split(???).map(??? => variables.getOrElse(???)).sum)
}
___

Create a free account to access the full topic