0% found this document useful (0 votes)
26 views5 pages

TD Pile-File

The document discusses stacks and queues in Python. It provides functions to create, add/remove elements from, copy and reverse stacks and queues. It also includes functions to rotate elements in stacks/queues and check balanced parentheses in a string using a stack.

Uploaded by

odgsunset
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

TD Pile-File

The document discusses stacks and queues in Python. It provides functions to create, add/remove elements from, copy and reverse stacks and queues. It also includes functions to rotate elements in stacks/queues and check balanced parentheses in a string using a stack.

Uploaded by

odgsunset
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

In [18]: def creation():

return []
def vacuite(p):
return p==[]

def empiler(p,a):
p.append(a)

def enfiler(p,a):
p.append(a)

def depiler(p):
if vacuite(p):
print("pile vide")
else :
return p.pop()

def defiler(f): #Pour les files


if vacuite(f):
print("file vide")
else :
return f.pop(0)

Exercice 1 :
In [6]: def stack_copy(s):
t=creation()
s2=creation()

while not vacuite(s):


x = depiler(s)
empiler(t,x)
while not vacuite(t):
x = depiler(t)
empiler(s,x)
empiler(s2,x)
return s2

In [7]: s = [1,2,3]
p=stack_copy(s)
print(p,s)

[1, 2, 3] [1, 2, 3]

In [8]: #avec deque


from collections import deque

def stCopy(pile):
t=deque()
s2=deque()

while len(pile)!=0:
x = pile.pop()
t.append(x)
while len(t)!=0:
x = t.pop()
pile.append(x)
s2.append(x)
return s2

In [9]: s = deque([1,2,3])
p=stCopy(s)
print(p,s)

deque([1, 2, 3]) deque([1, 2, 3])

Exercice 2
In [10]: #avec deque
from collections import deque

def reverse(pile):
s1=deque()
rs = deque()

while len(pile)!=0 :
x = pile.pop()
s1.append(x)
rs.append(x)

while len(s1)!=0 :
x = s1.pop()
pile.append(x)

return rs

pile=deque([1,2,3,4])
rs = reverse(pile)
print(rs,pile)

deque([4, 3, 2, 1]) deque([1, 2, 3, 4])

In [11]: def inverStack(s):


t = stack_copy(s)
rs = creation()
while not vacuite(t):
x = depiler(t)
empiler(rs,x)
return rs

In [12]: p = [7,11,98,2,10]
print(inverStack(p))

[10, 2, 98, 11, 7]

Exercice 3 :
In [13]: def permutation(s):

p2 = creation()

x = depiler(s)
while not vacuite(s):
empiler(p2,depiler(s))
empiler(s,x)
while not vacuite(p2):
empiler(s,depiler(p2))

def permCirc(p,n):

for i in range(n):
permutation(p)

In [14]: s = [103,2,98,11,7]
permCirc(s,2)
print(s)

[11, 7, 103, 2, 98]

Exercice 4 :
In [23]: def stack_circperm2(s,k):
p = creation()

for i in range(k) :
x = pop(s)
push(p,x)

p = inverStack(p)
permCirc(p,1)
p = inverStack(p)

while not vacuite(p):


x = pop(p)
push(s,x)

In [24]: s = [5,103,2,98,11,7]
stack_circperm2(s,4)
print(s)

[11, 7, 5, 103, 2, 98]

Exercice 5 :
In [25]: def testPar(s):
p = creation()

for c in s:
if c in ['(','[','{']:
push(p,c)
elif c == ')' or c == '}' or c == ']' :
x = pop(p)
if c==')' and x !='(' :
return "Parnthesage incorrect"
if c=='}' and x !='{' :
return "Parnthesage incorrect"
if c==']' and x !='[' :
return "Parnthesage incorrect"
if vacuite(p) :
return "Parenthesage correct"
else :
return "Parenthesage incorrect"

In [38]: testPar("((({})[][]))")

'Parenthesage correct'
Out[38]:

Pour les Files :


Ex1:
In [15]: def file_copy(f):
f1 = creation()
while len(f) != len(f1) :
x = defiler(f)
push(f,x)
push(f1,x)
return f1

Ex2 :
In [26]: def file_inv(f):
f2 = file_copy(f)
if not vacuite(f2):
x = defiler(f2)
file_inv(f2)
push(f2,x)
return f2

ou bien :

In [19]: def file_inv(f):


p = creation()
f2 = creation()
for i in range(len(f)):
x = defiler(f)
empiler(p,x)
enfiler(f,x)
for i in range(len(f)):
x = depiler(p)
enfiler(f2,x)
return f2

In [20]: f = [1,2,3,4,5]
f2 = file_inv(f)
print(f2)

[5, 4, 3, 2, 1]

Ex 3 :
In [21]: def file_permCirc(f,n):
for i in range(n): #ou bien (len(f) - n) ça depend du sens de rotation
x = defiler(f)
enfiler(f,x)

In [22]: f=[103,2,98,11,7]
file_permCirc(f,2)
print(f)

[98, 11, 7, 103, 2]

Exercice 5 (autre approche) :


In [3]: def par(p1,p2):
P=['(',')']
A=['{','}']
C=['[',']']
if p1 in P and p2 in P :
return True
elif p1 in A and p2 in A :
return True
elif p1 in C and p2 in C :
return True
else :
return False

par('[',')')

False
Out[3]:

In [23]: def testPar(Exp):

pile = creation()

O = ('(','{','[')
F = (')','}',']')
for c in Exp :
if c in O:
empiler(pile,c)
elif c in F :
x = depiler(pile)
if not par(x,c) :
return False
if vacuite(pile):
return True
else:
return False

testPar("sin{[sin(3/n)])^n}")

False
Out[23]:

In [ ]:

You might also like