-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
76 lines (68 loc) · 2.35 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# **************************************************************************** #
# #
# ::: :::::::: #
# command.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: achane-l <achane-l@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/10/20 14:16:43 by achane-l #+# #+# #
# Updated: 2021/10/22 16:37:08 by achane-l ### ########.fr #
# #
# **************************************************************************** #
def push(stack_a, stack_b, command):
if (command == "pa\n"):
value = stack_b[0];
stack_b.pop(0);
stack_a.insert(0, value);
else:
value = stack_a[0];
stack_a.pop(0);
stack_b.insert(0, value);
def rotate_stack(my_stack):
value = my_stack[0];
my_stack.pop(0);
my_stack.append(value);
def rotate(stack_a, stack_b, command):
if (command == "ra\n"):
rotate_stack(stack_a);
elif (command == "rb\n"):
rotate_stack(stack_b);
else:
rotate_stack(stack_a);
rotate_stack(stack_b);
def reverse_rotate_stack(my_stack):
value = my_stack[-1];
my_stack.pop(-1);
my_stack.insert(0,value);
def reverse_rotate(stack_a, stack_b, command):
if (command == "rra\n"):
reverse_rotate_stack(stack_a);
elif (command == "rrb\n"):
reverse_rotate_stack(stack_b);
else:
reverse_rotate_stack(stack_a);
reverse_rotate_stack(stack_b);
def swap_stack(my_stack):
value = my_stack[1];
my_stack.pop(1);
my_stack.insert(0,value);
def swap(stack_a, stack_b, command):
if (command == "sa\n"):
swap_stack(stack_a);
elif (command == "sb\n"):
swap_stack(stack_b);
else:
swap_stack(stack_a);
swap_stack(stack_b);
def initiate_command(stack_a, stack_b, command):
if (command[0] == 'p'):
push(stack_a, stack_b, command);
elif (command[0] == 'r' and command[2] == '\n'):
rotate(stack_a, stack_b, command);
elif (command[0] == 'r' and command[3] == '\n'):
reverse_rotate(stack_a, stack_b, command);
elif (command[0] == 's'):
swap(stack_a, stack_b, command);
else:
return (0);
return (1);