diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
| commit | 710837b2a38b5fbd4404f8d9be06ef368af0d366 (patch) | |
| tree | 8796e6c6c7be4f7f935e68f3f0bff99bed04eb57 /challenge-126/stuart-little/python/ch-2.py | |
| parent | 637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3 (diff) | |
| parent | 73470591f10490d6385145990c49825266f80b2b (diff) | |
| download | perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.gz perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.bz2 perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-126/stuart-little/python/ch-2.py')
| -rwxr-xr-x | challenge-126/stuart-little/python/ch-2.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/python/ch-2.py b/challenge-126/stuart-little/python/ch-2.py new file mode 100755 index 0000000000..971e35a639 --- /dev/null +++ b/challenge-126/stuart-little/python/ch-2.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +# run <script> + +def nbrs(mat, i, j): + return [(i+x,j+y) for x in range(-1,2) for y in range(-1,2) if (x != 0 or y != 0) and (0 <= i+x < len(mat)) and (0 <= j+y < len(mat[0]))] + +inptStr="""x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +""" +inpt = [s.split() for s in inptStr.splitlines()] + +for i in range(len(inpt)): + for j in range(len(inpt[0])): + print(inpt[i][j] if (inpt[i][j] == 'x') else len(list(filter(lambda p: inpt[p[0]][p[1]] == 'x', nbrs(inpt,i,j)))), end=" ") + print() |
