algoritms um6p cs

Telechargé par soufianelamrani2002
Algorithms II
PhD. Mohammed-Amine KOULALI
UM6P-CS
Mohammed VI Polytechnic University
13 maj 2022
1 / 43
Table of Contents
1Introduction
2Hash Tables
3Python Hash Table Implementation
2 / 43
Maps and Dictionaries
In a dictionary unique keys are mapped to associated values (e.g., Pyt-
hons dict class ).
The keys are assumed to be uniques, but the values are not necessarily
uniques.
Dictionaries are commonly known as associative arrays or maps.
3 / 43
Map as Python Dictionary
Lines 1–15 / 19
1import sys
f i l e n a m e = s y s . ar gv [ 1 ]
3
f r e q = {}
5for piece in open ( f i l e n a m e ) . r ea d ( ) . l ow er () . s p l i t ( ) :
# o nl y c o n s i d e r a l p h a b e t i c c h a r a c t e r s w i t h i n t h i s p i e c e
7word = . j o i n ( c for cin piece i f c . i s a l p h a ( ) )
i f word : # r e q u i r e a t
Çl e a s t one a l p h a b e t i c c h a r a c t e r
9f r e q [ word ] = 1 + f r e q . get ( word , 0 )
11 max_word = ’ ’
max_count = 0
13 for (w, c ) in f r e q . i t e m s ( ) : # ( key , v a l u e ) t u p l e s
Çr e p r e s e n t ( word , c o unt )
i f c > max_count :
15 max_word = w
4 / 43
Map as Python Dictionary
Lines 14–28 / 19
1i f c > max_count :
max_word = w
3max_count = c
print(’ The most f r e q u e n t word i s , max_word)
5print( I t s number o f o c c u r r e n c e s i s , max_count)
4 / 43
1 / 57 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !