You are on page 1of 2

CPGE : Reda Slaoui Année scolaire : 2023/2024

Python : Correction TP Les chaînes de


caractères 2

Exercice 1 :

1 d e f ordCh ( c h a i n e ) :
2 L=[]
3 f o r ch i n c h a i n e :
4 L . append ( s t r ( ord ( ch ) )
5 r e t u r n " " . j o i n (L)
6
7 d e f code_vers_chaine ( c h a i n e ) :
8 s = chaine . s p l i t ( " " )
9 r = ""
10 for c in s :
11 r += c h r ( i n t ( c ) )
12 return r
13
14 def estMiniscule ( chaine ) :
15 f o r ch i n c h a i n e :
16 i f ord ( ’A ’ )<=ord ( ch )<=ord ( ’ Z ’ ) :
17 return False
18 r e t u r n True

Exercice 2 :

1 def conjuguer ( verbe ) :


2 S = [ ’ j e ’ , ’ tu ’ , ’ i l − e l l e ’ , ’ nous ’ , ’ vous ’ , ’ i l s − e l l e s ’ ]
3 T = [ ’ e ’ , ’ e s ’ , ’ e ’ , ’ ons ’ , ’ e z ’ , ’ e n t ’ ]
4 i f v e r b e [ − 2 : ] != " e r " :
5 p r i n t ( " Erreur " )
6 else :
7 f o r i in range ( len (S) ) :
8 p r i n t ( S [ i ]+ " "+v e r b e [: −2]+T [ i ] )

Exercice 3 :

1 def decalage (c , d) :
2 r e t u r n c h r ( ord ( ’ a ’ ) +( ord ( c )−ord ( ’ a ’ )+d ) %26)
3

1
4 d e f c h i f f e r e m e n t C e s a r ( ch , p ) :
5 s = "";
6 f o r i i n r a n g e ( l e n ( ch ) ) :
7 s+=d e c a l a g e ( ch [ i ] , p )
8 return s
9
10 d e f d e c h i f f e r e m e n t C e s a r ( ch , p ) :
11 s = "";
12 f o r i i n r a n g e ( l e n ( ch ) ) :
13 s+=d e c a l a g e ( ch [ i ] , −p )
14 return s

You might also like