Skip to content

Instantly share code, notes, and snippets.

@joereddington
Created September 11, 2019 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joereddington/4cbdbbbe8c836639b568c3547db8a85c to your computer and use it in GitHub Desktop.
Save joereddington/4cbdbbbe8c836639b568c3547db8a85c to your computer and use it in GitHub Desktop.
Finds the longest sections in a text file that don't repeat letters.
import sys
input_file=[]
with open(sys.argv[1]) as f:
while True:
c = f.read(1)
if not c:
print "End of file"
break
input_file.append(c)
def process(start_here):
subfile=input_file[start_here:]
currentResult=""
letters ={} #letters is a dictionary
index=len(subfile)
for i in range(index):
c=subfile[i]
if c in letters:
#print "Found {} at index {}, last seen {} ago".format(c,i,i-letters[c])
temp=currentResult.rsplit(' ',1)[0]
ideas.append(temp.rstrip('?:!.,;').replace("\n",""))
temp=letters[c]
length_of_string=len(currentResult)
#print length_of_string
how_much_to_cut=length_of_string+letters[c]-i
#print "Hey {}".format(how_much_to_cut)
replacement=currentResult[how_much_to_cut+1:]
removed=currentResult[:how_much_to_cut]
#print "Current result was {}, and will now be {} ...".format(currentResult,replacement)
letters={}
for x in range(len(replacement)):
letters[replacement[x]]=i-len(replacement)+x
#print "setting {}".format(replacement[x])
letters[c]=i
currentResult=replacement
currentResult+=c
if not c.isspace():
letters[c]=i
index+=1
ideas=[]
index=0
process(index)
print "About to remove doubles"
ideas=sorted(set(ideas))
print "About to sort"
ideas.sort(lambda x,y: cmp(len(x), len(y)))
for x in ideas:
print "X:"+x.strip()+":X"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment