1

I want to write a summation function, but can't figure out how I would parse the bottom expression and right expression.

def summation(count: int, bottom_var: str, espression: str):
    out = 0
    
    # parse bottom_var into a variable and it's value
    value = ···
    var_name = ···
    expression.replace(var_name, value)
    ···

I want you to be able to use the inputs the same way as in normal sigma notation, as in bottom_var is 'n=13', not '13'.

You enter an assignment for bottom_var, and an expression using the variable defined in bottom_var in expression.

Example:

summation(4, 'x=1', 'x+1')

(would return 14, as 2+3+4+5=14)

CATboardBETA
  • 418
  • 6
  • 29

3 Answers3

1

This is how i did it:

def summation(count,bottom_var,expression):
    begin = False
    x = ""
    
    v = ""
    
    for char in bottom_var:
        if begin:
            x += char
    
        if char == "=":
            begin = True
    
        if begin == False:
            v += char
    
    x = int(x)
    
    expression = expression.replace(v,str("x"))
    
    print(expression)
    
    for n in range(count):
        x = eval(expression)
    
summation(4,"d=152",'d+145*2')
Eimantas G
  • 437
  • 3
  • 7
1

First, parse the bottom_var to get the symbol and starting value:

var, value = bottom_var.split('=')
var = var.strip()
value = eval(value)

Using split we get the two parts of the equal sign easily. Using strip we even allow any number of spaces, like x = 1.

Then, we want to create a loop from the starting value to count:

for i in range(value, count+1):
    ...

Lastly, we want to use the loop to sum the expression when each time the symbol is replaced with the current iteration's value. All in all:

def summation(count: int, bottom_var: str, expression: str):
    var, value = bottom_var.split('=')
    var = var.strip()
    value = eval(value)
    res = 0
    for i in range(value, count+1):
        res += eval(expression.replace(var, str(i)))

    return res

For example:

>>> summation(4, 'x=1', 'x+1')
14

Proposing the code in this answer, I feel the need to ask you to read about Why is using 'eval' a bad practice? and please make sure that it is OK for your application. Notice that depending on the context of the use of your code, using eval can be quite dangerous and lead to bad outcomes.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • This nearly works properly, but does not support a more complex variable definition. The only method I know of to have this work is to replace `value = int(value)` with `value = int(eval(value))`, so I did so. It also does not allow floating point numbers, as they are being converted to an integer, but I can't see that being fixable with your method. – CATboardBETA Jan 10 '21 at 16:30
  • Sounds good, but do you mean something like `x=2z`? That is not "evaluateable". Maybe `x=2*z`. And still `z` has to be an integer and the result needs to be smaller than the `count` – Tomerikoo Jan 10 '21 at 16:32
  • I don't think floats are acceptable with sigma notation... – Tomerikoo Jan 10 '21 at 16:33
  • I fixed the first thing you mentioned (possible now to do `x=2*z`), but as to your second problem it's not applicable. The bottom and top values have to be integers. They represent an index... https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation – Tomerikoo Jan 10 '21 at 16:38
  • I realize this is quite old now, but I am having another issue, which may require for a change. I need the `eval()` statement to return type `decimal.Decimal`. How might this be done? – CATboardBETA Jan 30 '21 at 01:13
-1

There are some built-in function in python which execute code from text or str which are exec() and eval() function.

for example :

>>> exec('n=13')
>>> print(n)
13
>>> eval('n+1')
14

you can use this in your code.

Cdaman
  • 193
  • 9