Python Forum
Matrix understanding in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix understanding in Python
#7
Task is described as:

Quote:Given a 2D grid, each cell is either a zombie 1 or a human 0. Zombies can turn adjacent (up / down / left / right) human beings into zombies every hour. Find out how many hours does it take to infect all humans?

So subtasks can be defined as:

- find zombies
- infect neighbours adjacent to zombies
- repeat until all are zombies

You must separate finding zombies and infecting neighbours otherwise you will get incorrect result (how to separate existed zombies and newly created zombies).

Finding zombies is relatively easy. As we need to infect neighbours we should use indices to identify the location. For achieving that we can 'iterate over enumerated rows and for every enumerated item in row we return row-item number pair if item value is 1'

zombies = ((i, j) for i, row in enumerate(m) for j, item in enumerate(row) if item == 1)
Now we have locations (row, col) of zombies.

How to find neighbours?

If we have coordinate of zombie, then to get previous (to the left of zombie) we should change coordinates (0, -1), to get next (to the right of zombie) we shold change (0, 1), for row above (-1, 0) and row below (1, 0).

So we calculate coordinates of infected cells and change their values. What we should take into consideration: we shouldn't allow negative values (it will start counting from end); we will get IndexError for values in first and last row and column. Both of these are easily mitigated.

If we have 'infected' neighbours we should check whether humans are still present and if so repeat.
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,399 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