+ 1
need help to solve this in python language
Suppose there is a process of pushing items A,B,C,D and E in that order ,onto an initially empty stack S .S is then popped four times ; as each item is popped off it is inserted into an initially empty queue Q .Then write a program that will do this process and print the content of S and Q after the process
3 Answers
+ 3
S=[1,2,3,4,5]
Q=[]
for i in range(5):
Q.append(S.pop())
+ 4
You should try this first by yourself. If you still need help after this, you can put your code in playground and link it here.
Besides of this, the sample shown here does not cover all elements in S, so better use ...range(len(S))
S=[1,2,3,4,5]
Q=[]
for i in range(len(S)): # ...range(4) does not cover all elements in S
Q.append(S.pop())
+ 3
S=[1,2,3,4,5]
Q=[]
while S:
Q.append(S.pop())
Is better