Skip to content

Commit

Permalink
feat: implement math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jayachithra committed Nov 24, 2023
1 parent 7a4fd76 commit 0d3047d
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
import random
from typing import Union


def div(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
return a / b
def add(summand_1: Union[int, float], summand_2: Union[int, float]) -> Union[int, float]:
"""Return the result from adding two numbers."""
return summand_1 + summand_2


def sub(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
return a - b
def div(dividend: Union[int, float], divisor: Union[int, float]) -> Union[int, float]:
"""Return the integer result from dividing two numbers. If both the inputs are integers, the result will be an integer."""
if isinstance(dividend, int) and isinstance(divisor, int):
return dividend // divisor
return dividend / divisor


def max_(arg1: Union[list, int, float], *args: Union[int, float]) -> Union[int, float]:
"""Return the highest value from an array with numbers that is inclusive at both ends."""
if isinstance(arg1, list):
return max(arg1)
if len(args) == 0:
return arg1
return max(arg1, *args)


def min_(arg1: Union[list, int, float], *args: Union[int, float]) -> Union[int, float]:
"""Return the lowest value from a set of numbers or an array."""
if isinstance(arg1, list):
return min(arg1)
if len(args) == 0:
return arg1
return min(arg1, *args)


def mod(dividend: Union[int, float], divisor: Union[int, float]) -> Union[int, float]:
"""Return the remainder from dividing two numbers."""
return dividend % divisor


def mul(multiplicand1: Union[int, float], multiplicand2: Union[int, float]) -> Union[int, float]:
"""Return the product from multiplying two numbers."""
return multiplicand1 * multiplicand2


def rand(min_value: int, max_value: int) -> int:
"""Return a random integer from a specified range, which is inclusive only at the starting end."""
return random.randint(min_value, max_value - 1)


def range_(start_index: int, count: int) -> list:
"""Return an integer array that starts from a specified integer."""
return list(range(start_index, start_index + count))


def sub(minuend: Union[int, float], subtrahend: Union[int, float]) -> Union[int, float]:
"""Return the result from subtracting the second number from the first number."""
return minuend - subtrahend
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class FunctionsRepository:
functions: Dict[str, Callable] = {
"add": None,
"add": math_functions.add,
"addDays": date_functions.add_days,
"addHours": date_functions.add_hours,
"addMinutes": date_functions.add_minutes,
Expand Down Expand Up @@ -63,14 +63,14 @@ class FunctionsRepository:
"length": collection_functions.length,
"less": logical_functions.less,
"lessOrEquals": logical_functions.less_or_equals,
"max": None,
"min": None,
"mod": None,
"mul": None,
"max": math_functions.max_,
"min": math_functions.min_,
"mod": math_functions.mod,
"mul": math_functions.mul,
"not": logical_functions.not_,
"or": logical_functions.or_,
"rand": None,
"range": None,
"rand": math_functions.rand,
"range": math_functions.range_,
"replace": string_functions.replace,
"skip": collection_functions.skip,
"split": string_functions.split,
Expand Down
Loading

0 comments on commit 0d3047d

Please sign in to comment.