MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / parse_token

Function parse_token

data_structures/stacks/postfix_evaluation.py:40–61  ·  view source on GitHub ↗

Converts the given data to the appropriate number if it is indeed a number, else returns the data as it is with a False flag. This function also serves as a check of whether the input is a number or not. Parameters ---------- token: The data that needs to be converted to th

(token: str | float)

Source from the content-addressed store, hash-verified

38
39
40def parse_token(token: str | float) -> float | str:
41 """
42 Converts the given data to the appropriate number if it is indeed a number, else
43 returns the data as it is with a False flag. This function also serves as a check
44 of whether the input is a number or not.
45
46 Parameters
47 ----------
48 token: The data that needs to be converted to the appropriate operator or number.
49
50 Returns
51 -------
52 float or str
53 Returns a float if `token` is a number or a str if `token` is an operator
54 """
55 if token in OPERATORS:
56 return token
57 try:
58 return float(token)
59 except ValueError:
60 msg = f"{token} is neither a number nor a valid operator"
61 raise ValueError(msg)
62
63
64def evaluate(post_fix: list[str], verbose: bool = False) -> float:

Callers 1

evaluateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected