Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1dffa21ee8c4b9d7a277f4cf29c04057 to your computer and use it in GitHub Desktop.
Save anonymous/1dffa21ee8c4b9d7a277f4cf29c04057 to your computer and use it in GitHub Desktop.
from heapq import heappush, heapify, heappop
#Question1
def table_frequences(texte):
table={}
for i in texte:
if i in table:
table[i]=table[i]+1
else:
table[i]=1
return table
#question2
def arbre_huff(occurrences):
#construction d'un tas
tas=[(occ, lettre) for (lettre, occ) in occurrences.items()]
heapify(tas) #trier le tas
while len(tas)>=2:
occ1, noeud1 = heappop(tas) #noeud de + petit poids occ1
occ2, noeud2 = heappop(tas) #noeud de 2ème + petit poids
heappush(tas, (occ1+occ2,{0: noeud1, 1: noeud2})) #ajoute au tas le noeud de poids occ1+occ2
return heappop(tas)[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment