-
-
Save priteshgupta/10155815 to your computer and use it in GitHub Desktop.
Revisions
-
priteshgupta created this gist
Apr 8, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,73 @@ from time import time def sol(): words = open('words.txt') stuph = open('stuph_sample.txt').read() out = '' wordsl = [] for word in words: wordsl.append(word.strip()) l, i = 0, 0 maxl = len(max(wordsl, key=len)) while i < len(stuph): if len(stuph[l:i]) <= maxl and stuph[l:i] in wordsl and stuph[l:i+1] not in wordsl: out += stuph[l:i].title() l = i elif len(stuph[l:i]) == maxl: out += '-' l += 1 i = l if i == len(stuph) - 1 and l < i: i = l l += 1 out += '-' elif i == len(stuph) - 1: out += '-' i += 1 used = len(out) - out.count('-') print('Characters used: {} ({}%)'.format(used, float(used)/float(len(stuph)) * 100)) print('Output\n' + out + '\n') print('Benchmarking..........') bmark(wordsl, stuph) def bmark(words, stuph): t1 = time() for _ in range(10): stuph = stuph out = '' wordsl = words # Not looping through words again l, i = 0, 0 maxl = len(max(wordsl, key=len)) while i < len(stuph): if len(stuph[l:i]) <= maxl and stuph[l:i] in wordsl and stuph[l:i+1] not in wordsl: out += stuph[l:i].title() l = i elif len(stuph[l:i]) == maxl: out += '-' l += 1 i = l if i == len(stuph) - 1 and l < i: i = l l += 1 out += '-' elif i == len(stuph) - 1: out += '-' i += 1 t2 = time() total = t2 - t1 print(' average time per run: {} ms (10 runs, {} ms total)'.format(round((total/10) * 1000), round(total * 1000)))