Morgan and a String

  • + 0 comments

    I could pass them all use the following. I have a helper function called fast_get that one will return '[' which is the next ascii charecter after Z in case any of the stacks reached end of index. This way I can make sure even if I get out of bond ot the stacks the loop still works fine.

    `python def fast_get(seq, i, default='['): return seq[i] if 0 <= i < len(seq) else default

    def morganAndString(a, b): # Write your code here a = deque(a) b= deque(b) result="" while a or b: if a and b: i=0

            while fast_get(a,i)==fast_get(b,i) and (i<len(a) or i< len(b)):
                i+=1
                if fast_get(a,i)<fast_get(b,i):
                    for _ in range(i+1):
                        result+= a.popleft()
                    i=0
    
    
                elif fast_get(b,i)<fast_get(a,i):
                    for _ in range(i+1):
                        result+= b.popleft()
                    i=0
            i=0                            
            if fast_get(a,i)<fast_get(b,i):
                result += a.popleft()
    
    
            elif fast_get(b,i)<fast_get(a,i):
                result += b.popleft()
            elif fast_get(b,i)==fast_get(a,i):
                result += a.popleft()                
    
        elif b:
            result += b.popleft()
        elif a:
            result += a.popleft()
    return result
    ```