-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: functions with zero or one argument or non-existing functions mi…
…ght error
- Loading branch information
Showing
3 changed files
with
63 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from collections.abc import Callable | ||
from math import factorial | ||
|
||
|
||
def fn_max(args: list[int]) -> int: | ||
return max(args) | ||
|
||
|
||
def fn_min(args: list[int]) -> int: | ||
return min(args) | ||
|
||
|
||
def fn_fac(args: list[int]) -> int: | ||
if args: | ||
return factorial(args[0]) | ||
return 1 | ||
|
||
|
||
def fn_comb(args: list[int]) -> int: | ||
if len(args) >= 2: | ||
return factorial(args[0]) // factorial(args[1]) // factorial(args[0] - args[1]) | ||
return 0 | ||
|
||
|
||
def fn_any(args: list[int]) -> int: | ||
return 1 if any(args) else 0 | ||
|
||
|
||
funcs: dict[str, Callable[[list[int]], int]] = { | ||
"max": fn_max, | ||
"min": fn_min, | ||
"fac": fn_fac, | ||
"comb": fn_comb, | ||
"any": fn_any, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rollbot.interpreter.calculator import evaluate, EvaluationError | ||
import pytest | ||
|
||
|
||
def test_func_arity(): | ||
evaluate("max(d20)") | ||
evaluate("max(d20, d20)") | ||
evaluate("min(d20)") | ||
evaluate("min(d20, d20)") | ||
evaluate("fac(5)") | ||
evaluate("fac(5, 5)") | ||
|
||
with pytest.raises(EvaluationError): | ||
evaluate("nosuchfun()") | ||
|
||
with pytest.raises(EvaluationError): | ||
evaluate("nosuchfun(1)") |