Skip to content

Instantly share code, notes, and snippets.

@alekseyl1992
Created November 2, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alekseyl1992/58c26ccc5861e44b77c494cdbf64c740 to your computer and use it in GitHub Desktop.
Save alekseyl1992/58c26ccc5861e44b77c494cdbf64c740 to your computer and use it in GitHub Desktop.
lab.py
def get_area(matrix, start_row, start_col):
max_area = 0
height = 0
prev_line_width = 0
for j in range(start_row, len(matrix)):
row = matrix[start_row]
height += 1
current_line_width = 0
for i in range(start_col, len(row)):
if matrix[j][i] == 't':
current_line_width += 1
else:
break
# width decreased, calc current area
# and continue with smaller width
if current_line_width < prev_line_width:
area = prev_line_width * height
if area > max_area:
max_area = area
prev_line_width = current_line_width
# may be width hasn't decreased at all
# check current area here as well
area = prev_line_width * height
if area > max_area:
max_area = area
return max_area
def get_largest_area(matrix):
result_row = 0
result_col = 0
result_area = 0
# for all start points
for row_id, row in enumerate(matrix):
for col_id, value in enumerate(row):
if value == 't':
area = get_area(matrix, row_id, col_id)
if area > result_area:
result_area = area
result_row = row_id
result_col = col_id
return result_row, result_col, result_area
def main():
matrix = [
['t', 'f', 'f', 'f', 'f'],
['f', 'f', 'f', 'f', 't'],
['f', 't', 't', 'f', 't'],
['f', 't', 't', 'f', 'f'],
['f', 't', 't', 'f', 'f']]
# print input matrix
for row in matrix:
for col in row:
print(col, end=' ')
print('')
row, col, area = get_largest_area(matrix)
# print results
print(row + 1, col + 1, area)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment