Find longest subsequence in a list - python

I want to write a function that finds the longest ascending sequence in a list in linear time. This seems like a really easy task but without nested for-loops I am stuck somehow. My idea was the following:
if len(L) == 0 or len(L) == 1:
return len(L)
if all(L[i] == L[0] for i in range(len(L)-1)):
return len(L)
left = [1] * len(L)
right = [1] * len(L)
count = 1
for i in range(len(L)-1):
if L[i] <= L[i+1]:
count += 1
left[i+1] = count
else:
count = 1
left[i+1] = count
count = 1
for i in range(len(L)-1, -1, -1):
if L[i] <= L[i-1]:
count += 1
right[i-1] = count
else:
count = 1
right[i-1] = count
idx_left = left.index(max(left))
idx_right = right.index(max(right))
if max(max(left), max(right)) == max(left) and idx_left == len(left) - 1:
return max(left)

You can try this:
mylist=[10,9,8,10,6,5,4,3,2,3]
previous = mylist[0]
max_sublist = [previous]
current_sublist = [previous]
increasing = True
for x in mylist[1:]:
if increasing and previous <= x:
current_sublist.append(x)
elif previous >= x:
increasing = False
current_sublist.append(x)
else:
if len(current_sublist) > len(max_sublist):
max_sublist = current_sublist[:]
current_sublist = [previous, x]
increasing = True
previous = x
if len(current_sublist) > len(max_sublist):
max_sublist = current_sublist[:]
print(f"{max_sublist=}\n{len(max_sublist)=}")
It gives:
max_sublist=[8, 10, 6, 5, 4, 3, 2]
len(max_sublist)=7

try working on the diffrences between 2 values,
I'm not sure it works for every case but it's a start in o(n),
added 1 to the resault in the end cause it's counting comparisons so the last value will not be counted
def sequance(seq):
max_len = 0
current_len = 0
going_down = False
for i in range(len(seq)-1):
if seq[i] == seq[i+1]:
current_len += 1
if max_len < current_len:
max_len = current_len
continue
if seq[i] < seq[i+1]:
if going_down:
current_len = 1
going_down = False
continue
else:
current_len +=1
if max_len < current_len:
max_len = current_len
continue
if seq[i] > seq[i+1]:
if going_down:
current_len += 1
if max_len < current_len:
max_len = current_len
continue
else:
going_down = True
current_len += 1
if max_len < current_len:
max_len = current_len
return max_len + 1
[10, 9, 8, 10, 6, 5, 4, 3, 2, 3] # 7
[4, 5, 3, 2, 1, 3, 6, 4, 7] # 5
[10, 9, 8, 7, 6, 5, 4, 3, 2, 3] # 9
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # 19
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1] # 10
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1] # 9
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1] # 9
[1, 1, 1, 0, 0, 0, 0, 0, 0, 2] # 9

