Created
September 11, 2019 09:58
-
-
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.
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 characters
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