Python Forum
Matrix understanding in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix understanding in Python
#15
Take a pencil and paper and try to visualise.

Matrix has rows and columns. Rows are lists inside lists and items in those lists are columns (keep in mind that Python uses zero indexing):

m = [[1, 2, 3],    # row with index 0 
     [4, 5, 6],    # row with index 1
     [7, 8, 9]]    # row with index 2
Columns are: [1, 4, 7] (index 0), [2, 5, 8] (index 1) and [3, 6, 9] (index 2).

So m[0][0] is 1 and m[1][1] is 5.

If we want to find neighbours up, down, left, right and have 'start coordinate' then:

- for up deduct one row
- for down add one row
- for left deduct one column
- for right add one column

So for position m[1][1] which is 5 we should perform following transactions:

- up, start [1][1], change [-1][0], [1-1, 1-0] -> [0][1] -> row zero column one -> 2
- down, start [1][1], change [1][0], [1+1][[1+0] -> [2][1] -> row two column one -> 8
- left, start [1][1], change [0, -1], [1+0][1-1] -> [1][0] -> row one column zero -> 4
- right, start [1][1], change [0, 1], [1+0][1+1] -> [1][2] -> row one column two -> 6

So far so good. However, we have corner cases:

[0][0] - if we deduct row or column we will get [-1] which is not IndexError in Python but index of last element. So while calculating neighbours coordinates we must ignore pairs where are negative values

[0][2] - if we add one column we will get [3] which is out of bounds (IndexError). We can use try...except to ignore IndexError and continue.

As I wrote earlier - one way to solve it is to find coordinates of zombies, based on that calculate coordinates of neighbours, then change neighbours values. Before next loop check whether there is any human left, if so repeat.

It's around 12 rows of code. If you can articulate your solution in spoken language translating into Python is not so difficult. If you don't have articulated plan it impossible. This is not about coding, its about problem solving.
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,367 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