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):
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.
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 2Columns 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.
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.