yuin #4735(2007/12/10 18:43 GMT) [ Scala ] Rating0/0=0.00
パーサコンビネータでパーサを作ってナイーブに。 解答は101個です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import scala.util.parsing.combinator.{Parsers, ImplicitConversions, ~, mkTilde} import scala.util.parsing.combinator.syntactical.StdTokenParsers import scala.util.parsing.combinator.lexical.StdLexical object Komachi extends StdTokenParsers with Application{ type Tokens = StdLexical ; val lexical = new StdLexical lexical.delimiters ++= List("+","-", "*", "/") def expr = term*("+" ^^ {(x: double, y: double) => x + y} | "-" ^^ {(x: double, y: double) => x - y}) def term = factor*("*" ^^ {(x: double, y: double) => x * y} | "/" ^^ {(x: double, y: double) => x / y}) def factor: Parser[double] = numericLit ^^ (_.toDouble) ((List(List[String]())) /: List.make(8, List("+", "-", "*", "/", ""))){ for(i <-_; j <-_) yield j::i }.foreach{fs => val s = (new StringBuilder("1") /: (2 to 9)){(s,i) => s.append(fs(i-2)).append(i) }.toString if(expr(new lexical.Scanner(s)).get == 100.0) println(s) } }
Rating0/0=0.00-0+
[ reply ]
yuin
#4735()
[
Scala
]
Rating0/0=0.00
パーサコンビネータでパーサを作ってナイーブに。 解答は101個です。
import scala.util.parsing.combinator.{Parsers, ImplicitConversions, ~, mkTilde} import scala.util.parsing.combinator.syntactical.StdTokenParsers import scala.util.parsing.combinator.lexical.StdLexical object Komachi extends StdTokenParsers with Application{ type Tokens = StdLexical ; val lexical = new StdLexical lexical.delimiters ++= List("+","-", "*", "/") def expr = term*("+" ^^ {(x: double, y: double) => x + y} | "-" ^^ {(x: double, y: double) => x - y}) def term = factor*("*" ^^ {(x: double, y: double) => x * y} | "/" ^^ {(x: double, y: double) => x / y}) def factor: Parser[double] = numericLit ^^ (_.toDouble) ((List(List[String]())) /: List.make(8, List("+", "-", "*", "/", ""))){ for(i <-_; j <-_) yield j::i }.foreach{fs => val s = (new StringBuilder("1") /: (2 to 9)){(s,i) => s.append(fs(i-2)).append(i) }.toString if(expr(new lexical.Scanner(s)).get == 100.0) println(s) } }Rating0/0=0.00-0+
[ reply ]