Reputation: 195
Assume a list of [1, 2, 3, 4]
to push to the stack, the possible sequence of popping from the stack is:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
3 2 1 4
3 2 4 1
3 4 2 1
4 3 2 1
I found a C++ version of the algorithm, it gives the correct output:
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>
using namespace std;
void printAllOutStackSeq( queue<int> inQueue, int n, stack<int> st, queue<int> out )
{
if( n <= 0 || ( inQueue.empty() && st.empty() && out.empty() ) )
{
return;
}
if( out.size() == n )
{
while( !out.empty() )
{
cout << out.front() << ' ';
out.pop();
}
cout << endl;
return;
}
stack<int> stCopy = st;
queue<int> outCopy = out;
if( !st.empty() )
{
out.push( st.top() );
st.pop();
printAllOutStackSeq( inQueue, n, st, out );
}
if( !inQueue.empty() )
{
stCopy.push( inQueue.front() );
inQueue.pop();
printAllOutStackSeq( inQueue, n, stCopy, outCopy );
}
return;
}
int main()
{
int ret = 0;
int a[] = { 1, 2, 3, 4 };
queue<int> inQueue;
for( int i = 0; i < 4; i++ )
{
inQueue.push( a[i] );
}
int n;
stack<int> st;
queue<int> out;
printAllOutStackSeq( inQueue, 4, st, out );
return ret;
}
After I converted the C++ to Python as below:
from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return
if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return
stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)
if stack:
popped.append(stack.pop())
print_out_stack_seq(to_push, n, stack, popped)
if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(to_push, n, stack_copy, popped_copy)
return
to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)
It just print the first line:
1 2 3 4
I really don't know why, can somebody help me out?
Upvotes: 0
Views: 321
Reputation: 56
In your C++ version, inQueue is passed by copy in recursive calls. This is not the case in your python version where inQueue is to_push.
Adding a copy of to_push before calling print_out_stack_seq seems to fix your issue.
from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return
if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return
stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)
if stack:
popped.append(stack.pop())
print_out_stack_seq(copy.copy(to_push), n, stack, popped)
if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(copy.copy(to_push), n, stack_copy, popped_copy)
return
to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)
Output:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
3 2 1 4
3 2 4 1
3 4 2 1
4 3 2 1
However, this output does not seems to be the right answer as the sequence (4 3 1 2) is valid but not printed. As others mentioned, what you print is the list of all permutations. In python it can be done with the following code:
import itertools
to_push = [1, 2, 3, 4]
for p in itertools.permutations(to_push):
print(p)
Output:
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)
Upvotes: 1