Skip to content

Instantly share code, notes, and snippets.

@priteshgupta
Created April 8, 2014 16:57
Show Gist options
  • Save priteshgupta/10155815 to your computer and use it in GitHub Desktop.
Save priteshgupta/10155815 to your computer and use it in GitHub Desktop.
stuph
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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment