Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Created May 31, 2018 03:15
Show Gist options
  • Select an option

  • Save chrisdel101/1f252b0021b635852c7d92647f8a292d to your computer and use it in GitHub Desktop.

Select an option

Save chrisdel101/1f252b0021b635852c7d92647f8a292d to your computer and use it in GitHub Desktop.
Lines from similarites
def lines(file1, file2):
result = set()
def getLinesList(file):
# storage array
fileList = []
# temp storage array
tempList = []
# if all lines have \n at the end
for i in file:
# if not a new line, then all one line
if not i == "\n":
# add all to new list on line
tempList.append(i)
else:
# when end of line
# make a string
tmp = ''.join(tempList)
# push string into file list
fileList.append(tmp)
# clear the array
tempList.clear()
finalLine = getFinalLine(file)
# if return is not None; since if nothing, still returns None
if(finalLine != None):
# append final line to the list of lines
fileList.append(finalLine)
removedList = removeBlankLines(fileList)
# if there were blank lines, use the new list, else use the origial
if(removedList != fileList):
print('reassign')
fileList = removedList
return fileList
lines1 = getLinesList(file1)
# print(lines1)
lines2 = getLinesList(file2)
# print(lines1)
for i in lines1:
for j in lines2:
if i == j:
# add to the set
result.add(i)
# make set into a list with this syntax
result = list(result)
print(result)
return result
# if last line does not end in a \n, get that line
def getFinalLine(str):
# get final \n
last_n = str.rindex("\n")
# get all of string at exists after that
lastLine = str[last_n:len(str)]
# space is equal to 1, single char equal to 2
# if len 1, space, means there is already a \n
print(f"len:{len(lastLine)}")
if(len(lastLine) <= 1):
return #returns None if this fires
else:
# if greater than one, no \n, needs to be handled
# strip off all spaces
return lastLine.strip()
# remove extra \n following the end of the text, they show up as "" in finalList
def removeBlankLines(inputList):
results = []
# take all non-blank lines and make a new list
for i in inputList:
if(i != ""):
# print(i)
results.append(i)
# else:
# print(i)
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment