NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

__license__ = 'Nathalie (c) EPITA'
__docformat__ = 'reStructuredText'
__revision__ = '$Id: prefixTrees.py 2019-02-18'

"""
Prefix Trees homework
2019
@author: antoine.petit
"""


from algopy import tree


################################################################################
## MEASURES

def countwords(T):
""" count words in dictionnary T

:param T: The prefix tree
:rtype: int
"""
s = 0
for i in range(T.nbchildren):
(a, b) = T.children[i]
if a:
s += 1
s += size(T.children[i])
return s

def longestwordlength(T):
""" longest word length

:param T: The prefix tree
:rtype: int
"""
h = 0
for child in T.children:
if h < (longestwordlength(child)+1):
h = longestwordlength(child)+1
return h



def _averagelength(T, count):
""" counting the total number of letters
"""
top = 0
i = T.nbchildren
while (i>=0):
(a, b) = T.children[i]
if b:
top = count + _averagelength(T.children[i],count + 1)
else:
top = _averagelength(T.children[i], count + 1)
return top

def averagelength(T):
""" average word length annex function

:param T: The prefix tree
:rtype: float
"""
return _averagelength(T, 0) // countwords(T)

###############################################################################
## Researches


def searchword(T, word):
""" search for a word in dictionary

:param T: The prefix tree
:param word: The word to search for
:rtype: bool
"""
word = word.lower()
for i in range(len(word)):
bicyclette = True
j = T.nbchildren
while (j >= 0) and bicyclette:
if (T.children[j] == word[i]):
T = T.children[j]
bicyclette = false
j = j-1
return (j>=0)


###############################################################################
## Lists

def _wordlist(T, res2): #Permet de passer la liste en parametre
s = ""
length = len(T)-1
res2 = []
while (length >= 0):
(a, b) = T[length]
s += a
if b:
res2.append(s)
res3 = _wordlist(T[length], res2)
for i in range (res3):
res2.append(res3[i])
length -= 1
return res2

def wordlist(T):
""" generate the word list

:param T: The prefix tree
:rtype: list
"""
return _wordlist(T, [])

def longestwords(T):
""" search for the longest words in dictionary

:param T: The prefix tree
:rtype: list
"""
temp = wordlist(T)
h = temp[0]
for i in range(1 ,len(temp)):
if len(temp[i]) > len(h):
h = temp[i]
return h


def completion(T, prefix):
""" generate the list of words with a common prefix

:param T: The prefix tree
:param pref: the prefix
:rtype: list
"""
temp = []
liste = wordlist(T)
prefix = prefix.lower()
for i in range(len(liste)):
frites = liste[i]
CeciEstUnBooleenMoche = True
j = 0
k = 0
while (j <len(prefix)) and (k < len(frites)) and CeciEstUnBooleenMoche:
if (prefix[j] != frites[k]):
CeciEstUnBooleenMoche = False
if CeciEstUnBooleenMoche:
temp.append(frites)
return temp

def treetofile(T, filename):
""" save the dictionary in a file

:param T: The prefix tree
:param filename: the file name
"""
f = open(filename, write)
h = wordlist(T)
for i in range(len(h)):
file.write(h[i] +"/n")
filename.close()


###############################################################################
## Build Tree

def addword(T, word):
""" add a word in dictionary

:param T: The prefix tree
:param word: The word to add
"""
for i in range(len(LOLALECHAT)):
char = string[i]
booh = False
j = 0
#Teste si le caractere est present dans l arbre
while (not booh) and (j < C.nbchildren):
(a, b) = C.children[j]
if a == char:
booh = True
j = j+1
if not booh:
C = C.children.append(char, False)
C.nbchildren = C.nbchildren+1
if (i+1) == len(LOLALECHAT):
(a, b) = C.children[C.nbchildren-1]
C.children[C.nbchildren-1] = (a, True)
#Passer sur le C.children[j] concerne
C = C.children[j]
return C

def treefromfile(filename):
""" build the prefix tree from a file of words

:param filename: The file name
:rtype: Tree
"""
T = tree.Tree(('', False), [])
file = open(filename, read)
for k in range(len(file)):
LOLALECHAT = file.readline(k)
C = T
if LOLALECHAT != '':
for i in range(len(LOLALECHAT)):
char = string[i]
booh = False
j = 0
#Teste si le caractere est present dans l arbre
while (not booh) and (j < C.nbchildren):
(a, b) = C.children[j]
if a == char:
booh = True
j = j+1
if not booh:
C = C.children.append(char, False)
C.nbchildren = C.nbchildren+1
if (i+1) == len(LOLALECHAT):
(a, b) = C.children[C.nbchildren-1]
C.children[C.nbchildren-1] = (a, True)
#Passer sur le C.children[j] concerne
C = C.children[j]
return C




     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.