diff --git a/spoj/MMASS.py b/spoj/MMASS.py new file mode 100644 index 0000000..9f9569a --- /dev/null +++ b/spoj/MMASS.py @@ -0,0 +1,21 @@ +formula = input() +MMASS = {'H': 1, 'C': 12, 'O': 16} +nums = [str(i) for i in range(2, 10)] +stack = [] + +for i in range(len(formula)): + if formula[i] in ['H', 'C', 'O']: + stack.append(MMASS[formula[i]]) + elif formula[i] == '(': + stack.append('(') + elif formula[i] in nums: + item = stack.pop() + stack.append(item*int(formula[i])) + elif formula[i] == ')': + item = stack.pop() + ans = 0 + while(item != '('): + ans += item + item = stack.pop() + stack.append(ans) +print(sum(stack)) diff --git a/spoj/README.md b/spoj/README.md index 3bfc61e..5b449fe 100644 --- a/spoj/README.md +++ b/spoj/README.md @@ -19,6 +19,7 @@ Name | Status | Tags | Languages [FCTRL](FCTRL.py) | :heavy_check_mark: | `#math` | Python [FCTRL2](FCTRL2.py) | :heavy_check_mark: | `#math` `#big-numbers` | Python [JNEXT](JNEXT.py) | :heavy_check_mark: | `#ad-hoc-1` | Python +[MMASS](MMASS.py) | :heavy_check_mark: | `#ad-hoc-1` | Python [NSTEPS](NSTEPS.py) | :heavy_check_mark: | `#ad-hoc-1` | Python [ONP](ONP.py) | :heavy_check_mark: | `#stack` | Python [PRIME1](PRIME1.py) | :rocket: | `#number-theory` | Python