# HG changeset patch # User Teemu Piippo # Date 1430695161 -10800 # Node ID 7316dc5f61efba1c9a4ba2e193c7d7524831b289 # Parent 06808909d69422156b14a51432c8ba1a00f10258 Added scientific notation and dice expressions (cheaply as operators) diff -r 06808909d694 -r 7316dc5f61ef calc.py --- a/calc.py Mon May 04 00:50:18 2015 +0300 +++ b/calc.py Mon May 04 02:19:21 2015 +0300 @@ -99,9 +99,22 @@ def intf (func): return lambda *args: do_intf (func, *args) +def dice (numRolls, maxValue): + sumValue = 0 + + for i in range (0, numRolls): + sumValue += int (random.random() * maxValue) + 1 + + return sumValue + +def scientific (mantissa, exp): + return mantissa * 10 ** exp + Operators = [] OperatorData = { - 'lneg': { 'symbol': '!', 'operands': 1, 'priority': 5, 'function': lambda x: not x }, + 'sci': { 'symbol': 'e', 'operands': 2, 'priority': 1, 'function': intf (scientific) }, + 'dice': { 'symbol': 'd', 'operands': 2, 'priority': 2, 'function': intf (dice) }, + 'not': { 'symbol': '!', 'operands': 1, 'priority': 5, 'function': lambda x: not x }, 'compl': { 'symbol': '~', 'operands': 1, 'priority': 5, 'function': intf (operator.inv) }, 'neg': { 'symbol': '-', 'operands': 1, 'priority': 5, 'function': lambda x: -x }, 'pow': { 'symbol': '**', 'operands': 2, 'priority': 10, 'function': lambda x, y: x ** y }, @@ -287,7 +300,7 @@ if base != 10: if not self.preferred_base: self.preferred_base = base - + # Skip leading zeroes while i < len (expr) and expr[i] == '0': i += 1