I'm facing the following problem: I have a row of cells. Some cells are available, some are not. Every cell is associated with a
cell_id. I want to determine the length of cell. This length of a cell
c is defined as the length of the chain
c, c1, c2, c2, ... where
c1 has the next
c2 has the next
c1, and so forth.
This length is finite, since at some point there won't be the cell
c(n+1) contained in the row of cells.
This is my current function:
def get_cell_length(cell, row): if cell not in row: return 0 length = 1 next_cell_id = get_next_cell_id(cell.get_attribute('id')) next_cell_found = True while next_cell_found: next_cell_found = False for next_cell in row: if next_cell.get_attribute('id') == next_cell_id: length = length + 1 cell = next_cell next_cell_id = get_next_cell_id(cell.get_attribute('id')) next_cell_found = True return length
get_next_cell_idis given the id of a cell
cand returns the next
The input could look like this:
row = [("cell1", 1), ("cell2", 2), ("cell3", 3), ("cell5", 5), ("cell6", 6), ("cell7", 7), ("cell10", 10)]
In this case
cell.get_attribute('id') should just return
get_next_cell_id(id) would just return
id + 1.
The expected output would be:
get_cell_length(("cell1", 1), row) -> 3 get_cell_length(("cell2", 2), row) -> 2 get_cell_length(("cell3", 3), row) -> 1 get_cell_length(("cell6", 5), row) -> 2 get_cell_length(("cell10", 10), row) -> 1
But the function does not look clean to me and seem slow as well. Any help is welcome!