-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
37 lines (25 loc) · 852 Bytes
/
lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import re
from collections import Counter
#my lamda function
def tokenize_mol(molecule):
return re.findall('[A-Z][a-z]?|\d+|.', molecule)
def lambda_handler(event, context):
def tokenize_mol(molecule):
return re.findall('[A-Z][a-z]?|\d+|.', molecule)
my_mol=event['molecule']
composition = [[]]
for token in tokenize_mol(my_mol):
if token.isalpha():
last = [token]
composition[-1].append(token)
elif token.isdecimal():
count=int(token)
composition[-1].extend(last*(count-1))
elif token in '([{':
composition.append([])
elif token in ')]}':
last = composition.pop()
composition[-1].extend(last)
my_dict = dict(Counter(composition[-1]))
return (my_dict)