PASTE-IT!

Holding 522M in 34869 pastes. Meet us at #paste-it.net, OFTC!
Try our Mozilla Jetpack clipboard paster and our Chrome extension!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
import time

class Nodo(object):
    def __init__(self):
        self.hijos = {}
        self._fin = False

    def agregar(self, hijo):
        prilet,resto = hijo[0],hijo[1:]
        nodo = self.hijos.setdefault(prilet, Nodo())
        if not resto:
            nodo._fin = True
        else:
            nodo.agregar(resto)

    def get(self, busq):
        nodo = self
        for let in busq:
            nodo = nodo[let]
        self._result = []
        self._arma(nodo, busq)
        return self._result

    def _arma(self, nodo, base):
        for k,v in nodo.hijos.items():
            if v._fin:
                self._result.append(base+k)
            self._arma(v, base+k)

    def __repr__(self):
        return repr(self.hijos)
    def __getitem__(self, k):
        return self.hijos[k]

if __name__ == "__main__":
    if len(sys.argv) == 2:
        tiempo_inicial_carga = tiempo_inicial_total = time.time()
        raiz = Nodo()
        for linea in open("diccionariocs.txt"):
            raiz.agregar(linea[:-2])
        print "En cargar: %.2f seg" % (time.time() - tiempo_inicial_carga)
        tiempo_inicial_busqueda = time.time()
        raiz.get(sys.argv[1])
        print "Busq x palabra: %.2f mseg" % (1000 * (time.time()-tiempo_inicial_busqueda))
        print "Tiempo total: %.2f seg" % (time.time() - tiempo_inicial_total)
    else:
        print "Usar: python facu-original.py <cadena>"
Paste by
humitos,
822 day(s) ago
21:22 09-11-2007
in syntax