From 730915136413554ad43fbfc9d7e4f766291a4ec0 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Wed, 26 Aug 2020 14:35:58 -0400 Subject: python solution for challenge 75 task 2 first draft --- challenge-075/walt-mankowski/python/ch-2.py | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-075/walt-mankowski/python/ch-2.py (limited to 'challenge-075/walt-mankowski/python/ch-2.py') diff --git a/challenge-075/walt-mankowski/python/ch-2.py b/challenge-075/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..efd720f539 --- /dev/null +++ b/challenge-075/walt-mankowski/python/ch-2.py @@ -0,0 +1,49 @@ +from sys import argv +import numpy as np + +def print_hist(hist, a, rows, cols): + for row in range(rows-1, -1, -1): + print(row+1, end='') + for col in range(cols): + print(f" {'#' if hist[row][col] == 1 else ' '}", end='') + print() + + print('-', end='') + print(" -" * cols) + + print(' ', end='') + for i in range(cols): + print(f' {a[i]}', end='') + print() + print() + +a = [int(x) for x in argv[1:]] + +# build the histogram +rows = max(a) +cols = len(a) +hist = np.zeros([rows, cols]) +for row in range(rows): + for col in range(cols): + if row < a[col]: + hist[row][col] = 1 +print_hist(hist, a, rows, cols) + +best_area = 0 +best_height = -1 +best_width = -1 + +for height in range(1, rows+1): + for width in range(1, cols+1): + area = height * width + if area <= best_area: + continue + for r0 in range(row-height+1): + for c0 in range(0, cols-width+1): + if sum(sum(hist[r0:r0+height,c0:c0+width])) == area: + best_area = area + best_height = height + best_width = width + +print(f'The best rectangle is {best_height} x {best_width} for an aea of {best_area}.') + -- cgit From 59f42083ee3c1c04bf9db831430d5b9d94c446a2 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Wed, 26 Aug 2020 14:45:02 -0400 Subject: use dtype of bool for the histogram --- challenge-075/walt-mankowski/python/ch-2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'challenge-075/walt-mankowski/python/ch-2.py') diff --git a/challenge-075/walt-mankowski/python/ch-2.py b/challenge-075/walt-mankowski/python/ch-2.py index efd720f539..fb8d13b9c3 100644 --- a/challenge-075/walt-mankowski/python/ch-2.py +++ b/challenge-075/walt-mankowski/python/ch-2.py @@ -5,7 +5,7 @@ def print_hist(hist, a, rows, cols): for row in range(rows-1, -1, -1): print(row+1, end='') for col in range(cols): - print(f" {'#' if hist[row][col] == 1 else ' '}", end='') + print(f" {'#' if hist[row][col] else ' '}", end='') print() print('-', end='') @@ -22,11 +22,11 @@ a = [int(x) for x in argv[1:]] # build the histogram rows = max(a) cols = len(a) -hist = np.zeros([rows, cols]) +hist = np.zeros([rows, cols], dtype=bool) for row in range(rows): for col in range(cols): if row < a[col]: - hist[row][col] = 1 + hist[row][col] = True print_hist(hist, a, rows, cols) best_area = 0 -- cgit From 4c7aa6d402ab4dbf985cbf9e2271ee9d7a2df6f5 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Wed, 26 Aug 2020 20:58:43 -0400 Subject: fixed typo and got output to match the perl version --- challenge-075/walt-mankowski/python/ch-2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-075/walt-mankowski/python/ch-2.py') diff --git a/challenge-075/walt-mankowski/python/ch-2.py b/challenge-075/walt-mankowski/python/ch-2.py index fb8d13b9c3..af82ba5642 100644 --- a/challenge-075/walt-mankowski/python/ch-2.py +++ b/challenge-075/walt-mankowski/python/ch-2.py @@ -45,5 +45,5 @@ for height in range(1, rows+1): best_height = height best_width = width -print(f'The best rectangle is {best_height} x {best_width} for an aea of {best_area}.') +print(f'The best rectangle is {best_height} x {best_width} for an area of {best_area}') -- cgit