File tree Expand file tree Collapse file tree 6 files changed +117
-0
lines changed
Expand file tree Collapse file tree 6 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ from sys import stdin
2+
3+
4+ class Queue :
5+ def __init__ (self ):
6+ self .queue = list ()
7+ self .queue_size = 0
8+
9+ def push (self , x ):
10+ self .queue .append (x )
11+ self .queue_size += 1
12+
13+ def pop (self ):
14+ if self .empty ():
15+ return - 1
16+ else :
17+ self .queue_size -= 1
18+ return self .queue .pop (0 )
19+
20+ def size (self ):
21+ return self .queue_size
22+
23+ def empty (self ):
24+ if self .queue_size == 0 :
25+ return 1
26+ else :
27+ return 0
28+
29+ def front (self ):
30+ if self .empty ():
31+ return - 1
32+ else :
33+ return self .queue [0 ]
34+
35+ def back (self ):
36+ if self .empty ():
37+ return - 1
38+ else :
39+ return self .queue [- 1 ]
40+
41+
42+ queue = Queue ()
43+ for _ in range (int (stdin .readline ())):
44+ cmd_args = stdin .readline ().split ()
45+
46+ if cmd_args [0 ] == "push" :
47+ getattr (queue , cmd_args [0 ])(cmd_args [1 ])
48+ else :
49+ print (getattr (queue , cmd_args [0 ])())
Original file line number Diff line number Diff line change 1+ from sys import stdin
2+
3+
4+ class Editor :
5+ def __init__ (self , data ):
6+ self .left_stack = list (data )
7+ self .right_stack = list ()
8+
9+ def print (self ):
10+ print ('' .join (self .left_stack + self .right_stack [::- 1 ]))
11+
12+ def left_cursor (self ):
13+ if self .left_stack :
14+ self .right_stack .append (self .left_stack .pop ())
15+
16+ def right_cursor (self ):
17+ if self .right_stack :
18+ self .left_stack .append (self .right_stack .pop ())
19+
20+ def press (self , data ):
21+ self .left_stack .append (data )
22+
23+ def backspace (self ):
24+ if self .left_stack :
25+ self .left_stack .pop ()
26+
27+
28+ editor = Editor (stdin .readline ().strip ())
29+ for _ in range (int (stdin .readline ())):
30+ cmd_args = stdin .readline ().split ()
31+
32+ if cmd_args [0 ] == "L" :
33+ editor .left_cursor ()
34+ elif cmd_args [0 ] == "D" :
35+ editor .right_cursor ()
36+ elif cmd_args [0 ] == "B" :
37+ editor .backspace ()
38+ elif cmd_args [0 ] == "P" :
39+ editor .press (cmd_args [1 ])
40+
41+ editor .print ()
Original file line number Diff line number Diff line change 1+ from sys import stdin
2+ from sys import exit
3+
4+ sequence = list ()
5+ for _ in range (int (stdin .readline ())):
6+ sequence .append (int (stdin .readline ()))
7+
8+ seq_cnt = 1
9+ delay_stack = [1 ]
10+ instructions = ['+' ]
11+
12+ for seq_value in sequence :
13+ if seq_cnt < seq_value :
14+ for i in range (seq_cnt + 1 , seq_value + 1 ):
15+ delay_stack .append (i )
16+ instructions .append ('+' )
17+ seq_cnt = seq_value
18+
19+ if seq_value <= seq_cnt and delay_stack \
20+ and delay_stack [- 1 ] == seq_value :
21+ delay_stack .pop ()
22+ instructions .append ('-' )
23+ else :
24+ print ("NO" )
25+ exit (0 )
26+
27+ print ('\n ' .join (instructions ))
You can’t perform that action at this time.
0 commit comments