Last active
March 13, 2021 12:51
-
-
Save flysand7/08a0a85664b8fe63272952103f7c2701 to your computer and use it in GitHub Desktop.
This is a program that swaps two stack contents using third stack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 10 | |
# stack data | |
class t_stack: | |
arr = 0; | |
top = 0; | |
def __init__(self): | |
self.arr = []; | |
def push(S, value): | |
assert(S.top != len(S.arr)); | |
S.arr[S.top] = value; | |
S.top += 1; | |
def pop(S): | |
assert(S.top != 0); | |
S.top -= 1; | |
result = S.arr[S.top]; | |
S.arr[S.top] = '0'; | |
return result; | |
def move(_from, _to, n=1): | |
for i in range(n): | |
push(_to, pop(_from)); | |
# initial data | |
import string; | |
A = t_stack(); | |
B = t_stack(); | |
C = t_stack(); | |
for i in range(n): | |
A.arr.append(string.ascii_lowercase[i]); | |
B.arr.append(string.ascii_uppercase[i]); | |
C.arr.append('0'); | |
A.top = n; | |
B.top = n; | |
C.top = 0; | |
print(''.join(A.arr)); | |
print(''.join(B.arr)); | |
print(''.join(C.arr)); | |
# move | |
move(B, C, n); | |
move(A, B, n); | |
move(C, A, n-1); | |
for m in range(n-1, 1, -1): | |
move(B, A, 1); | |
move(B, C, m); | |
move(A, B, 1); | |
move(C, B, m); | |
move(C, A, 1); | |
print(); | |
print(''.join(A.arr)); | |
print(''.join(B.arr)); | |
print(''.join(C.arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment