Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> repeat_naive("hello", 1) True >>> repeat_naive("hello", 2) False >>> repeat_naive("hello"*10, 45) True >>> repeat_naive("hello"*10, 46) False >>> st = gen_str(10**3) >>> st 'wmsorzqbwxztycsdkrcpbcabfvtuinnnipcchfdxplypiurypilutqdkpcddmraldieipuumrcbrcjuufhsvhwhmcpbvjdxoikkdaymobfjljyygezgsuzgwaqxjbrihtykfxryglyouyyeqqibyfirlcgakizupgyzqzxrcdnsaaojxudpxjnxhflrmujrhwvcvpjkzdgwdeqdhpurgclcfcexfazdfwulakfnsfnlikypmjanhxcgeslzllvbnuglmpfoqscftxkplihjvlwphfqtumrpqrzssvipdehxwlrrskwpwgzrkfitnacntmnpfdobhnysqatyssumknztorgrdpswaawvyghijfbkujsyacgyxedeqqgdozvzwuxqrsgviawvlevqwydtypttszyqyaabwkqhrugpzjjrleempuesrapvihojtfvoowczyqyqsfewzayjxsvkxamiwlubzgoyvrndkrontnzyncvviarlfqsblahlztnibxheuawylpljtjupeetuqncfkmahpkuohbjrpaypbkixsisgzgdcbjihwwjktlyrytwzxggpgidclaxrlatuixortrflbfhyvuirjfzxbgnmkrweegbwrynbxwpwjevuipdqeumnoukxqcuyxtpjvinrmthericjqwhmtkdhypbdahwsyqkwzzsrzpkdmowxibdtwxgurfdlkomoovspyjdvlloikhqyxxyedflwpmsoeoreadlgfwphctqiagnkuadywugcwpjtlneiglotdehghhgxxwjsxqgrsfyfnygmhqiwgnqwrrxyzfodlsuwstooqddkocsyedntdvbhkzrjxcwcrslhslbljgfdchvbmfweuadvyhotzlgmeqtpbbrdvljnhztaxnplyqcgsxdogxwmntsqdrntxgmesxiejhfhslvvmeagwrvbmhfrvmheoutocjaqqmhvgeevkyciexxmvvbpafnsabwzj' >>> repeat_naive(st,3) True >>> repeat_naive(st,10) False #The following command takes time... (the naive solution is not very efficient) >>> repeat_naive(gen_str(10000),10) False #The solution using hash is much faster! >>> repeat_hash1(gen_str(10000),10) False #Comparing the 3 solutions #when n is doubled the time of repeat_hash1 and repeat_hash2 grows linearly (x2), # while the naive solution time grows quadratically (x4) >>> ================================ RESTART ================================ >>> str_len= 1000 repeating substring len= 10 repeat_naive 0.2299300915705299 found? False repeat_hash1 0.004555830235129343 found? False repeat_hash2 0.0006252326366699701 found? False >>> ================================ RESTART ================================ >>> str_len= 2000 repeating substring len= 10 repeat_naive 0.8622854068571126 found? False repeat_hash1 0.008956536463812115 found? False repeat_hash2 0.0014213779827326745 found? False >>> ================================ RESTART ================================ #when l is very small we get an answer (True) very quickly for random strings >>> str_len= 2000 repeating substring len= 2 repeat_naive 0.000876273013514735 found? True repeat_hash1 0.0006658885467564664 found? True repeat_hash2 3.789288707090749e-05 found? True >>> ================================ RESTART ================================ #comparing the effect of the table size on repeat_hash1. # small tables - too many collisions # very large tables - take to much time to create the table itself # Best behaviour for table size = number of items inserted to the table (in this case (n-l) ~ n substrings) >>> str_len= 1000 repeating substring len= 10 0.10515828766779937 found? False table size= 1 0.012255191226849316 found? False table size= 10 0.005445523646148337 found? False table size= 100 0.0047077965009866 found? False table size= 1000 0.00582563666957836 found? False table size= 10000 0.025222058238999118 found? False table size= 100000 #Itrerators: # an example: list iterator >>> l = [1,3,2] >>> type(l) >>> li = iter(l) >>> type(li) >>> li >>> next(li) 1 >>> li2 = iter(l) >>> next(li2) 1 >>> next(li2) 3 >>> next(li2) 2 >>> next(li2) Traceback (most recent call last): File "", line 1, in next(li2) StopIteration >>> next(li) 3 >>> next(li) 2 >>> next(li) Traceback (most recent call last): File "", line 1, in next(li) StopIteration >>> for item in l: print(item) 1 3 2 >>> #Generators >>> type(countdown_gen) >>> cg = countdown_gen() >>> type(cg) >>> next(cg) 5 >>> next(cg) 4 >>> next(cg) 3 >>> next(cg) 2 >>> next(cg) 1 >>> next(cg) 'launch' >>> next(cg) Traceback (most recent call last): File "", line 1, in next(cg) StopIteration >>> next(cg) Traceback (most recent call last): File "", line 1, in next(cg) StopIteration >>> next(cg) Traceback (most recent call last): File "", line 1, in next(cg) StopIteration >>> cd = countdown_gen() >>> cd2 = countdown_gen() >>> next(cd) 5 >>> next(cd) 4 >>> next(cd) 3 >>> next(cd2) 5 >>> for e in cd2: print(e) 4 3 2 1 launch >>>