Problem:
ICPC Calculator In mathematics, we usually specify the order of operations by using
parentheses.
For example, 7 × (3 + 2) always means multiplying 7 by the result of 3 + 2 and never means adding 2 to the result of 7 × 3.

However, there are people who do not like parentheses.
International Counter of Parentheses Council (ICPC) is attempting
to make a notation without parentheses the world standard.
They are always making studies of such no-parentheses notations. Dr. Tsukuba, a member of ICPC, invented a new parenthesis-free notation.
In his notation, a single expression is represented by multiple
lines, each of which contains an addition operator ( + ), a
multiplication operator ( * ) or an integer.
An expression is either a single integer or an operator application to operands. Integers are denoted in decimal notation in one line.
An operator application is denoted by a line of its operator immediately
followed by lines denoting its two or more operands,
each of which is an expression, recursively.
Note that when an operand is an operator application,
it comprises multiple lines. As expressions may be arbitrarily nested,
we have to make it clear which operator is applied to which operands.
For that purpose, each of the expressions is given its nesting level. The top level expression has the nesting level of 0.
When an expression of level n is an operator application,
its operands are expressions of level n + 1.
The first line of an expression starts with a sequence of periods ( . ),
the number of which indicates the level of the expression. For example, 2 + 3 in the regular mathematics is denoted as in Figure 1.
An operator can be applied to two or more operands.
Operators + and * represent operations
of summation of all operands and multiplication of all operands,
respectively.  For example, Figure 2 shows an expression multiplying
2, 3, and 4.
For a more complicated example, an expression (2 + 3 + 4) × 5 in
the regular mathematics can be expressed as in Figure 3 while (2 + 3) × 4 × 5
can be expressed as in Figure 4. +
.2
.3 *
.2
.3
.4 *
.+
..2
..3
..4
.5 *
.+
..2
..3
.4
.5 Your job is to write a program that computes the value of expressions
  written in Dr. Tsukuba's notation to help him. Input The input consists of multiple datasets.  Each dataset
  starts with a line containing a positive integer n , followed by n lines denoting a single expression in Dr. Tsukuba's notation. You may assume that, in the expressions given in the input,
  every integer comprises a single digit and that
  every expression has no more than nine integers.
  You may also assume that all the input expressions are valid
  in the Dr. Tsukuba's notation.
  The input contains no extra characters, such as spaces or empty lines. The last dataset is immediately followed by a line with a single zero. Output For each dataset, output a single line containing an integer
which is the value of the given expression. Sample Input 1
9
4
+
.1
.2
.3
9
+
.0
.+
..*
...1
...*
....1
....2
..0
10
+
.+
..6
..2
.+
..1
..*
...7
...6
.3
0 Output for the Sample Input 9
6
2
54

Solution (Python):
while True:
  n = int(input())
  if n==0:
    break
  if n==1:
    print(int(input()))
    continue

  op = "+*"
  f = [input() for i in range(n)]
  f = f[::-1]
  stack = []

  for i in range(n):
    if f[i][-1] not in op:
      stack.append(f[i])
    else:
      nest = f[i].count(".")
      if f[i][-1]=="*":
        cur = 1
        while len(stack):
          if stack[-1].count(".")!=nest+1:
            break
          else:
            t = stack.pop().strip(".")
            cur *= int(t)
        stack.append("."*(nest)+str(cur))
      else:
        cur = 0
        while len(stack):
          if stack[-1].count(".")!=nest+1:
            break
          else:
            t = stack.pop().strip(".")
            cur += int(t)
        stack.append("."*(nest)+str(cur))
  print(stack[0])

Solution (Python):
for n in iter(input, '0'):
    st = ''
    prv_lv = 0
    ops = ['']
    for _ in range(int(n)):
        line = input()
        lv = line.count('.')
        op = line[lv:]
        while prv_lv > lv:
            ops.pop()
            st += ')'
            prv_lv -= 1
        if op in '*+':
            st += ops[-1]
            st += '(' + str(int(op == '*'))
            ops.append(op)
        else:
            st += ops[-1] + op
        prv_lv = lv
    while len(ops) > 1:
        ops.pop()
        st += ')'
    print(eval(st))

Solution (Python):
from functools import reduce
import operator

while True:
  n = int(input())
  if n == 0:break
  def fix(x):
    if x[-1] in ("+", "*"):
      return (x.count("."), x[-1])
    else:
      return (x.count("."), int(x[-1]))

  lst = [fix(input()) for _ in range(n)]
  while True:
    if len(lst) == 1:
      print(lst[0][1])
      break
    
    max_depth = -1
    index = 100
    op = "-"
    for i, t in enumerate(lst):
      d, v = t
      if d > max_depth and v in ("+", "*"):
        max_depth = d
        index = i
        op = v

    pop_lst = []
    acc_lst = []
    i = index + 1
    while i < len(lst) and lst[i][0] == max_depth + 1:
      pop_lst.append(i)
      acc_lst.append(lst[i][1])
      i += 1
    if op == "+":
      new_value = reduce(operator.add, acc_lst)
    else:
      new_value = reduce(operator.mul, acc_lst)

    for i in reversed(pop_lst):
      lst.pop(i)
    lst.pop(index)
    lst.insert(index, (max_depth, new_value))

Solution (Python):
import functools
import operator

def calc(exps, level):
    lst = []
    while exps:
        exp = exps[0]
        nlevel = len(exp)-1

        if nlevel < level:
            break
        else:
            val = exps.pop(0)[-1]
            if val in ('+', '*'):
                operand = calc(exps, level+1)
                if val == '+':
                    val = sum(operand)
                else:
                    val = functools.reduce(operator.mul, operand)

            elif '0' <= val <= '9':
                val = int(val)

            lst.insert(0, val)

    return lst

def main():
    while True:
        n = int(input().strip())
        if n == 0:
            break
        exps = [input().strip() for _ in range(n)]
        print(calc(exps, 0)[0])

if __name__ == '__main__':
    main()

Solution (Python):

from functools import reduce
def check(dt,st,lv):
    global p
    if p>=len(dt):
        return st
        
    if not dt[p][-1].isdigit() and lv==len(dt[p]):
        tmp=p
        p+=1
        st.append(reduce(lambda a,b:a+b if dt[tmp][-1]=='+' else a*b,check(dt,[],lv+1)))
        return check(dt,st,lv)

    elif lv==len(dt[p]):
        st.append(int(dt[p][-1]))
        p+=1
        return check(dt,st,lv)
    else:
        return st

    
while 1:
    n=int(input())
    if n==0:
        break
    data=[]
    for i in range(n):
        data.append(list(input()))
    p=0
    print(check(data,[],1)[0])