Python - without using recursion or regex


  • 0
    B

    ugly solution, but works

    import string, operator
    class Solution(object):
        def is_number(self,s):
            try:
                float(s)
                return True
            except ValueError:
                pass
    
            try:
                import unicodedata
                unicodedata.numeric(s)
                return True
            except (TypeError, ValueError):
                pass
    
            return False
    
        def countOfAtoms(self, formula):
            """
            :type formula: str
            :rtype: str
            """
            caps = set(list(string.ascii_uppercase))
            lowers = set(list(string.ascii_lowercase))
    
            brackets = []
            all = {}
            st = None
            i = 0
            bracks = 0
            while i < len(formula):
                if formula[i] == ')':
                    bracks -= 1
                    i += 1
                    ct = ''
                    while i < len(formula) and self.is_number(formula[i]):
                        ct += formula[i]
                        i += 1
    
                    ct_num = 1
                    if len(ct) > 0:
                        ct_num = int(ct)
    
                    last_chrs = brackets[-1]
                    for each_c in last_chrs:
                        last_chrs[each_c] = last_chrs[each_c]*ct_num
    
                    brackets = brackets[:-1]
                    if len(brackets) == 0:
                        for each_c in last_chrs:
                            if each_c in all:
                                all[each_c] += last_chrs[each_c]
                            else:
                                all[each_c] = last_chrs[each_c]
                    else:
                        bracks_last = brackets[-1]
                        for each_c in last_chrs:
                            if each_c in bracks_last:
                                bracks_last[each_c] += last_chrs[each_c]
                            else:
                                bracks_last[each_c] = last_chrs[each_c]
    
                elif formula[i] == '(':
                    bracks += 1
                    i += 1
                    brackets.append({})
                else:
                    cur_ch = formula[i]
                    i += 1
                    while i < len(formula) and formula[i] in lowers:
                        cur_ch += formula[i]
                        i += 1
                    ct = ''
                    while i < len(formula) and self.is_number(formula[i]):
                        ct += formula[i]
                        i += 1
                    ct_num = 1
                    if len(ct) > 0:
                        ct_num = int(ct)
    
                    if len(brackets) > 0:
                        if cur_ch in brackets[-1]:
                            brackets[-1][cur_ch] += ct_num
                        else:
                            brackets[-1][cur_ch] = ct_num
                    else:
                        if cur_ch in all:
                            all[cur_ch] += ct_num
                        else:
                            all[cur_ch] = ct_num
            ct = all
            sorted_x = sorted(ct.items(), key=operator.itemgetter(0))
            ot = ''
    
            for i in sorted_x:
                ot += i[0]
                if i[1] != 1:
                    ot += str(i[1])
            #print all
            return ot
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.