I've tried to look for an answer last night and a little this morning, but had no luck. For homework, we have been asked to go through a list, and return the longest run of consecutive numbers. I've been working on this code and it has only been comparing the numbers (if it works correctly) with the first number. I need to know the len(consecutive increases).
#!/usr/bin/python3
import sys
def fOpen(fname):
try:
fd = open(fname,"r")
except:
print("Couldn't open file.")
sys.exit(0)
all = fd.read().splitlines()
fd.close()
return all
words = fOpen(sys.argv[1])
current = []
lastc = []
for x in words:
if len(current) == 0: #Add the first number
current.append(int(x))
elif len(current) == 1: #Check first number < next number
if current[0] < int(x):
current.append(int(x))
else: #elif current[0] >= int(x):
if len(current) >= len(lastc):
lastc = current
current[:] = []
current.append(int(x))
elif len(current) >= 2:
if current[-1] < int(x):
current.append(int(x))
else: #elif current[-1] >= int(x):
if len(current) >= len(lastc):
lastc = current
elif len(current) < len(lastc):
current[:] = []
current[:] = []
current.append(int(x))
print(lastc)
The print statements are just got myself to be able to trace through and will be removed later. Thank you in advance.