Python Forum
Matrix understanding in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix understanding in Python
#17
I don't think that following solution is 'fancier' than jefsummer's, it's just different possibility.

As this should be a learning process more throughout explanation than usually:

Pseudocode:

# define coordinates offsets
# define hours counter

# while there is human:
    # find all zombies coordinates
    # find all zombie neighbours coordinates with brute-force using coordinates offsets
    # for every zombie neighbour coordinate:
         # try set value to 1
         # except IndexError - continue
    # add one to hours
Following assumes that m is the matrix which consists initial state.

# define coordinates offsets
Relative changes in coordinates to find up/down/right/left:

offsets = [(0, -1), (0, 1), (1, 0), (-1, 0)]
# define hours counter
Set hours to zero

hours = 0
# while there is human
Find if there is 0 in matrix; for that we have to flatten matrix; I used set but list would do as well

while 0 in set(item for row in m for item in row):
# find all zombies coordinates
We need to find coordinates of items which value is 1: 'give me row and item index for every row and every item in row if item is equal to 1'. In order to get index I used enumerate:

zombies = ((i, j) for i, row in enumerate(m) for j, item in enumerate(row) if item == 1)
# find all zombie neighbours coordinates with brute-force using coordinates offsets
It's too complicated and brute force for my liking but it gets job done. 'give me row and item index adjusted with offset for every zombie coordinate if both row and item adjusted values are not less than zero' (negative values are not IndexErrors but start indexing from end and we don't want that; with Python 3.8 nice walrus should be done)

infected = [(i+row, j+col) for (i, j) in zombies for (row, col) in offsets if all([0 <= (i+row), 0 <=(j+col)])]
# for every zombie neighbour coordinate
Iterate through zombie neighbours coordinates.

for (row, column) in infected:
# try set value to 1
As coordinates contain out of bounds indices (last column and last row) then we try to change value

try:
    m[row][column] = 1
# except IndexError - continue
If we have zombies in last column/row and encounter IndexError - ignore and continue:

except IndexError:
    continue
# add one to hours
If changes are made add 1 hour

hours += 1
Everthing together:

offsets = [(0, -1), (0, 1), (1, 0), (-1, 0)]
hours = 0

while 0 in set(item for row in m for item in row):
    zombies = ((i, j) for i, row in enumerate(m) for j, item in enumerate(row) if item == 1)
    infected = [(i+row, j+col) for (i, j) in zombies for (row, col) in offsets if all([0 <= (i+row), 0 <=(j+col)])]
    for (row, column) in infected:
        try:
            m[row][column] = 1
        except IndexError:
            continue
    hours += 1
Of course, we can make 'infected' comprehension even more complicated and avoid try...except altogether (value check must be then 0 <= row indice < len(m); 0 <= col indice < len(m[0]))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Matrix understanding in Python - by RavCOder - Nov-13-2019, 11:35 AM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 11:51 AM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 12:02 PM
RE: Matrix understanding in Python - by jefsummers - Nov-13-2019, 12:31 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 12:36 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 01:43 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 02:11 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 02:17 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 02:27 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 02:36 PM
RE: Matrix understanding in Python - by perfringo - Nov-13-2019, 03:02 PM
RE: Matrix understanding in Python - by RavCOder - Nov-13-2019, 03:15 PM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 07:21 AM
RE: Matrix understanding in Python - by RavCOder - Nov-14-2019, 08:44 AM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 12:10 PM
RE: Matrix understanding in Python - by jefsummers - Nov-14-2019, 12:37 PM
RE: Matrix understanding in Python - by perfringo - Nov-14-2019, 02:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,349 May-15-2019, 01:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020