neighbors

#309
Raw
Author
winny
Created
Dec. 20, 2020, 10:26 p.m.
Expires
Never
Size
939 bytes
Hits
411
Syntax
None
Private
No
$ ed neighbors.py
271
/==
            if x == pt.x and x == pt.y:
s,and x,and y,p
            if x == pt.x and y == pt.y:
wq
271
$ python
Python 3.9.0 (default, Oct  8 2020, 15:55:37)
[Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from neighbors import *
>>> neighbors(Point(1,2))
<generator object neighbors at 0xb34dc098>
>>> from pprint import pprint
>>> pprint(list(_))
[Point(x=0, y=1),
 Point(x=0, y=2),
 Point(x=0, y=3),
 Point(x=1, y=1),
 Point(x=1, y=3),
 Point(x=2, y=1),
 Point(x=2, y=2),
 Point(x=2, y=3)]
>>>
$ cat neighbors.py
from collections import namedtuple

Point = namedtuple('Point', ('x', 'y'))

def neighbors(pt):
    for x in range(pt.x-1, pt.x+2):
        for y in range(pt.y-1, pt.y+2):
            if x == pt.x and y == pt.y:
                continue
            yield Point(x=x, y=y)
$