I'm trying to find a run in a list without using pre-defined list functions in Python, such as enumerate, zip, index, etc.
By a run, I mean given a list [1,2,3,6,4], I want to return a new list [1,2,3] as well as returning where the run starts and ends, and the length of the run.
Below is my attempt at this but I can't seem to get it, I seem to always keep running into the problem where I get an index out of range error.
def run_cards (hand_shuffle, hand_sample):
true_length = len(hand_shuffle) - 1
y = 0
r = []
for x in range(len(hand_shuffle)):
y = x + 1
if y <= true_length: #Checks if y is in range with x
t = hand_shuffle[y] - hand_shuffle[x] #Subtracts the two numbers to see if they run
r.append(t)
lent = len(hand_shuffle)
if hand_shuffle[lent-1] - hand_shuffle[lent-2] == 1:
r.append(1)
h = []
for i in range(len(r)):
if r[i] == 1: #If t from above for loop is equal to 1 append it to the new list
h.append(i)
h.append(i+1)
p = []
for j in h:
p.append(hand_shuffle[j])
print (p)
return hand_shuffle, hand_sample
Right now, my code runs, but doesn't give me what I am looking for.