You can consider the increasing/decreasing state of grouped identical values, and keep track of the previous length. The complexity is O(n) with a single pass on the input:
from itertools import groupby
def sequence(lst):
max_len = 0
prev = float('nan')
prev_len = 0
running_len = 0
increasing = False
for k, g in groupby(lst):
L = len(list(g))
if k < prev:
running_len += L
increasing = False
else:
if increasing:
running_len += L
else:
max_len = max(max_len, running_len)
running_len = L + prev_len
increasing = True
prev = k
prev_len = L
return max(max_len, running_len)
sequence([10,9,8,10,6,5,4,3,2,3])
Output: 7
NB. itertools.groupby is just a convenience to avoid having to handle the successive identical values. But you don't have to use it and can track those yourself.
Other examples:
sequence([10, 9, 8, 10, 6, 5, 4, 3, 2, 3])
#7 * * * * * * *
sequence([4, 5, 3, 2, 1, 3, 6, 4, 7])
#5 * * * * *
sequence([10, 9, 8, 7, 6, 5, 4, 3, 2, 3])
#9 * * * * * * * * *
sequence([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
#10 * * * * * * * * * *
sequence([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#10 * * * * * * * * * *
sequence([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
#19 * * * * * * * * * * * * * * * * * * *
sequence([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
#10 * * * * * * * * * *
sequence([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
#10 * * * * * * * * * *
sequence([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])
#9 * * * * * * * * *
sequence([1, 1, 0, 0, 0, 0, 0, 0, 0, 1])
#9 * * * * * * * * *
sequence([1, 1, 1, 0, 0, 0, 0, 0, 0, 2])
#9 * * * * * * * * *
refactoring the code
This is the exact same logic as above but tests have been combined, intermediate variables removed, etc.
from itertools import groupby
def sequence(lst):
max_len = prev_len = running_len = 0
prev = float('nan')
decreasing = False
for k, g in groupby(lst):
if k < prev:
decreasing = True
elif decreasing:
max_len = max(max_len, running_len)
running_len = prev_len
decreasing = False
prev = k
prev_len = len(list(g))
running_len += prev_len
return max(max_len, running_len)

Related

Python algorithm Golomb

program must find Sum(n)
G1+G2+G3+...+Gn
I wrote the program in python. It is working when I run. but if i send program to testing in site, 4 tests success, 5-th fails
my code:
import math
n = int(input())
k = 0
result = 0
while n > 0:
k += 1
num = (math.floor(k / 2) + 1)
sum_of_nums = num * k
n -= num
result += sum_of_nums
result += n * k
print(int(result))
first 25 numbers: [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, ...]
1 = (floor(k / 2) + 1) * k k = 1
2 + 2 = (floor(k / 2) + 1) * k k = 2
3 + 3 = (floor(k / 2) + 1) * k k = 3
4 + 4 + 4 = (floor(k / 2) + 1) * k k = 4
...
result += n * k. this code removes from result (n may be < 0 or =0)
Here is a simple iterative way to generate the Golomb sequence:
n = 25
ns = [1]
for i in range(n-1):
ns.append(1+ns[i+1-ns[ns[-1]-1]])
ns
Output:
>>> ns
[1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9]
>>> sum(ns)
140
NB. I think your example in incorrect

Use clipping or zero padding

Write a function that accept an array of N elements, and converts it into a 9-element array. Use clipping or zero padding to obtain the desired output. Care should be taken to place the middle element of the input array as the middle element of output array.
Examples
A = [ 2, 5, 1 ] # N == 3, so zero padding to make the output size 9
output = [0, 0, 0, 2, 5, 1, 0, 0, 0]
A = [ 2, 3, 7, 4 ]
output = [0, 0, 2, 3, 7, 4, 0, 0, 0] or [0, 0, 0, 2, 3, 7, 4, 0, 0] # Because there are two middle elements if N is even
A = [ 1, 3, 3, 4, 7, 6, 4, 3, 3, 2, 2, 2, 1 ] # N == 13, so remove 4 elements (2 at the right, 2 at the left) to make the output size 9
output = [3, 4, 7, 6, 4, 3, 3, 2, 2]
A = [ 1, 2, 3, 3, 4, 7, 6, 4, 3, 3, 2, 2, 2, 1 ]
output = [3, 3, 4, 7, 6, 4, 3, 3, 2] or [3, 4, 7, 6, 4, 3, 3, 2, 2]
Imagine you want an output list of n=9 with zeros padding and an input list A.
Then treat the thing as separate problems:
If len(A) == n return A
If len(A) > n then return the middle n elements of A.
Else return a list that starts with (n - len(A)) // 2 zeros, then A, then ends with n - (n - len(A)) // 2 - len(A) zeros (however much is left over from n elements).
E.g.
def fulllist(A, n):
if len(A) == n:
return A
if len(A) > n:
start = (len(A) - n) // 2
return A[start:start+n]
if len(A) < n:
leftpad = [0] * ((n - len(A)) // 2)
rightpad = [0] * (n - len(leftpad) - len(A))
return leftpad + A + rightpad
This gives the following as output:
>>> fulllist([2, 5, 1], 9)
[0, 0, 0, 2, 5, 1, 0, 0, 0]
>>> fulllist([2, 3, 7, 4], 9)
[0, 0, 2, 3, 7, 4, 0, 0, 0]
>>> fulllist([1, 3, 3, 4, 7, 6, 4, 3, 3, 2, 2, 2, 1], 9)
[3, 4, 7, 6, 4, 3, 3, 2, 2]
>>> fulllist([1, 2, 3, 3, 4, 7, 6, 4, 3, 3, 2, 2, 2, 1], 9)
[3, 3, 4, 7, 6, 4, 3, 3, 2]
The above function was written to match the pseudocode, but it would be better to alter it slightly, eliminating case 1. by treating it as a special case of 2. or 3.. That way the code is shorter, and more important, the function always returns a brand new list in every case instead of sometimes returning a reference to the input list, which could get confused in subsequent code and lead to some bugs. I chose to merge 1. with 2.
def fulllist(A, n):
if len(A) >= n:
start = (len(A) - n) // 2
return A[start:start+n]
else:
leftpad = [0] * ((n - len(A)) // 2)
rightpad = [0] * (n - len(leftpad) - len(A))
return leftpad + A + rightpad
Or you can shorten the code further by abusing the fact that [0] * -1 == [].
def fulllist(A, n):
leftpad = [0] * ((n - len(A)) // 2)
rightpad = [0] * (n - len(leftpad) - len(A))
start = (len(A) - n) // 2 if len(A) > n else 0
contents = A[start:start+n]
return leftpad + contents + rightpad
Although that may be going too far for some and it might make the code harder to understand. I like it though.
you can try something like this:
def padding(arr,n):
diff = abs(n - len(arr))
start, end = int(diff / 2), diff - int(diff / 2)
if len(arr) < n:
for i in range(start):
arr.insert(0,0)
for i in range(end):
arr.append(0)
else:
for i in range(start):
arr.pop(0)
for i in range(end):
arr.pop(-1)
return arr
A = [ 1, 3, 3, 4, 7, 6, 4, 3, 3, 2, 2, 2, 1 ]
print(padding(A,9))
Try -
def padding(l1):
flag = 0
while len(l1)>=9:
if len(l1) == 9:
return l1
if flag %2== 0:
l1.pop()
else:
l1.pop(0)
flag+=1
if len(l1)<9:
iterations = (9- len(l1))//2
for _ in range(iterations):
l1.insert(0,0)
l1.append(0)
if iterations % 2 == 0:
l1.append(0)
return l1
padding(A)

Backtracking algorithm for Sudoku in python

I'm trying to use backtracking to make a Sudoku solver. When I work through the code by hand it works perfectly but when I run it I get empty cells in my return solution. I've spent such a ridiculous amount of time trying to fix it but I can't even figure out where exactly its going wrong. I'm fairly certain my function for checking cell solutions is correct. Here's the code:
import numpy as np
board = np.array([[0, 0, 0, 8, 4, 0, 0, 0, 3],
[0, 7, 5, 0, 0, 0, 0, 0, 0],
[0, 4, 3, 0, 0, 6, 0, 0, 0],
[0, 0, 7, 0, 0, 8, 4, 9, 0],
[0, 0, 0, 9, 3, 1, 0, 0, 0],
[0, 5, 2, 7, 0, 0, 8, 0, 0],
[0, 0, 0, 2, 0, 0, 3, 4, 0],
[0, 0, 0, 0, 0, 0, 6, 2, 0],
[2, 0, 0, 0, 7, 3, 0, 0, 0]])
#check if input is viable solution for a cell
def isSolution(row, col, n):
#return 0 for false (not a possible solution)
#return 1 for true (possible solution)
a = 0
for i in range(0,9):
if(board[row,i] == n):
a += 1
if(board[i,col] == n):
a += 1
h = (1 - (2 * ((row % 3) != 0)))
i = (1 - (2 * ((col % 3) != 0)))
j = (2 - (row % 3)**2)
k = (2 - (col % 3)**2)
if(board[row + h, col + i] == n):
a += 1
elif(board[row + h, col + k] == n):
a += 1
elif(board[row + j, col + i] == n):
a += 1
elif(board[row + j, col + k] == n):
a += 1
if(a == 0):
return 1
else:
return 0
def solve():
for row in range(0, 9):
for col in range(0,9):
if(board[row,col] == 0):
for n in range(1, 10):
if(isSolution(row, col, n) == 1):
board[row,col] = n
print(board)
solve()
board[row,col] = 0
return
#main
solve()
Please help if you can I'm trying to get better at python and feel like I've hit a wall here
Expanding comment of Thierry Lathuille
def solve():
for row in range(0, 9):
for col in range(0,9):
if(board[row,col] == 0):
for n in range(1, 10):
if(isSolution(row, col, n) == 1):
board[row,col] = n
# print(board)
if (board != 0).all():
raise StopIteration
solve()
board[row,col] = 0
return
#main
try:
solve()
except StopIteration:
print(board)

Sum of the cells around a single cell in a matrix with python

I have two matrices: in "matrix" there are 0 and 1. In "matrix_2" I would like to sum the cells around the single cell.
For example:
matrix =
[[0 0 0 0 0 1 1 0 0 0]
[0 0 0 0 0 1 1 0 0 0]
[1 1 1 0 1 1 0 1 0 0]
[0 0 1 1 1 1 0 1 0 0]
[0 0 1 1 1 1 1 0 0 0]
[0 0 0 0 1 0 0 0 0 0]]
And
matrix_2 =
[[0 0 0 0 0 4 4 0 0 0]
[0 0 0 0 0 6 6 0 0 0]
[2 4 4 0 6 6 0 3 0 0]
[0 0 6 8 8 7 0 3 0 0]
[0 0 4 7 7 6 4 0 0 0]
[0 0 0 0 4 0 0 0 0 0]]
In this case, matrix_2 computes the sum of the cell and the cells immediately around.
matrix_2 = np.zeros((y_segment, x_segment), dtype=int)
for y_matrix in xrange(0, y_segment, 1):
for x_matrix in xrange(0, x_segment, 1):
if matrix[y_matrix][x_matrix] != 0:
if 0 < x_matrix < x_segment - 1 and 0 < y_matrix < y_segment - 1:
# print "y_matrix: " + str(y_matrix) + ", x_matrix: " + str(x_matrix)
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix-1][x_matrix] + \
matrix[y_matrix-1][x_matrix-1] + matrix[y_matrix-1][x_matrix+1] + \
matrix[y_matrix+1][x_matrix-1] + matrix[y_matrix+1][x_matrix] + \
matrix[y_matrix+1][x_matrix+1] + matrix[y_matrix][x_matrix-1] + \
matrix[y_matrix][x_matrix+1]
if x_matrix == 0 and y_matrix == 0: # 1
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix][x_matrix+1] + \
matrix[y_matrix+1][x_matrix+1] + matrix[y_matrix+1][x_matrix]
if x_matrix == 0 and y_matrix == y_segment-1: # 10
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix - 1][x_matrix] + \
matrix[y_matrix - 1][x_matrix + 1] + matrix[y_matrix][x_matrix + 1]
if x_matrix == x_segment-1 and y_matrix == y_segment-1: # 12
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix - 1][x_matrix] + \
matrix[y_matrix][x_matrix - 1] + matrix[y_matrix - 1][x_matrix - 1]
if x_matrix == x_segment-1 and y_matrix == 0: # 3
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix][x_matrix-1] + \
matrix[y_matrix + 1][x_matrix - 1] + matrix[y_matrix + 1][x_matrix]
if x_matrix == 0 and y_matrix != 0 and y_matrix != y_segment-1:
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix-1][x_matrix] + \
matrix[y_matrix-1][x_matrix+1] + matrix[y_matrix+1][x_matrix] + \
matrix[y_matrix+1][x_matrix+1] + matrix[y_matrix][x_matrix+1]
if x_matrix == x_segment-1 and y_matrix != 0 and y_matrix != y_segment-1:
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix - 1][x_matrix] + \
matrix[y_matrix - 1][x_matrix - 1] + matrix[y_matrix + 1][x_matrix] + \
matrix[y_matrix + 1][x_matrix - 1] + matrix[y_matrix][x_matrix - 1]
if y_matrix == 0 and x_matrix != 0 and x_matrix != x_segment-1:
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix][x_matrix + 1] + \
matrix[y_matrix][x_matrix - 1] + matrix[y_matrix + 1][x_matrix] + \
matrix[y_matrix + 1][x_matrix + 1] + matrix[y_matrix + 1][x_matrix - 1]
if y_matrix == y_segment-1 and x_matrix != 0 and x_matrix != x_segment-1:
matrix_2[y_matrix][x_matrix] = matrix[y_matrix][x_matrix] + matrix[y_matrix][x_matrix + 1] + \
matrix[y_matrix][x_matrix - 1] + matrix[y_matrix - 1][x_matrix] + \
matrix[y_matrix - 1][x_matrix + 1] + matrix[y_matrix - 1][x_matrix - 1]
But know I would like to compute the sum of the 9x9 cells around my cell.
Is there a function for that? Because for sure my code is not well written and it will be very long. Thank you.
Using convolution
kernel
size=3
kernel = np.ones((size,size))
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
calculation
result = signal.convolve(matrix, kernel, method='direct').astype(int)
array([[0, 0, 0, 0, 0, 1, 2, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 2, 4, 4, 2, 0, 0, 0],
[1, 2, 3, 2, 2, 4, 6, 6, 3, 1, 0, 0],
[1, 2, 4, 4, 5, 6, 6, 6, 3, 2, 0, 0],
[1, 2, 5, 6, 8, 8, 7, 6, 3, 2, 0, 0],
[0, 0, 2, 4, 7, 7, 6, 4, 2, 1, 0, 0],
[0, 0, 1, 2, 4, 4, 4, 2, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0]])
trimming
This adds the outer layers too, so you need to trim these
result_trimmed = result[(size-1)//2:-(size-1)//2,(size-1)//2:-(size-1)//2]
array([[0, 0, 0, 0, 2, 4, 4, 2, 0, 0],
[2, 3, 2, 2, 4, 6, 6, 3, 1, 0],
[2, 4, 4, 5, 6, 6, 6, 3, 2, 0],
[2, 5, 6, 8, 8, 7, 6, 3, 2, 0],
[0, 2, 4, 7, 7, 6, 4, 2, 1, 0],
[0, 1, 2, 4, 4, 4, 2, 1, 0, 0]])
Pure python
This does the same as the scipy alternative
from itertools import product
def convolution(matrix, size):
if size % 2 != 1:
ValueError('`size` must be an odd integer')
h, w = (len(matrix), max(len(row) for row in matrix), )
# print(w, h)
result = [[0] * w for _ in range(h)]
for x, y in product(range(w), range(h)):
# print(x, y)
y_min, y_max = max(0, y - size // 2), min(h, y + size // 2 + 1)
x_min, x_max = max(0, x - size // 2), min(w, x + size // 2 + 1)
# print(matrix[y][x], x_min, x_max, y_min, y_max)
rows = matrix[y_min: y_max]
result[y][x] = sum(sum(row[x_min: x_max]) for row in rows)
return result
OP's matrix2
If that is the result the OP expects, this can be achieved by
numpy method:
assuming matrix is the np.array form of the initial matrix
np.where(matrix != 0, result_trimmed, 0)
array([[0, 0, 0, 0, 0, 4, 4, 0, 0, 0],
[0, 0, 0, 0, 0, 6, 6, 0, 0, 0],
[2, 4, 4, 0, 6, 6, 0, 3, 0, 0],
[0, 0, 6, 8, 8, 7, 0, 3, 0, 0],
[0, 0, 4, 7, 7, 6, 4, 0, 0, 0],
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0]])
native python
Add 2 lines in the inner loop:
...
for x, y in product(range(w), range(h)):
if matrix[y][x] == 0:
continue
...
Please correct me if I'm wrong but for
cell
matrix[r][c]
we're adding following values
r-1,c-1
r-1,c
r-1,c+1
r,c-1
r,c
r,c+1
r+1,c-1
r+1,c
r+1,c+1
I have written following code please check.
def get_sum_of_cells_around(matrix,num_rows,num_cols):
def valid(r,c):
if r<0 or r>=num_rows or c<0 or c>=num_cols:
return False
return True
result = []
for r in range(0,num_rows):
row = []
for c in range(0,num_cols):
value = 0
for i in range(-1,2):
for j in range(-1,2):
if valid(r+i,c+j):
value+=matrix[r+i][c+j]
row.append(value)
result.append(row)
return result

Manipulating two lists

I am trying to write the code such that, if n[i+1] is not equal to n[i], then the xmove would be m[i+1] - m[i] else, if n[i+1] is equal to n[i], then the xmove at that index is 0, while this continues till n[i+1] is not equal to n[i], then the xmove at that point is the difference between the first and the last m index while the equality condition exists. Same goes for the ymove. The output would be this
xmove = [1, 1, 0, 0, 2, 1]
ymove = [1, 1, 0, 0, 5, 1]
Thank you
m = [1, 2, 3, 4, 5, 6, 7]
n = [1, 2, 3, 3, 3, 8, 9]
xmove = []
ymove = []
first = []
Sum = []
for i in range(len(n)-1):
if n[i+1] == n[1]:
messi = 0
xmove.append(messi)
first.append(n[i])
Sum.append(1)
liit = sum(Sum)
u = first[0] - liit
xmove.append(u)
else:
u = n[i+1] - n[i]
xmove.append(u)
Thanks for the clarification in comments -- what about something like:
m = [1, 2, 3, 4, 5, 6, 7]
n = [1, 2, 3, 3, 3, 8, 9]
ind = range(len(n))
xeq = set()
yeq = set()
xmove = []
ymove = []
for (i,j) in zip(ind[:-1], ind[1:]):
# Handle xmove (based on n) ------------------------------------------------
if n[i] != n[j]:
if not xeq:
xe = m[j] - m[i]
else:
xe = m[max(xeq)] - m[min(xeq)]
xeq.clear()
else:
xe = 0
xeq.update([i,j])
xmove.append(xe)
# Handle ymove (based on m) ------------------------------------------------
if m[i] != m[j]:
if not yeq:
ye = n[j] - n[i]
else:
ye = n[max(yeq)] - n[min(yeq)]
yeq.clear()
else:
ye = 0
yeq.update([i,j])
ymove.append(ye)
print xmove, xmove == [1, 1, 0, 0, 2, 1]
print ymove, ymove == [1, 1, 0, 0, 5, 1]
Output:
[1, 1, 0, 0, 2, 1] True
[1, 1, 0, 0, 5, 1] True
Or using a function:
def get_moves(a, b):
ind = range(len(a))
eq = set()
moves = []
for (i,j) in zip(ind[:-1], ind[1:]):
if b[i] != b[j]:
e = a[j] - a[i] if not eq else a[max(eq)] - a[min(eq)]
eq.clear()
else:
e = 0
eq.update([i,j])
moves.append(e)
return moves
m = [1, 2, 3, 4, 5, 6, 7]
n = [1, 2, 3, 3, 3, 8, 9]
xmove = get_moves(m,n)
ymove = get_moves(n,m)
print xmove, xmove == [1, 1, 0, 0, 2, 1]
print ymove, ymove == [1, 1, 0, 0, 5, 1]
Output:
[1, 1, 0, 0, 2, 1] True
[1, 1, 0, 0, 5, 1] True

Categories