29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
def get_row(lst, x):
|
|
"""
|
|
You are given a 2 dimensional data, as a nested lists,
|
|
which is similar to matrix, however, unlike matrices,
|
|
each row may contain a different number of columns.
|
|
Given lst, and integer x, find integers x in the list,
|
|
and return list of tuples, [(x1, y1), (x2, y2) ...] such that
|
|
each tuple is a coordinate - (row, columns), starting with 0.
|
|
Sort coordinates initially by rows in ascending order.
|
|
Also, sort coordinates of the row by columns in descending order.
|
|
|
|
Examples:
|
|
get_row([
|
|
[1,2,3,4,5,6],
|
|
[1,2,3,4,1,6],
|
|
[1,2,3,4,5,1]
|
|
], 1) == [(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)]
|
|
get_row([], 1) == []
|
|
get_row([[], [1], [1, 2, 3]], 3) == [(2, 2)]
|
|
"""
|
|
result = []
|
|
for row_idx, row in enumerate(lst):
|
|
row_matches = []
|
|
for col_idx, val in enumerate(row):
|
|
if val == x:
|
|
row_matches.append((row_idx, col_idx))
|
|
row_matches.sort(key=lambda coord: coord[1], reverse=True)
|
|
result.extend(row_matches)
|
|
return result |