Skip to content

Instantly share code, notes, and snippets.

@eranation
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save eranation/c039ce9e22e04db18d14 to your computer and use it in GitHub Desktop.

Select an option

Save eranation/c039ce9e22e04db18d14 to your computer and use it in GitHub Desktop.

Revisions

  1. eranation revised this gist May 8, 2015. 2 changed files with 4 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion iterative.scala
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    import javax.script.ScriptEngineManager

    val operators:Seq[String] = Seq("+","-","")
    // I *could* write an evaluation engine by hand, since it's only + and - it would be dead easy, but... not this time...
    val e = new ScriptEngineManager().getEngineByName("javascript")
    val operators:Seq[String] = Seq("+","-","")

    val choose1 = for {
    c1 <- operators
    3 changes: 3 additions & 0 deletions recursive.scala
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    import javax.script.ScriptEngineManager

    // I *could* write an evaluation engine by hand, since it's only + and - it would be dead easy, but... not this time...
    val e = new ScriptEngineManager().getEngineByName("javascript")
    val operators:Seq[String] = Seq("+","-","")

    def operatorPermutations(position:Int):Seq[String] = {
  2. eranation created this gist May 8, 2015.
    21 changes: 21 additions & 0 deletions iterative.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import javax.script.ScriptEngineManager

    val operators:Seq[String] = Seq("+","-","")
    // I *could* write an evaluation engine by hand, since it's only + and - it would be dead easy, but... not this time...
    val e = new ScriptEngineManager().getEngineByName("javascript")

    val choose1 = for {
    c1 <- operators
    c2 <- operators
    c3 <- operators
    c4 <- operators
    c5 <- operators
    c6 <- operators
    c7 <- operators
    c8 <- operators
    stringRep = s"1${c1}2${c2}3${c3}4${c4}5${c5}6${c6}7${c7}8${c8}9"
    result = e.eval(stringRep).asInstanceOf[Double].toInt
    if result == 100
    } yield {
    stringRep
    }
    25 changes: 25 additions & 0 deletions recursive.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    import javax.script.ScriptEngineManager
    val operators:Seq[String] = Seq("+","-","")

    def operatorPermutations(position:Int):Seq[String] = {
    if (position > 1) {
    for {
    curOp <- operators
    choose1 = operatorPermutations(position-1)
    choice<-choose1
    strRes = choice + curOp + position.toString
    } yield {
    strRes
    }
    } else {
    Seq("1")
    }
    }

    for {
    option <- operatorPermutations(9)
    result = e.eval(option).asInstanceOf[Double].toInt
    if result == 100
    } yield {
    option
    }