Evaluate dice rolling notation strings

前端 未结 14 1627
甜味超标
甜味超标 2021-01-30 14:55

Rules

Write a function that accepts string as a parameter, returning evaluated value of expression in dice notation, including addition and multiplication.

To

14条回答
  •  耶瑟儿~
    2021-01-30 15:20

    PHP, 147 symbols, no eval:

    preg_match('/(\d+)?d(\d+)[\s+]?([\+\*])?[\s+]?(\d+)?/',$i,$a);$d=rand(1,$a[2])*((!$a[1])?1:$a[1]);$e['+']=$d+$a[4];$e['*']=$d*$a[4];print$e[$a[3]];
    

    $i contains input string.

    Edit: oops, forgot about prefixed operation. brb.

提交回复
热议问题