diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-12 17:45:45 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-12 17:45:45 +1000 |
| commit | 77152288a79d0382bcf33f7d87037b6cf82f98f8 (patch) | |
| tree | 4c655f119eeec8bfdd1c0b1eeb1e6f007bd8d9d1 /challenge-077/ash/python | |
| parent | fb64b5b9eb4c7c9f9ad2e74a1c8cd3c4b3bf69df (diff) | |
| parent | 2ee2505393ea8b144120538bbbdad94fa44d51d3 (diff) | |
| download | perlweeklychallenge-club-77152288a79d0382bcf33f7d87037b6cf82f98f8.tar.gz perlweeklychallenge-club-77152288a79d0382bcf33f7d87037b6cf82f98f8.tar.bz2 perlweeklychallenge-club-77152288a79d0382bcf33f7d87037b6cf82f98f8.zip | |
Merge remote-tracking branch 'upstream/master' into ch-077
Diffstat (limited to 'challenge-077/ash/python')
| -rw-r--r-- | challenge-077/ash/python/ch-2.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-077/ash/python/ch-2.py b/challenge-077/ash/python/ch-2.py new file mode 100644 index 0000000000..d5b7b5e506 --- /dev/null +++ b/challenge-077/ash/python/ch-2.py @@ -0,0 +1,42 @@ +# Task 2 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ + +# Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/ + +# Output for the given example: +# +# $ python3 ch-2.py +# Lonely X at position (0, 0). +# Lonely X at position (2, 1). +# Lonely X at position (0, 2). + +matrix = [ + ['X', 'O', 'O'], + ['O', 'O', 'X'], + ['X', 'O', 'O'], +] + +def is_O_cell(x, y): + if 0 <= x < len(matrix[0]) and 0 <= y < len(matrix): + return matrix[y][x] == 'O' + else: + return True + +x_cells = [ + [col_i, row_i] + for row_i, row in enumerate(matrix) + for col_i, cell in enumerate(row) + if cell == 'X' +] + +for x_cell in x_cells: + x, y = x_cell + if is_O_cell(x , y + 1) and \ + is_O_cell(x , y - 1) and \ + is_O_cell(x + 1, y ) and \ + is_O_cell(x - 1, y ) and \ + is_O_cell(x + 1, y + 1) and \ + is_O_cell(x + 1, y - 1) and \ + is_O_cell(x - 1, y + 1) and \ + is_O_cell(x - 1, y - 1): + print(f"Lonely X at position ({x}, {y}).") |
