pytudes._2021.coderbyte.math_challenge__hard

https://coderbyte.com/information/Reverse%20Polish%20Notation

Module Contents

Functions

MathChallenge(rpn_expression)

Returns the value of an arithmetic expression written in Reverse Polish notation

MathChallenge_cases(rpn_expression)

Returns the value of an arithmetic expression written in Reverse Polish notation

pytudes._2021.coderbyte.math_challenge__hard.MathChallenge(rpn_expression)[source]

Returns the value of an arithmetic expression written in Reverse Polish notation

Args:
rpn_expression:

The input string containing the Reverse Polish notation expression which to evaluate. rpn_expression is composed of only integers and the operators: +,-,* and /.

Examples:
>>> MathChallenge("1 1 + 1 + 1 +")
4
>>> # ((1 + 1) + 1) + 1
>>> # = (2 + 1) + 1
>>> # = 3 + 1
>>> # = 4
>>> MathChallenge("4 5 + 2 1 + *")
27
>>> # (4 + 5) * (2 + 1)
>>> # = 9 * (2 + 1)
>>> # = 9 * 3
>>> # = 27
>>> MathChallenge("2 12 + 7 / ")
2
>>> # (2 + 12) / 7
>>> # = 14 / 7
>>> # = 2
>>> MathChallenge("1 1 - 1 + 1 +")
2
>>> # ((1 - 1) + 1) + 1
>>> # = (0 + 1) + 1
>>> # = 1 + 1
>>> # = 2
>>> MathChallenge("1 1 - +")
Traceback (most recent call last):
...
ValueError: Malformed RPN arithmetic expression: 1 1 - +; Stack had < 2 elements (≥2 expected)
Parameters:

rpn_expression (str) –

Return type:

int

pytudes._2021.coderbyte.math_challenge__hard.MathChallenge_cases(rpn_expression)[source]

Returns the value of an arithmetic expression written in Reverse Polish notation

Args:
rpn_expression:

The input string containing the Reverse Polish notation expression which to evaluate. rpn_expression is composed of only integers and the operators: +,-,* and /.

Examples:
>>> MathChallenge_cases("1 1 + 1 + 1 +")
4
>>> # ((1 + 1) + 1) + 1
>>> # = (2 + 1) + 1
>>> # = 3 + 1
>>> # = 4
>>> MathChallenge_cases("4 5 + 2 1 + *")
27
>>> # (4 + 5) * (2 + 1)
>>> # = 9 * (2 + 1)
>>> # = 9 * 3
>>> # = 27
>>> MathChallenge_cases("2 12 + 7 / ")
2
>>> # (2 + 12) / 7
>>> # = 14 / 7
>>> # = 2
>>> MathChallenge_cases("1 1 - 1 + 1 +")
2
>>> # ((1 - 1) + 1) + 1
>>> # = (0 + 1) + 1
>>> # = 1 + 1
>>> # = 2
>>> MathChallenge_cases("1 1 - +")
Traceback (most recent call last):
...
ValueError: Malformed RPN arithmetic expression: 1 1 - +; Stack had < 2 elements (≥2 expected)
Parameters:

rpn_expression (str) –

Return type:

int