Skip to content

Commit

Permalink
Added variable
Browse files Browse the repository at this point in the history
  • Loading branch information
charon25 committed Mar 9, 2023
1 parent d55ac7e commit b4f6045
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ additional_functions = {
}
```

The `sy.compute` (and `sy.shuting_yard`) also have a `case_sensitive` parameter (bool, default is `True`).
The `sy.compute` (and `sy.shuting_yard`) also have extra parameters :
- `case_sensitive` (bool, defaults to `True`) : if `True`, will consider `sin` and `SIN` different functions.
- `variable` (str, optional) : if defined, will consider any token matching it as a number. This is useful in expression such as `min(x, 1)` to get `x` to behave as a number.

## Additional features

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="shunting-yard",
version="1.0.4",
version="1.0.5",
author="Paul 'charon25' Kern",
description="Compute any math expression",
long_description=long_description,
Expand All @@ -14,5 +14,5 @@
url="https://www.github.com/charon25/ShuntingYard",
license="MIT",
packages=['shunting_yard'],
download_url="https://github.com/charon25/ShuntingYard/archive/refs/tags/v1.0.4.tar.gz"
download_url="https://github.com/charon25/ShuntingYard/archive/refs/tags/v1.0.5.tar.gz"
)
6 changes: 4 additions & 2 deletions shunting_yard/shunting_yard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from string import ascii_lowercase
import math
from enum import Enum
from typing import Optional

from shunting_yard.tokenize import tokenize
from shunting_yard.constants import BASE_OPERATORS, NUMBER_CHARS, FUNCTION_CHARS, UNARY_OPERATORS_SYMBOLS
Expand Down Expand Up @@ -42,7 +43,7 @@ class Associativity(Enum):


# Reference : https://en.wikipedia.org/wiki/Shunting_yard_algorithm
def shunting_yard(expression: str, case_sensitive: bool = True) -> str:
def shunting_yard(expression: str, case_sensitive: bool = True, variable: Optional[str] = None) -> str:
"""Convert the given classical math expression into Reverse Polish Notation using the Shunting-yard algorithm (see https://en.wikipedia.org/wiki/Shunting_yard_algorithm for more details). All whitespace are ignored.
Expand All @@ -56,6 +57,7 @@ def shunting_yard(expression: str, case_sensitive: bool = True) -> str:
Args:
expression (str): string containing the mathematical expression to convert.
case_sensitive (bool): indicates whether the expression should care about case.
variable (str, optional): if defined, will treat every token matching the variable as a number.
Raises:
MismatchedBracketsError: raised if the bracket are unbalanced.
Expand All @@ -73,7 +75,7 @@ def shunting_yard(expression: str, case_sensitive: bool = True) -> str:
for token in tokenize(expression):
first_char = token[0]

if first_char in NUMBER_CHARS:
if first_char in NUMBER_CHARS or token == variable:
output.append(token)
if len(operator_stack) > 0 and operator_stack[-1] in UNARY_OPERATORS_SYMBOLS:
output.append(operator_stack.pop())
Expand Down
7 changes: 7 additions & 0 deletions tests/test_shunting_yard.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_unary_in_function(self):
self.assertEqual(shunting_yard('min(1, -(2+3))'), '1 2 3 + -u min')
self.assertEqual(shunting_yard('min(1, -min(3, 4))'), '1 3 4 min -u min')

def test_variable(self):
self.assertEqual(shunting_yard('min(x, 1)'), '1 x min')
self.assertEqual(shunting_yard('min(x, 1)', variable='x'), 'x 1 min')
self.assertEqual(shunting_yard('min(1, x)', variable='x'), '1 x min')
self.assertEqual(shunting_yard('min(ab, 1)', variable='ab'), 'ab 1 min')
self.assertEqual(shunting_yard('min(1, x, 2)', variable='x'), '1 x 2 min')


if __name__ == '__main__':
unittest.main()

0 comments on commit b4f6045

Please sign in to comment.