Sha256: fbef67ef8078ef0c9a85c006e6dfb75fea7884311977baf918b3c725cfea764e

Contents?: true

Size: 1.88 KB

Versions: 148

Compression:

Stored size: 1.88 KB

Contents

import scala.math.pow
import scala.util.parsing.combinator.RegexParsers

sealed abstract class Expression {
  def evaluate(): Integer
}

case class Const(value: Int) extends Expression {
  def evaluate() = value
}

case class Sum(e1: Expression, e2: Expression) extends Expression {
  def evaluate(): Integer = e1.evaluate() + e2.evaluate()
}

case class Diff(e1: Expression, e2: Expression) extends Expression {
  def evaluate(): Integer = e1.evaluate() - e2.evaluate()
}

case class Mult(e1: Expression, e2: Expression) extends Expression {
  def evaluate(): Integer = e1.evaluate() * e2.evaluate()
}

case class Div(e1: Expression, e2: Expression) extends Expression {
  def evaluate(): Integer = e1.evaluate() / e2.evaluate()
}

case class Pow(e1: Expression, e2: Expression) extends Expression {
  def evaluate(): Integer = pow(e1.evaluate().toDouble, e2.evaluate().toDouble).toInt
}

object Wordy extends RegexParsers {
  private lazy val prefix = "What is "
  private lazy val questionMark = "?"
  private lazy val operators = "plus" | "minus" | "multiplied by" |
    "divided by" | "raised to the"

  private def operation = expr ~ rep(operators ~ expr) ^^ {
    case op ~ list => list.foldLeft(op) {
      case (x, "plus" ~ y) => Sum(x, y)
      case (x, "minus" ~ y) => Diff(x, y)
      case (x, "multiplied by" ~ y) => Mult(x, y)
      case (x, "divided by" ~ y) => Div(x, y)
      case (x, "raised to the" ~ y) => Pow(x, y)
    }
  }

  private def operand: Parser[Const] = """-?\d+""".r ^^ {s => Const(s.toInt)}
  private def expr: Parser[Expression] = operand | operation

  // Top level parser
  private def questionParser = prefix ~> operation <~ questionMark

  def parse(str: String): ParseResult[Expression] = parseAll(questionParser, str)

  def answer(question: String): Option[Int] = parse(question) match {
    case Success(result: Expression, _) => Some(result.evaluate())
    case _ => None
  }
}

Version data entries

148 entries across 148 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.179 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.178 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.177 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.176 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.175 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.174 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.173 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.172 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.171 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.170 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.169 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.167 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.166 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.165 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.164 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.163 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.162 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.161 tracks/scala/exercises/wordy/example.scala
trackler-2.2.1.160 tracks/scala/exercises/wordy/example.scala