Skip to content

Commit

Permalink
Added MMASS
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet29 committed Jun 9, 2020
1 parent 1a032de commit e492902
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spoj/MMASS.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions spoj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e492902

Please sign in to comment.