We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
```
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Morgan and a String
You are viewing a single comment's thread. Return to all 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