This commit is contained in:
osiris account 2023-03-12 15:32:23 -07:00
parent 0696b08d04
commit 314bdf533e
94 changed files with 11 additions and 7 deletions

View file

@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/

View file

@ -0,0 +1,4 @@
run:
python3 find_glob_boundary.py
.PHONY: run

View file

@ -0,0 +1,103 @@
#!/usr/bin/env python3
import sys
def _find_left_boundary(glob):
"""
Given a glob, search for left boundary, returning a tuple with
boundary values or None if boundary was not found.
"""
row, ncells = 0, 0
while row < len(glob):
col = 0
while col < len(glob[row]):
ncells = ncells + 1
if glob[row][col] == 1:
return (row, col), ncells
else:
col = col + 1
row = row + 1
return None, ncells
def _find_right_boundary(glob):
"""
Given a glob, search for right boundary, returning a tuple with
boundary values or None if boundary was not found.
"""
row, ncells = len(glob) - 1, 0
while row > 0:
col = len(glob[row]) - 1
while col > 0:
ncells = ncells + 1
if glob[row][col] == 1:
return (row, col), ncells
else:
col = col - 1
row = row - 1
return None, ncells
def _print_results(n_cells, top_left, bottom_right):
"""
Print results in the desired format.
"""
print("Cell Reads : {}".format(n_cells))
print("Boundary:")
print(" Top Left: {}".format(top_left))
print(" Bottom Right: {}".format(bottom_right))
def main(glob):
"""
Grab a NxN glob and print out the results.
"""
print("\nTesting {}".format(glob))
if len(glob[0]) != len(glob):
print("Matrix needs to be NxN.")
else:
top_left, nleft = _find_left_boundary(glob)
if top_left:
bottom_right, nright = _find_right_boundary(glob)
if not bottom_right:
bottom_right = top_left
_print_results(nleft + nright, top_left, bottom_right)
else:
print("Could not find left boundary, maybe there is no 1s in your glob?")
if __name__ == "__main__":
globs = []
globs.append([[0, 1]])
globs.append([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
globs.append([[0, 0], [0,0]])
globs.append([[1, 0, 0], [0, 0, 0], [0, 0, 0]])
globs.append([[0, 1, 0], [0, 0, 0], [0, 0, 0]])
globs.append([[0, 1, 1], [0, 1, 1], [0, 0, 0]])
for glob in globs:
main(glob)