In: Computer Science
what is the expected output of the following segment of code?
dq = Deque()
dq.push_back('M')
dq.push_back(' ')
l = ['A', 'L', 'E', 'S', 'T', 'E']
for i in l:
→ if ord(i) % 3 == 0:
→ → dq.push_back(i)
→ elif ord(i) % 5 == 0:
→ → dq.push_front(i)
→ → dq.push_back(i)
→ elif ord(i) % 4 == 0:
→ → dq.push_back(i)
→ else:
→ → dq.peek_front()
→ → dq.push_front('X')
→ → dq.push_back('R')
dq.push_front(dq.peek_back())
dq.pop_back()
print(dq)
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
Output
deque(['E', 'X', 'A', 'M', ' ', 'A', 'L', 'E', 'R', 'T'])
The above is not compatible with python, so I made same program that would be compatible in python
from collections import deque
dq = deque()
dq.append('M')
dq.append(' ')
l = ['A', 'L', 'E', 'S', 'T', 'E']
for i in l:
if ord(i) % 3 == 0:
dq.append(i)
elif ord(i) % 5 == 0:
dq.appendleft(i)
dq.append(i)
elif ord(i) % 4 == 0:
dq.append(i)
else:
dq[0]
dq.appendleft('X')
dq.append('R')
dq.appendleft(dq[len(dq)-1])
dq.pop()
print(dq)