"'O
0
c
:J
0
LI)
.--t
0
N
@
.......
J::
Ol
·;::::
>0..
0
u
Chapitre 9 • Concepts fondamentaux
218
Implémentation en Python
i mp o r t r a n d o m
n o mb r e = i n t ( 8 9 9 . 9 * r a n d o m. r a n d o m( ) + 1 0 0 )
n b = s t r ( n o mb r e )
essai=O
r e p=inpu t(" d ev i ne nb
")
wh i 1 e r e p ! = n b :
nb _ b onnes_re p=Ü
essa i =e ssai +l
for k in ran ge(3):
i f nb[k]==rep[k]:
nb _ b onn es _ rep=nb _ bonne s_ r ep+ l
r e p=i nput( st r(nb _ bonn es_ r ep) +' chi f fr e(s) co rr ect(s),
r e c o mme n c e : ' )
prin t( ' gagné en '+st r(e ss ai)+' essa i (s) !' )
Q .. 11) Fonction position(mot,texte: chaînes ,n: entier): booléen.
Rôle : renvoie VRAI si mot apparaît dans texte à la position n, FAUX sinon.
Variable locale : i (entier)
Début
Si n- 1 +longueur(mot)>longueur(texte) Alors
1
Retourner(FAUX)
# mot trop long pour la position n
FinSi
Pour i de 0 à longueur(mot)-1 Faire
Si texte[n-1 +i] !=mot[i] Alors
1
Retourner(FAUX)
Fin Si
Fin Pour
Retou rner(VRAI)
Fin Fonction
Implémentation en Python
d e f p o s i t i o n ( mo t , t e x t e , n ) :
i f n - 1 + 1 e n ( mo t ) > 1 e n ( t e x t e ) :
r e t u r n ( F a 1 s e )
# mo t t r o p 1 o n g p o u r 1 a p o s i t i o n n
f o r i i n r a n g e ( 1 e n ( mo t ) ) :
i f t e x t e [ n - 1 + i ] ! = mo t [ i ] :
r et urn( Fat se)
r etu rn (T ru e)
2) Fonction compte(mot,texte: chaînes) : entier.
Rôle : compte le nombre d'apparitions de la chaîne mot dans la chaîne texte .
Variables locales : n,compteur (entiers)
Début
compteur ~ 0
#initialisation du compteur
Pour n de 1 à longueur(texte)) Faire
Si position(mot,texte,n) Alors
1
compteur ~ compteur+ l
Fin Si
Fin Pour
Retourner(co mpteur)
Fin Fonction
Précédent

- 226/302

Suivant