Commits

Flávio Coelho committed ac49211

exemplo de MDC com testes e profiling adicionado

Comments (0)

Files changed (5)

+# -*- coding: utf-8 -*-
+"""
+Máximo Divisor Comum: O algoritmo de euclides e variantes
+Created on Thu Jun 16 11:11:53 2011
+
+@author: Flávio Codeço Coelho<fccoelho@gmail.com>
+"""
+# Por decomposição em fatores primos
+import time
+
+def timeit(fun):
+    """
+    Decorador para calcular o tempo usado por funções (ou métodos)
+    """
+    def timed(*args, **kw):
+        ts = time.time()
+        result = fun(*args, **kw)
+        te = time.time()
+        print '%r (%s,%s)  %2.2f sec'%(fun.__name__, args, kw , te-ts)
+        return result
+    return timed
+
+def MDC_forca_bruta(a,b):
+    """
+    testa inteiros em ordem decrescente
+    """
+    if a == 0 or b == 0:
+        return a or b
+    for i in range(min(a,b),0,-1):
+        if not (a%i or b%i):
+            return i
+
+def MDC_E(a,b):
+    """
+    Algoritmo de Euclides
+    Versão recursiva
+    """
+    if b == 0:
+        return a
+    return MDC_E(b,a%b)
+    
+def MDC_E_l(a,b):
+    """
+    Algoritmo de Euclides
+    sem recursão
+    """
+    while b != 0:
+        b,a = a%b,b
+    return a
+    
+if __name__=="__main__":
+    
+
+
         self.depth = depth
         self.lingua = lingua
         self.fila=[]
-        self.depth = depth
         self.curdepth = 0
         self.started = 0
         self.nlinks = 0
             self.termoatual = self.urlatual.split('/')[-1]
             
             if ":" in self.termoatual:
-                print "+++",self.termoatual
+                #~ print "+++",self.termoatual
                 continue
             if self.termoatual in ['Main_page']+self.history:
                 continue
 
    
 if __name__=="__main__":
-    palavra = u'matemática'
-    lingua = 'pt'
+    #~ palavra = u'matemática'
+    #~ lingua = 'pt'
     #~ Cr = Crawler(palavra,lingua,1)
-    Cr = Crawler(sys.argv[1],sys.argv[2],2)
+    Cr = Crawler(sys.argv[1],sys.argv[2],1)
     Cr.move()
     #~ NX.draw(Cr.G)
     #~ P.show()
                 print "%s infectou %s"%(self.nome, alvo.nome)
                 t = threading.Thread(target=alvo.contraiu)
                 t.start()
-                t.join()
+        #t.join()
                 
     def contraiu(self):
         """
 print "usamos %s das arestas possiveis"%(NX.density(G))
 print NX.degree_histogram(G), G.doentes
 am = NX.adj_matrix(G)
+print G.number_of_nodes(),G.number_of_edges()
 
 P.figure()
 P.pcolor(array(am))
     for ng,c in ngs.iteritems():
         print ' '.join(ng), c
     frases = PT.sent_seg(PT.raw)
+    print frases[10]
     for w,c in PT.fp.items()[10:30]:
         print w,c
     PT.concordance('Lobo',30)
+"""
+Modulo de teste para MDC.py
+"""
+from MDC import *
+import unittest
+
+class TestMDCForcaBruta(unittest.TestCase):
+    def test_m_d_c_forca_bruta(self):
+        a=1071
+        b=462
+        self.assertEqual(21, MDC_forca_bruta(a, b))
+    def test_m_d_c_forca_bruta2(self):
+        a=0
+        b=462
+        self.assertEqual(462, MDC_forca_bruta(a, b))
+    def test_m_d_c_forca_bruta3(self):
+        a=1071
+        b=0
+        self.assertEqual(1071, MDC_forca_bruta(a, b))
+    def test_m_d_c_forca_bruta4(self):
+        a=18
+        b=84
+        self.assertEqual(6, MDC_forca_bruta(a, b))
+
+class TestMDCE(unittest.TestCase):
+    def test_m_d_c__e(self):
+        a=1071
+        b=462
+        self.assertEqual(21, MDC_E(a, b))
+    def test_m_d_c__e2(self):
+        a=0
+        b=462
+        self.assertEqual(462, MDC_E(a, b))
+    def test_m_d_c__e3(self):
+        a=1071
+        b=0
+        self.assertEqual(1071, MDC_E(a, b))
+    def test_m_d_c__e4(self):
+        a=18
+        b=84
+        self.assertEqual(6, MDC_E(a, b))
+
+class TestMDCE_l(unittest.TestCase):
+    def test_m_d_c__e__l(self):
+        a=1071
+        b=462
+        self.assertEqual(21, MDC_E_l(a, b))
+    def test_m_d_c__e__l2(self):
+        a=0
+        b=462
+        self.assertEqual(462, MDC_E_l(a, b))
+    def test_m_d_c__e__l3(self):
+        a=1071
+        b=0
+        self.assertEqual(1071, MDC_E_l(a, b))
+    def test_m_d_c__e__l4(self):
+        a=18
+        b=84
+        self.assertEqual(6, MDC_E_l(a, b))
+
+if __name__ == '__main__':
+    unittest.main()
+
+